blob: 971e5d3798e47838ba9a928f3fb2babb9de0e5d2 [file] [log] [blame]
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001/*
2 * Generic GPIO driver for logic cells found in the Nomadik SoC
3 *
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010016#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010017#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010018#include <linux/clk.h>
19#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010020#include <linux/gpio.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010025
Rabin Vincent378be062010-06-02 06:06:29 +010026#include <plat/pincfg.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010027#include <mach/hardware.h>
28#include <mach/gpio.h>
29
30/*
31 * The GPIO module in the Nomadik family of Systems-on-Chip is an
32 * AMBA device, managing 32 pins and alternate functions. The logic block
33 * is currently only used in the Nomadik.
34 *
35 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
36 */
37
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010038struct nmk_gpio_chip {
39 struct gpio_chip chip;
40 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010041 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053042 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010043 unsigned int parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053044 unsigned int secondary_parent_irq;
45 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010046 spinlock_t lock;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010047 /* Keep track of configured edges */
48 u32 edge_rising;
49 u32 edge_falling;
Rabin Vincentc84c7c02010-03-17 15:19:04 +053050 u32 backup[10];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010051};
52
Rabin Vincent6f9a9742010-06-02 05:50:28 +010053static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
54 unsigned offset, int gpio_mode)
55{
56 u32 bit = 1 << offset;
57 u32 afunc, bfunc;
58
59 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
60 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
61 if (gpio_mode & NMK_GPIO_ALT_A)
62 afunc |= bit;
63 if (gpio_mode & NMK_GPIO_ALT_B)
64 bfunc |= bit;
65 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
66 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
67}
68
Rabin Vincent81a3c292010-05-27 12:39:23 +010069static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
70 unsigned offset, enum nmk_gpio_slpm mode)
71{
72 u32 bit = 1 << offset;
73 u32 slpm;
74
75 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
76 if (mode == NMK_GPIO_SLPM_NOCHANGE)
77 slpm |= bit;
78 else
79 slpm &= ~bit;
80 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
81}
82
Rabin Vincent5b327ed2010-05-27 12:29:50 +010083static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
84 unsigned offset, enum nmk_gpio_pull pull)
85{
86 u32 bit = 1 << offset;
87 u32 pdis;
88
89 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
90 if (pull == NMK_GPIO_PULL_NONE)
91 pdis |= bit;
92 else
93 pdis &= ~bit;
94 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
95
96 if (pull == NMK_GPIO_PULL_UP)
97 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
98 else if (pull == NMK_GPIO_PULL_DOWN)
99 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
100}
101
Rabin Vincent378be062010-06-02 06:06:29 +0100102static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
103 unsigned offset)
104{
105 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
106}
107
Rabin Vincent6720db72010-09-02 11:28:48 +0100108static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
109 unsigned offset, int val)
110{
111 if (val)
112 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
113 else
114 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
115}
116
117static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
118 unsigned offset, int val)
119{
120 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
121 __nmk_gpio_set_output(nmk_chip, offset, val);
122}
123
Rabin Vincent378be062010-06-02 06:06:29 +0100124static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincentdacdc962010-12-03 20:35:37 +0530125 pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100126{
127 static const char *afnames[] = {
128 [NMK_GPIO_ALT_GPIO] = "GPIO",
129 [NMK_GPIO_ALT_A] = "A",
130 [NMK_GPIO_ALT_B] = "B",
131 [NMK_GPIO_ALT_C] = "C"
132 };
133 static const char *pullnames[] = {
134 [NMK_GPIO_PULL_NONE] = "none",
135 [NMK_GPIO_PULL_UP] = "up",
136 [NMK_GPIO_PULL_DOWN] = "down",
137 [3] /* illegal */ = "??"
138 };
139 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100140 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
141 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100142 };
143
144 int pin = PIN_NUM(cfg);
145 int pull = PIN_PULL(cfg);
146 int af = PIN_ALT(cfg);
147 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100148 int output = PIN_DIR(cfg);
149 int val = PIN_VAL(cfg);
Rabin Vincent378be062010-06-02 06:06:29 +0100150
Rabin Vincentdacdc962010-12-03 20:35:37 +0530151 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
152 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100153 output ? "output " : "input",
154 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100155
Rabin Vincentdacdc962010-12-03 20:35:37 +0530156 if (sleep) {
157 int slpm_pull = PIN_SLPM_PULL(cfg);
158 int slpm_output = PIN_SLPM_DIR(cfg);
159 int slpm_val = PIN_SLPM_VAL(cfg);
160
161 /*
162 * The SLPM_* values are normal values + 1 to allow zero to
163 * mean "same as normal".
164 */
165 if (slpm_pull)
166 pull = slpm_pull - 1;
167 if (slpm_output)
168 output = slpm_output - 1;
169 if (slpm_val)
170 val = slpm_val - 1;
171
172 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
173 pin,
174 slpm_pull ? pullnames[pull] : "same",
175 slpm_output ? (output ? "output" : "input") : "same",
176 slpm_val ? (val ? "high" : "low") : "same");
177 }
178
Rabin Vincent6720db72010-09-02 11:28:48 +0100179 if (output)
180 __nmk_gpio_make_output(nmk_chip, offset, val);
181 else {
182 __nmk_gpio_make_input(nmk_chip, offset);
183 __nmk_gpio_set_pull(nmk_chip, offset, pull);
184 }
185
Rabin Vincent378be062010-06-02 06:06:29 +0100186 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
187 __nmk_gpio_set_mode(nmk_chip, offset, af);
188}
189
190/**
191 * nmk_config_pin - configure a pin's mux attributes
192 * @cfg: pin confguration
193 *
194 * Configures a pin's mode (alternate function or GPIO), its pull up status,
195 * and its sleep mode based on the specified configuration. The @cfg is
196 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
197 * are constructed using, and can be further enhanced with, the macros in
198 * plat/pincfg.h.
199 *
200 * If a pin's mode is set to GPIO, it is configured as an input to avoid
201 * side-effects. The gpio can be manipulated later using standard GPIO API
202 * calls.
203 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530204int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100205{
206 struct nmk_gpio_chip *nmk_chip;
207 int gpio = PIN_NUM(cfg);
208 unsigned long flags;
209
210 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
211 if (!nmk_chip)
212 return -EINVAL;
213
214 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530215 __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100216 spin_unlock_irqrestore(&nmk_chip->lock, flags);
217
218 return 0;
219}
220EXPORT_SYMBOL(nmk_config_pin);
221
222/**
223 * nmk_config_pins - configure several pins at once
224 * @cfgs: array of pin configurations
225 * @num: number of elments in the array
226 *
227 * Configures several pins using nmk_config_pin(). Refer to that function for
228 * further information.
229 */
230int nmk_config_pins(pin_cfg_t *cfgs, int num)
231{
232 int ret = 0;
233 int i;
234
235 for (i = 0; i < num; i++) {
Rabin Vincentdacdc962010-12-03 20:35:37 +0530236 ret = nmk_config_pin(cfgs[i], false);
Rabin Vincent378be062010-06-02 06:06:29 +0100237 if (ret)
238 break;
239 }
240
241 return ret;
242}
243EXPORT_SYMBOL(nmk_config_pins);
244
Rabin Vincentdacdc962010-12-03 20:35:37 +0530245int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
246{
247 int ret = 0;
248 int i;
249
250 for (i = 0; i < num; i++) {
251 ret = nmk_config_pin(cfgs[i], true);
252 if (ret)
253 break;
254 }
255
256 return ret;
257}
258EXPORT_SYMBOL(nmk_config_pins_sleep);
259
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100260/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100261 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
262 * @gpio: pin number
263 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
264 *
265 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
266 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
267 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
268 * configured even when in sleep and deep sleep.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100269 *
270 * On DB8500v2 onwards, this setting loses the previous meaning and instead
271 * indicates if wakeup detection is enabled on the pin. Note that
272 * enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100273 */
274int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
275{
276 struct nmk_gpio_chip *nmk_chip;
277 unsigned long flags;
278
279 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
280 if (!nmk_chip)
281 return -EINVAL;
282
283 spin_lock_irqsave(&nmk_chip->lock, flags);
284 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
285 spin_unlock_irqrestore(&nmk_chip->lock, flags);
286
287 return 0;
288}
289
290/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100291 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
292 * @gpio: pin number
293 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
294 *
295 * Enables/disables pull up/down on a specified pin. This only takes effect if
296 * the pin is configured as an input (either explicitly or by the alternate
297 * function).
298 *
299 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
300 * configured as an input. Otherwise, due to the way the controller registers
301 * work, this function will change the value output on the pin.
302 */
303int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
304{
305 struct nmk_gpio_chip *nmk_chip;
306 unsigned long flags;
307
308 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
309 if (!nmk_chip)
310 return -EINVAL;
311
312 spin_lock_irqsave(&nmk_chip->lock, flags);
313 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
314 spin_unlock_irqrestore(&nmk_chip->lock, flags);
315
316 return 0;
317}
318
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100319/* Mode functions */
320int nmk_gpio_set_mode(int gpio, int gpio_mode)
321{
322 struct nmk_gpio_chip *nmk_chip;
323 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100324
325 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
326 if (!nmk_chip)
327 return -EINVAL;
328
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100329 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100330 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100331 spin_unlock_irqrestore(&nmk_chip->lock, flags);
332
333 return 0;
334}
335EXPORT_SYMBOL(nmk_gpio_set_mode);
336
337int nmk_gpio_get_mode(int gpio)
338{
339 struct nmk_gpio_chip *nmk_chip;
340 u32 afunc, bfunc, bit;
341
342 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
343 if (!nmk_chip)
344 return -EINVAL;
345
346 bit = 1 << (gpio - nmk_chip->chip.base);
347
348 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
349 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
350
351 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
352}
353EXPORT_SYMBOL(nmk_gpio_get_mode);
354
355
356/* IRQ functions */
357static inline int nmk_gpio_get_bitmask(int gpio)
358{
359 return 1 << (gpio % 32);
360}
361
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100362static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100363{
364 int gpio;
365 struct nmk_gpio_chip *nmk_chip;
366
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100367 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
368 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100369 if (!nmk_chip)
370 return;
371 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
372}
373
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100374enum nmk_gpio_irq_type {
375 NORMAL,
376 WAKE,
377};
378
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100379static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100380 int gpio, enum nmk_gpio_irq_type which,
381 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100382{
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100383 u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
384 u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100385 u32 bitmask = nmk_gpio_get_bitmask(gpio);
386 u32 reg;
387
388 /* we must individually set/clear the two edges */
389 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100390 reg = readl(nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100391 if (enable)
392 reg |= bitmask;
393 else
394 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100395 writel(reg, nmk_chip->addr + rimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100396 }
397 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100398 reg = readl(nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100399 if (enable)
400 reg |= bitmask;
401 else
402 reg &= ~bitmask;
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100403 writel(reg, nmk_chip->addr + fimsc);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100404 }
405}
406
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100407static int nmk_gpio_irq_modify(struct irq_data *d, enum nmk_gpio_irq_type which,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100408 bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100409{
410 int gpio;
411 struct nmk_gpio_chip *nmk_chip;
412 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100413 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100414
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100415 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
416 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100417 bitmask = nmk_gpio_get_bitmask(gpio);
418 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100419 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100420
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100421 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100422 __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100423 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100424
425 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100426}
427
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100428static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100429{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100430 nmk_gpio_irq_modify(d, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100431}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100432
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100433static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100434{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100435 nmk_gpio_irq_modify(d, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100436}
437
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100438static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100439{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100440 struct nmk_gpio_chip *nmk_chip;
441 unsigned long flags;
442 int gpio;
443
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100444 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
445 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100446 if (!nmk_chip)
447 return -EINVAL;
448
449 spin_lock_irqsave(&nmk_chip->lock, flags);
450#ifdef CONFIG_ARCH_U8500
451 if (cpu_is_u8500v2()) {
452 __nmk_gpio_set_slpm(nmk_chip, gpio,
453 on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
454 : NMK_GPIO_SLPM_WAKEUP_DISABLE);
455 }
456#endif
457 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
458 spin_unlock_irqrestore(&nmk_chip->lock, flags);
459
460 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100461}
462
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100463static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100464{
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100465 struct irq_desc *desc = irq_to_desc(d->irq);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100466 bool enabled = !(desc->status & IRQ_DISABLED);
467 bool wake = desc->wake_depth;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100468 int gpio;
469 struct nmk_gpio_chip *nmk_chip;
470 unsigned long flags;
471 u32 bitmask;
472
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100473 gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
474 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100475 bitmask = nmk_gpio_get_bitmask(gpio);
476 if (!nmk_chip)
477 return -EINVAL;
478
479 if (type & IRQ_TYPE_LEVEL_HIGH)
480 return -EINVAL;
481 if (type & IRQ_TYPE_LEVEL_LOW)
482 return -EINVAL;
483
484 spin_lock_irqsave(&nmk_chip->lock, flags);
485
Rabin Vincent7a852d82010-05-06 10:43:55 +0100486 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100487 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
488
489 if (wake)
490 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100491
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100492 nmk_chip->edge_rising &= ~bitmask;
493 if (type & IRQ_TYPE_EDGE_RISING)
494 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100495
496 nmk_chip->edge_falling &= ~bitmask;
497 if (type & IRQ_TYPE_EDGE_FALLING)
498 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100499
500 if (enabled)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100501 __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
502
503 if (wake)
504 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100505
506 spin_unlock_irqrestore(&nmk_chip->lock, flags);
507
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100508 return 0;
509}
510
511static struct irq_chip nmk_gpio_irq_chip = {
512 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100513 .irq_ack = nmk_gpio_irq_ack,
514 .irq_mask = nmk_gpio_irq_mask,
515 .irq_unmask = nmk_gpio_irq_unmask,
516 .irq_set_type = nmk_gpio_irq_set_type,
517 .irq_set_wake = nmk_gpio_irq_set_wake,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100518};
519
Rabin Vincent33b744b2010-10-14 10:38:03 +0530520static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
521 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100522{
523 struct nmk_gpio_chip *nmk_chip;
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100524 struct irq_chip *host_chip = get_irq_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100525 unsigned int first_irq;
526
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100527 if (host_chip->irq_mask_ack)
528 host_chip->irq_mask_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100529 else {
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100530 host_chip->irq_mask(&desc->irq_data);
531 if (host_chip->irq_ack)
532 host_chip->irq_ack(&desc->irq_data);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100533 }
534
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100535 nmk_chip = get_irq_data(irq);
536 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530537 while (status) {
538 int bit = __ffs(status);
539
540 generic_handle_irq(first_irq + bit);
541 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100542 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100543
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100544 host_chip->irq_unmask(&desc->irq_data);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100545}
546
Rabin Vincent33b744b2010-10-14 10:38:03 +0530547static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
548{
549 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
550 u32 status = readl(nmk_chip->addr + NMK_GPIO_IS);
551
552 __nmk_gpio_irq_handler(irq, desc, status);
553}
554
555static void nmk_gpio_secondary_irq_handler(unsigned int irq,
556 struct irq_desc *desc)
557{
558 struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
559 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
560
561 __nmk_gpio_irq_handler(irq, desc, status);
562}
563
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100564static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
565{
566 unsigned int first_irq;
567 int i;
568
569 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
Rabin Vincente493e062010-03-18 12:35:22 +0530570 for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100571 set_irq_chip(i, &nmk_gpio_irq_chip);
572 set_irq_handler(i, handle_edge_irq);
573 set_irq_flags(i, IRQF_VALID);
574 set_irq_chip_data(i, nmk_chip);
Rabin Vincent2210d642010-05-06 10:45:18 +0100575 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100576 }
Rabin Vincent33b744b2010-10-14 10:38:03 +0530577
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100578 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
579 set_irq_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530580
581 if (nmk_chip->secondary_parent_irq >= 0) {
582 set_irq_chained_handler(nmk_chip->secondary_parent_irq,
583 nmk_gpio_secondary_irq_handler);
584 set_irq_data(nmk_chip->secondary_parent_irq, nmk_chip);
585 }
586
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100587 return 0;
588}
589
590/* I/O Functions */
591static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
592{
593 struct nmk_gpio_chip *nmk_chip =
594 container_of(chip, struct nmk_gpio_chip, chip);
595
596 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
597 return 0;
598}
599
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100600static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
601{
602 struct nmk_gpio_chip *nmk_chip =
603 container_of(chip, struct nmk_gpio_chip, chip);
604 u32 bit = 1 << offset;
605
606 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
607}
608
609static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
610 int val)
611{
612 struct nmk_gpio_chip *nmk_chip =
613 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100614
Rabin Vincent6720db72010-09-02 11:28:48 +0100615 __nmk_gpio_set_output(nmk_chip, offset, val);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100616}
617
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100618static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
619 int val)
620{
621 struct nmk_gpio_chip *nmk_chip =
622 container_of(chip, struct nmk_gpio_chip, chip);
623
Rabin Vincent6720db72010-09-02 11:28:48 +0100624 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100625
626 return 0;
627}
628
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100629static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
630{
631 struct nmk_gpio_chip *nmk_chip =
632 container_of(chip, struct nmk_gpio_chip, chip);
633
634 return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
635}
636
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530637#ifdef CONFIG_DEBUG_FS
638
639#include <linux/seq_file.h>
640
641static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
642{
643 int mode;
644 unsigned i;
645 unsigned gpio = chip->base;
646 int is_out;
647 struct nmk_gpio_chip *nmk_chip =
648 container_of(chip, struct nmk_gpio_chip, chip);
649 const char *modes[] = {
650 [NMK_GPIO_ALT_GPIO] = "gpio",
651 [NMK_GPIO_ALT_A] = "altA",
652 [NMK_GPIO_ALT_B] = "altB",
653 [NMK_GPIO_ALT_C] = "altC",
654 };
655
656 for (i = 0; i < chip->ngpio; i++, gpio++) {
657 const char *label = gpiochip_is_requested(chip, i);
658 bool pull;
659 u32 bit = 1 << i;
660
661 if (!label)
662 continue;
663
664 is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
665 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
666 mode = nmk_gpio_get_mode(gpio);
667 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
668 gpio, label,
669 is_out ? "out" : "in ",
670 chip->get
671 ? (chip->get(chip, i) ? "hi" : "lo")
672 : "? ",
673 (mode < 0) ? "unknown" : modes[mode],
674 pull ? "pull" : "none");
675
676 if (!is_out) {
677 int irq = gpio_to_irq(gpio);
678 struct irq_desc *desc = irq_to_desc(irq);
679
680 /* This races with request_irq(), set_irq_type(),
681 * and set_irq_wake() ... but those are "rare".
682 *
683 * More significantly, trigger type flags aren't
684 * currently maintained by genirq.
685 */
686 if (irq >= 0 && desc->action) {
687 char *trigger;
688
689 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
690 case IRQ_TYPE_NONE:
691 trigger = "(default)";
692 break;
693 case IRQ_TYPE_EDGE_FALLING:
694 trigger = "edge-falling";
695 break;
696 case IRQ_TYPE_EDGE_RISING:
697 trigger = "edge-rising";
698 break;
699 case IRQ_TYPE_EDGE_BOTH:
700 trigger = "edge-both";
701 break;
702 case IRQ_TYPE_LEVEL_HIGH:
703 trigger = "level-high";
704 break;
705 case IRQ_TYPE_LEVEL_LOW:
706 trigger = "level-low";
707 break;
708 default:
709 trigger = "?trigger?";
710 break;
711 }
712
713 seq_printf(s, " irq-%d %s%s",
714 irq, trigger,
715 (desc->status & IRQ_WAKEUP)
716 ? " wakeup" : "");
717 }
718 }
719
720 seq_printf(s, "\n");
721 }
722}
723
724#else
725#define nmk_gpio_dbg_show NULL
726#endif
727
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100728/* This structure is replicated for each GPIO block allocated at probe time */
729static struct gpio_chip nmk_gpio_template = {
730 .direction_input = nmk_gpio_make_input,
731 .get = nmk_gpio_get_input,
732 .direction_output = nmk_gpio_make_output,
733 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100734 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530735 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100736 .can_sleep = 0,
737};
738
Uwe Kleine-Königfd0d67d2010-09-02 16:13:35 +0100739static int __devinit nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100740{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100741 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100742 struct nmk_gpio_chip *nmk_chip;
743 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100744 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100745 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530746 int secondary_irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100747 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100748 int ret;
749
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100750 if (!pdata)
751 return -ENODEV;
752
753 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
754 if (!res) {
755 ret = -ENOENT;
756 goto out;
757 }
758
759 irq = platform_get_irq(dev, 0);
760 if (irq < 0) {
761 ret = irq;
762 goto out;
763 }
764
Rabin Vincent33b744b2010-10-14 10:38:03 +0530765 secondary_irq = platform_get_irq(dev, 1);
766 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
767 ret = -EINVAL;
768 goto out;
769 }
770
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100771 if (request_mem_region(res->start, resource_size(res),
772 dev_name(&dev->dev)) == NULL) {
773 ret = -EBUSY;
774 goto out;
775 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100776
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100777 clk = clk_get(&dev->dev, NULL);
778 if (IS_ERR(clk)) {
779 ret = PTR_ERR(clk);
780 goto out_release;
781 }
782
783 clk_enable(clk);
784
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100785 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
786 if (!nmk_chip) {
787 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100788 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100789 }
790 /*
791 * The virt address in nmk_chip->addr is in the nomadik register space,
792 * so we can simply convert the resource address, without remapping
793 */
Rabin Vincent33b744b2010-10-14 10:38:03 +0530794 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100795 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100796 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100797 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100798 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530799 nmk_chip->secondary_parent_irq = secondary_irq;
800 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +0100801 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100802
803 chip = &nmk_chip->chip;
804 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +0530805 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +0530806 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100807 chip->dev = &dev->dev;
808 chip->owner = THIS_MODULE;
809
810 ret = gpiochip_add(&nmk_chip->chip);
811 if (ret)
812 goto out_free;
813
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100814 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100815
816 nmk_gpio_init_irq(nmk_chip);
817
818 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
819 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
820 return 0;
821
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100822out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100823 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100824out_clk:
825 clk_disable(clk);
826 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100827out_release:
828 release_mem_region(res->start, resource_size(res));
829out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100830 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
831 pdata->first_gpio, pdata->first_gpio+31);
832 return ret;
833}
834
Rabin Vincentc84c7c02010-03-17 15:19:04 +0530835#ifdef CONFIG_PM
836static int nmk_gpio_pm(struct platform_device *dev, bool suspend)
837{
838 struct nmk_gpio_chip *nmk_chip = platform_get_drvdata(dev);
839 int i;
840 static const unsigned int regs[] = {
841 NMK_GPIO_DAT,
842 NMK_GPIO_PDIS,
843 NMK_GPIO_DIR,
844 NMK_GPIO_AFSLA,
845 NMK_GPIO_AFSLB,
846 NMK_GPIO_SLPC,
847 NMK_GPIO_RIMSC,
848 NMK_GPIO_FIMSC,
849 NMK_GPIO_RWIMSC,
850 NMK_GPIO_FWIMSC,
851 };
852
853 BUILD_BUG_ON(ARRAY_SIZE(nmk_chip->backup) != ARRAY_SIZE(regs));
854
855 /* XXX: is this sufficient? what about pull-up/down configuration? */
856
857 for (i = 0; i < ARRAY_SIZE(regs); i++) {
858 if (suspend)
859 nmk_chip->backup[i] = readl(nmk_chip->addr + regs[i]);
860 else
861 writel(nmk_chip->backup[i], nmk_chip->addr + regs[i]);
862 }
863
864 return 0;
865}
866
867static int nmk_gpio_suspend(struct platform_device *dev, pm_message_t state)
868{
869 return nmk_gpio_pm(dev, true);
870}
871
872static int nmk_gpio_resume(struct platform_device *dev)
873{
874 return nmk_gpio_pm(dev, false);
875}
876#else
877#define nmk_gpio_suspend NULL
878#define nmk_gpio_resume NULL
879#endif
880
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100881static struct platform_driver nmk_gpio_driver = {
882 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100883 .owner = THIS_MODULE,
884 .name = "gpio",
885 },
886 .probe = nmk_gpio_probe,
Rabin Vincentc84c7c02010-03-17 15:19:04 +0530887 .suspend = nmk_gpio_suspend,
888 .resume = nmk_gpio_resume,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100889};
890
891static int __init nmk_gpio_init(void)
892{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100893 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100894}
895
Rabin Vincent33f45ea2010-06-02 06:09:52 +0100896core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100897
898MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
899MODULE_DESCRIPTION("Nomadik GPIO Driver");
900MODULE_LICENSE("GPL");
901
902