blob: dd4c15d0d68f821112a6f8d4a69e06668e61246d [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
Sachin Kamat785acec2014-01-15 14:31:29 +053022#if defined(CONFIG_ARCH_S3C24XX) || defined(CONFIG_ARCH_S3C64XX)
Linus Walleijb0161ca2014-01-14 14:24:24 +010023#include <mach/gpio-samsung.h>
Sachin Kamat785acec2014-01-15 14:31:29 +053024#endif
25
Ben Dookse856bb12010-01-19 17:14:46 +090026#include <plat/gpio-core.h>
Ben Dooksd87964c2008-12-12 00:24:30 +000027#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
Kukjin Kim782d8a32011-08-30 20:47:32 +090035static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000036{
37 chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
38 chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
39}
40
Kukjin Kim782d8a32011-08-30 20:47:32 +090041static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000042{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +090067struct samsung_gpio_pm samsung_gpio_pm_1bit = {
68 .save = samsung_gpio_pm_1bit_save,
69 .resume = samsung_gpio_pm_1bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +000070};
71
Kukjin Kim782d8a32011-08-30 20:47:32 +090072static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +000073{
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/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900102 * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
Ben Dooksd87964c2008-12-12 00:24:30 +0000103 * @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 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900128static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000129{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900194struct samsung_gpio_pm samsung_gpio_pm_2bit = {
195 .save = samsung_gpio_pm_2bit_save,
196 .resume = samsung_gpio_pm_2bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +0000197};
198
Tomasz Figae245f962013-06-19 01:26:42 +0900199#if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P) \
200 || defined(CONFIG_ARCH_EXYNOS)
Kukjin Kim782d8a32011-08-30 20:47:32 +0900201static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000202{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900211static u32 samsung_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
Ben Dooksd87964c2008-12-12 00:24:30 +0000212{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900250static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip *chip, int index)
Ben Dooksd87964c2008-12-12 00:24:30 +0000251{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900257 mask = samsung_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
Ben Dooksd87964c2008-12-12 00:24:30 +0000258
259 gpcon = old_gpcon & ~mask;
260 gpcon |= gps_gpcon & mask;
261
262 __raw_writel(gpcon, con);
263}
264
Kukjin Kim782d8a32011-08-30 20:47:32 +0900265static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip *chip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000266{
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900277 samsung_gpio_pm_4bit_con(chip, 0);
Ben Dooksd87964c2008-12-12 00:24:30 +0000278 if (chip->chip.ngpio > 8) {
279 old_gpcon[0] = __raw_readl(base - 4);
Kukjin Kim782d8a32011-08-30 20:47:32 +0900280 samsung_gpio_pm_4bit_con(chip, -1);
Ben Dooksd87964c2008-12-12 00:24:30 +0000281 }
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900306struct samsung_gpio_pm samsung_gpio_pm_4bit = {
307 .save = samsung_gpio_pm_4bit_save,
308 .resume = samsung_gpio_pm_4bit_resume,
Ben Dooksd87964c2008-12-12 00:24:30 +0000309};
Tomasz Figae245f962013-06-19 01:26:42 +0900310#endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P || CONFIG_ARCH_EXYNOS */
Ben Dooksd87964c2008-12-12 00:24:30 +0000311
312/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900313 * samsung_pm_save_gpio() - save gpio chip data for suspend
Ben Dooksd87964c2008-12-12 00:24:30 +0000314 * @ourchip: The chip for suspend.
315 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900316static void samsung_pm_save_gpio(struct samsung_gpio_chip *ourchip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000317{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900318 struct samsung_gpio_pm *pm = ourchip->pm;
Ben Dooksd87964c2008-12-12 00:24:30 +0000319
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/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900327 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
Ben Dooksd87964c2008-12-12 00:24:30 +0000328 *
329 * For all the GPIO banks, save the state of each one ready for going
330 * into a suspend mode.
331 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900332void samsung_pm_save_gpios(void)
Ben Dooksd87964c2008-12-12 00:24:30 +0000333{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900334 struct samsung_gpio_chip *ourchip;
Ben Dooksd87964c2008-12-12 00:24:30 +0000335 unsigned int gpio_nr;
336
Ben Dooks32b6cb32010-05-18 19:07:05 +0900337 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
Kukjin Kim782d8a32011-08-30 20:47:32 +0900338 ourchip = samsung_gpiolib_getchip(gpio_nr);
Pinkava J91dc74e2010-05-23 06:42:26 +0200339 if (!ourchip) {
340 gpio_nr++;
Ben Dooksd87964c2008-12-12 00:24:30 +0000341 continue;
Pinkava J91dc74e2010-05-23 06:42:26 +0200342 }
Ben Dooksd87964c2008-12-12 00:24:30 +0000343
Kukjin Kim782d8a32011-08-30 20:47:32 +0900344 samsung_pm_save_gpio(ourchip);
Ben Dooksd87964c2008-12-12 00:24:30 +0000345
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/**
Kukjin Kim782d8a32011-08-30 20:47:32 +0900359 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
Ben Dooksd87964c2008-12-12 00:24:30 +0000360 * @ourchip: The suspended chip.
361 */
Kukjin Kim782d8a32011-08-30 20:47:32 +0900362static void samsung_pm_resume_gpio(struct samsung_gpio_chip *ourchip)
Ben Dooksd87964c2008-12-12 00:24:30 +0000363{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900364 struct samsung_gpio_pm *pm = ourchip->pm;
Ben Dooksd87964c2008-12-12 00:24:30 +0000365
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
Kukjin Kim782d8a32011-08-30 20:47:32 +0900372void samsung_pm_restore_gpios(void)
Ben Dooksd87964c2008-12-12 00:24:30 +0000373{
Kukjin Kim782d8a32011-08-30 20:47:32 +0900374 struct samsung_gpio_chip *ourchip;
Ben Dooksd87964c2008-12-12 00:24:30 +0000375 unsigned int gpio_nr;
376
Ben Dooks32b6cb32010-05-18 19:07:05 +0900377 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
Kukjin Kim782d8a32011-08-30 20:47:32 +0900378 ourchip = samsung_gpiolib_getchip(gpio_nr);
Pinkava J91dc74e2010-05-23 06:42:26 +0200379 if (!ourchip) {
380 gpio_nr++;
Ben Dooksd87964c2008-12-12 00:24:30 +0000381 continue;
Pinkava J91dc74e2010-05-23 06:42:26 +0200382 }
Ben Dooksd87964c2008-12-12 00:24:30 +0000383
Kukjin Kim782d8a32011-08-30 20:47:32 +0900384 samsung_pm_resume_gpio(ourchip);
Ben Dooksd87964c2008-12-12 00:24:30 +0000385
386 gpio_nr += ourchip->chip.ngpio;
387 gpio_nr += CONFIG_S3C_GPIO_SPACE;
388 }
389}