blob: a9f7a37c4173a0c35c1df96178f362855ff0fc97 [file] [log] [blame]
Ben Dooksd87964c2008-12-12 00:24:30 +00001
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>
Kay Sieversedbaa602011-12-21 16:26:03 -080017#include <linux/device.h>
Ben Dooksd87964c2008-12-12 00:24:30 +000018#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/gpio.h>
21
Linus Walleijb0161ca2014-01-14 14:24:24 +010022#include <mach/gpio-samsung.h>
Ben Dookse856bb12010-01-19 17:14:46 +090023#include <plat/gpio-core.h>
Ben Dooksd87964c2008-12-12 00:24:30 +000024#include <plat/pm.h>
25
26/* PM GPIO helpers */
27
28#define OFFS_CON (0x00)
29#define OFFS_DAT (0x04)
30#define OFFS_UP (0x08)
31
Kukjin Kim782d8a32011-08-30 20:47:32 +090032static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000033{
34 chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
35 chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
36}
37
Kukjin Kim782d8a32011-08-30 20:47:32 +090038static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000039{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +090064struct samsung_gpio_pm samsung_gpio_pm_1bit = {
65 .save = samsung_gpio_pm_1bit_save,
66 .resume = samsung_gpio_pm_1bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +000067};
68
Kukjin Kim782d8a32011-08-30 20:47:32 +090069static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000070{
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
79static 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
86static 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
93static inline int is_out(unsigned long con)
94{
95 return con == 1;
96}
97
98/**
Kukjin Kim782d8a32011-08-30 20:47:32 +090099 * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
Ben Dooksd87964c2008-12-12 00:24:30 +0000100 * @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 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900125static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000126{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900191struct samsung_gpio_pm samsung_gpio_pm_2bit = {
192 .save = samsung_gpio_pm_2bit_save,
193 .resume = samsung_gpio_pm_2bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +0000194};
195
Tomasz Figae245f962013-06-19 01:26:42 +0900196#if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P) \
197 || defined(CONFIG_ARCH_EXYNOS)
Kukjin Kim782d8a32011-08-30 20:47:32 +0900198static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000199{
200 chip->pm_save[1] = __raw_readl(chip->base + OFFS_CON);
201 chip->pm_save[2] = __raw_readl(chip->base + OFFS_DAT);
202 chip->pm_save[3] = __raw_readl(chip->base + OFFS_UP);
203
204 if (chip->chip.ngpio > 8)
205 chip->pm_save[0] = __raw_readl(chip->base - 4);
206}
207
Kukjin Kim782d8a32011-08-30 20:47:32 +0900208static u32 samsung_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
Ben Dooksd87964c2008-12-12 00:24:30 +0000209{
210 u32 old, new, mask;
211 u32 change_mask = 0x0;
212 int nr;
213
214 for (nr = 0, mask = 0x0f; nr < 16; nr += 4, mask <<= 4) {
215 old = (old_gpcon & mask) >> nr;
216 new = (gps_gpcon & mask) >> nr;
217
218 /* If there is no change, then skip */
219
220 if (old == new)
221 continue;
222
223 /* If both are special function, then skip */
224
225 if (is_sfn(old) && is_sfn(new))
226 continue;
227
228 /* Change is IN => OUT, do not change now */
229
230 if (is_in(old) && is_out(new))
231 continue;
232
233 /* Change is SFN => OUT, do not change now */
234
235 if (is_sfn(old) && is_out(new))
236 continue;
237
238 /* We should now be at the case of IN=>SFN,
239 * OUT=>SFN, OUT=>IN, SFN=>IN. */
240
241 change_mask |= mask;
242 }
243
244 return change_mask;
245}
246
Kukjin Kim782d8a32011-08-30 20:47:32 +0900247static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip *chip, int index)
Ben Dooksd87964c2008-12-12 00:24:30 +0000248{
249 void __iomem *con = chip->base + (index * 4);
250 u32 old_gpcon = __raw_readl(con);
251 u32 gps_gpcon = chip->pm_save[index + 1];
252 u32 gpcon, mask;
253
Kukjin Kim782d8a32011-08-30 20:47:32 +0900254 mask = samsung_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
Ben Dooksd87964c2008-12-12 00:24:30 +0000255
256 gpcon = old_gpcon & ~mask;
257 gpcon |= gps_gpcon & mask;
258
259 __raw_writel(gpcon, con);
260}
261
Kukjin Kim782d8a32011-08-30 20:47:32 +0900262static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000263{
264 void __iomem *base = chip->base;
265 u32 old_gpcon[2];
266 u32 old_gpdat = __raw_readl(base + OFFS_DAT);
267 u32 gps_gpdat = chip->pm_save[2];
268
269 /* First, modify the CON settings */
270
271 old_gpcon[0] = 0;
272 old_gpcon[1] = __raw_readl(base + OFFS_CON);
273
Kukjin Kim782d8a32011-08-30 20:47:32 +0900274 samsung_gpio_pm_4bit_con(chip, 0);
Ben Dooksd87964c2008-12-12 00:24:30 +0000275 if (chip->chip.ngpio > 8) {
276 old_gpcon[0] = __raw_readl(base - 4);
Kukjin Kim782d8a32011-08-30 20:47:32 +0900277 samsung_gpio_pm_4bit_con(chip, -1);
Ben Dooksd87964c2008-12-12 00:24:30 +0000278 }
279
280 /* Now change the configurations that require DAT,CON */
281
282 __raw_writel(chip->pm_save[2], base + OFFS_DAT);
283 __raw_writel(chip->pm_save[1], base + OFFS_CON);
284 if (chip->chip.ngpio > 8)
285 __raw_writel(chip->pm_save[0], base - 4);
286
287 __raw_writel(chip->pm_save[2], base + OFFS_DAT);
288 __raw_writel(chip->pm_save[3], base + OFFS_UP);
289
290 if (chip->chip.ngpio > 8) {
291 S3C_PMDBG("%s: CON4 %08x,%08x => %08x,%08x, DAT %08x => %08x\n",
292 chip->chip.label, old_gpcon[0], old_gpcon[1],
293 __raw_readl(base - 4),
294 __raw_readl(base + OFFS_CON),
295 old_gpdat, gps_gpdat);
296 } else
297 S3C_PMDBG("%s: CON4 %08x => %08x, DAT %08x => %08x\n",
298 chip->chip.label, old_gpcon[1],
299 __raw_readl(base + OFFS_CON),
300 old_gpdat, gps_gpdat);
301}
302
Kukjin Kim782d8a32011-08-30 20:47:32 +0900303struct samsung_gpio_pm samsung_gpio_pm_4bit = {
304 .save = samsung_gpio_pm_4bit_save,
305 .resume = samsung_gpio_pm_4bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +0000306};
Tomasz Figae245f962013-06-19 01:26:42 +0900307#endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P || CONFIG_ARCH_EXYNOS */
Ben Dooksd87964c2008-12-12 00:24:30 +0000308
309/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900310 * samsung_pm_save_gpio() - save gpio chip data for suspend
Ben Dooksd87964c2008-12-12 00:24:30 +0000311 * @ourchip: The chip for suspend.
312 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900313static void samsung_pm_save_gpio(struct samsung_gpio_chip *ourchip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000314{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900315 struct samsung_gpio_pm *pm = ourchip->pm;
Ben Dooksd87964c2008-12-12 00:24:30 +0000316
317 if (pm == NULL || pm->save == NULL)
318 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
319 else
320 pm->save(ourchip);
321}
322
323/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900324 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
Ben Dooksd87964c2008-12-12 00:24:30 +0000325 *
326 * For all the GPIO banks, save the state of each one ready for going
327 * into a suspend mode.
328 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900329void samsung_pm_save_gpios(void)
Ben Dooksd87964c2008-12-12 00:24:30 +0000330{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900331 struct samsung_gpio_chip *ourchip;
Ben Dooksd87964c2008-12-12 00:24:30 +0000332 unsigned int gpio_nr;
333
Ben Dooks32b6cb32010-05-18 19:07:05 +0900334 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
Kukjin Kim782d8a32011-08-30 20:47:32 +0900335 ourchip = samsung_gpiolib_getchip(gpio_nr);
Pinkava J91dc74e2010-05-23 06:42:26 +0200336 if (!ourchip) {
337 gpio_nr++;
Ben Dooksd87964c2008-12-12 00:24:30 +0000338 continue;
Pinkava J91dc74e2010-05-23 06:42:26 +0200339 }
Ben Dooksd87964c2008-12-12 00:24:30 +0000340
Kukjin Kim782d8a32011-08-30 20:47:32 +0900341 samsung_pm_save_gpio(ourchip);
Ben Dooksd87964c2008-12-12 00:24:30 +0000342
343 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
344 ourchip->chip.label,
345 ourchip->pm_save[0],
346 ourchip->pm_save[1],
347 ourchip->pm_save[2],
348 ourchip->pm_save[3]);
349
350 gpio_nr += ourchip->chip.ngpio;
351 gpio_nr += CONFIG_S3C_GPIO_SPACE;
352 }
353}
354
355/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900356 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
Ben Dooksd87964c2008-12-12 00:24:30 +0000357 * @ourchip: The suspended chip.
358 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900359static void samsung_pm_resume_gpio(struct samsung_gpio_chip *ourchip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000360{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900361 struct samsung_gpio_pm *pm = ourchip->pm;
Ben Dooksd87964c2008-12-12 00:24:30 +0000362
363 if (pm == NULL || pm->resume == NULL)
364 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
365 else
366 pm->resume(ourchip);
367}
368
Kukjin Kim782d8a32011-08-30 20:47:32 +0900369void samsung_pm_restore_gpios(void)
Ben Dooksd87964c2008-12-12 00:24:30 +0000370{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900371 struct samsung_gpio_chip *ourchip;
Ben Dooksd87964c2008-12-12 00:24:30 +0000372 unsigned int gpio_nr;
373
Ben Dooks32b6cb32010-05-18 19:07:05 +0900374 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
Kukjin Kim782d8a32011-08-30 20:47:32 +0900375 ourchip = samsung_gpiolib_getchip(gpio_nr);
Pinkava J91dc74e2010-05-23 06:42:26 +0200376 if (!ourchip) {
377 gpio_nr++;
Ben Dooksd87964c2008-12-12 00:24:30 +0000378 continue;
Pinkava J91dc74e2010-05-23 06:42:26 +0200379 }
Ben Dooksd87964c2008-12-12 00:24:30 +0000380
Kukjin Kim782d8a32011-08-30 20:47:32 +0900381 samsung_pm_resume_gpio(ourchip);
Ben Dooksd87964c2008-12-12 00:24:30 +0000382
383 gpio_nr += ourchip->chip.ngpio;
384 gpio_nr += CONFIG_S3C_GPIO_SPACE;
385 }
386}