]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/plat-samsung/pm-gpio.c
Merge tag 'clk-for-linus-3.14-part2' of git://git.linaro.org/people/mike.turquette...
[karo-tx-linux.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/device.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/gpio.h>
21
22 #if defined(CONFIG_ARCH_S3C24XX) || defined(CONFIG_ARCH_S3C64XX)
23 #include <mach/gpio-samsung.h>
24 #endif
25
26 #include <plat/gpio-core.h>
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
35 static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip *chip)
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
41 static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip *chip)
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
67 struct samsung_gpio_pm samsung_gpio_pm_1bit = {
68         .save   = samsung_gpio_pm_1bit_save,
69         .resume = samsung_gpio_pm_1bit_resume,
70 };
71
72 static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip *chip)
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
82 static 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
89 static 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
96 static inline int is_out(unsigned long con)
97 {
98         return con == 1;
99 }
100
101 /**
102  * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
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  */
128 static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip *chip)
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
194 struct samsung_gpio_pm samsung_gpio_pm_2bit = {
195         .save   = samsung_gpio_pm_2bit_save,
196         .resume = samsung_gpio_pm_2bit_resume,
197 };
198
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)
202 {
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);
206
207         if (chip->chip.ngpio > 8)
208                 chip->pm_save[0] = __raw_readl(chip->base - 4);
209 }
210
211 static u32 samsung_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
212 {
213         u32 old, new, mask;
214         u32 change_mask = 0x0;
215         int nr;
216
217         for (nr = 0, mask = 0x0f; nr < 16; nr += 4, mask <<= 4) {
218                 old = (old_gpcon & mask) >> nr;
219                 new = (gps_gpcon & mask) >> nr;
220
221                 /* If there is no change, then skip */
222
223                 if (old == new)
224                         continue;
225
226                 /* If both are special function, then skip */
227
228                 if (is_sfn(old) && is_sfn(new))
229                         continue;
230
231                 /* Change is IN => OUT, do not change now */
232
233                 if (is_in(old) && is_out(new))
234                         continue;
235
236                 /* Change is SFN => OUT, do not change now */
237
238                 if (is_sfn(old) && is_out(new))
239                         continue;
240
241                 /* We should now be at the case of IN=>SFN,
242                  * OUT=>SFN, OUT=>IN, SFN=>IN. */
243
244                 change_mask |= mask;
245         }
246
247         return change_mask;
248 }
249
250 static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip *chip, int index)
251 {
252         void __iomem *con = chip->base + (index * 4);
253         u32 old_gpcon = __raw_readl(con);
254         u32 gps_gpcon = chip->pm_save[index + 1];
255         u32 gpcon, mask;
256
257         mask = samsung_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
258
259         gpcon = old_gpcon & ~mask;
260         gpcon |= gps_gpcon & mask;
261
262         __raw_writel(gpcon, con);
263 }
264
265 static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip *chip)
266 {
267         void __iomem *base = chip->base;
268         u32 old_gpcon[2];
269         u32 old_gpdat = __raw_readl(base + OFFS_DAT);
270         u32 gps_gpdat = chip->pm_save[2];
271
272         /* First, modify the CON settings */
273
274         old_gpcon[0] = 0;
275         old_gpcon[1] = __raw_readl(base + OFFS_CON);
276
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);
281         }
282
283         /* Now change the configurations that require DAT,CON */
284
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);
289
290         __raw_writel(chip->pm_save[2], base + OFFS_DAT);
291         __raw_writel(chip->pm_save[3], base + OFFS_UP);
292
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);
299         } else
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);
304 }
305
306 struct samsung_gpio_pm samsung_gpio_pm_4bit = {
307         .save   = samsung_gpio_pm_4bit_save,
308         .resume = samsung_gpio_pm_4bit_resume,
309 };
310 #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P || CONFIG_ARCH_EXYNOS */
311
312 /**
313  * samsung_pm_save_gpio() - save gpio chip data for suspend
314  * @ourchip: The chip for suspend.
315  */
316 static void samsung_pm_save_gpio(struct samsung_gpio_chip *ourchip)
317 {
318         struct samsung_gpio_pm *pm = ourchip->pm;
319
320         if (pm == NULL || pm->save == NULL)
321                 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
322         else
323                 pm->save(ourchip);
324 }
325
326 /**
327  * samsung_pm_save_gpios() - Save the state of the GPIO banks.
328  *
329  * For all the GPIO banks, save the state of each one ready for going
330  * into a suspend mode.
331  */
332 void samsung_pm_save_gpios(void)
333 {
334         struct samsung_gpio_chip *ourchip;
335         unsigned int gpio_nr;
336
337         for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
338                 ourchip = samsung_gpiolib_getchip(gpio_nr);
339                 if (!ourchip) {
340                         gpio_nr++;
341                         continue;
342                 }
343
344                 samsung_pm_save_gpio(ourchip);
345
346                 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
347                           ourchip->chip.label,
348                           ourchip->pm_save[0],
349                           ourchip->pm_save[1],
350                           ourchip->pm_save[2],
351                           ourchip->pm_save[3]);
352
353                 gpio_nr += ourchip->chip.ngpio;
354                 gpio_nr += CONFIG_S3C_GPIO_SPACE;
355         }
356 }
357
358 /**
359  * samsung_pm_resume_gpio() - restore gpio chip data after suspend
360  * @ourchip: The suspended chip.
361  */
362 static void samsung_pm_resume_gpio(struct samsung_gpio_chip *ourchip)
363 {
364         struct samsung_gpio_pm *pm = ourchip->pm;
365
366         if (pm == NULL || pm->resume == NULL)
367                 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
368         else
369                 pm->resume(ourchip);
370 }
371
372 void samsung_pm_restore_gpios(void)
373 {
374         struct samsung_gpio_chip *ourchip;
375         unsigned int gpio_nr;
376
377         for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
378                 ourchip = samsung_gpiolib_getchip(gpio_nr);
379                 if (!ourchip) {
380                         gpio_nr++;
381                         continue;
382                 }
383
384                 samsung_pm_resume_gpio(ourchip);
385
386                 gpio_nr += ourchip->chip.ngpio;
387                 gpio_nr += CONFIG_S3C_GPIO_SPACE;
388         }
389 }