blob: c68ec404474c93a926c6cd7d56631ca01da55424 [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>
Linus Walleij33d78642011-06-09 11:08:47 +02007 * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010017#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010018#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010019#include <linux/clk.h>
20#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010021#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
Lee Jonesa60b57e2012-04-19 21:36:31 +010025#include <linux/irqdomain.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Lee Jones855f80c2012-05-26 06:09:29 +010027#include <linux/of_device.h>
Linus Walleije98ea772012-04-26 23:57:25 +020028#include <linux/pinctrl/pinctrl.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020029#include <linux/pinctrl/pinmux.h>
Linus Walleijd41af622012-05-03 15:58:12 +020030#include <linux/pinctrl/pinconf.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020031/* Since we request GPIOs from ourself */
32#include <linux/pinctrl/consumer.h>
Linus Walleijbb16bd92012-10-10 14:27:58 +020033#include <linux/platform_data/pinctrl-nomadik.h>
Will Deaconadfed152011-02-28 10:12:29 +000034#include <asm/mach/irq.h>
Linus Walleijc3b9d1d2012-10-18 11:08:05 +020035#include <mach/irqs.h>
Linus Walleije98ea772012-04-26 23:57:25 +020036#include "pinctrl-nomadik.h"
37
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010038/*
39 * The GPIO module in the Nomadik family of Systems-on-Chip is an
40 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020041 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010042 *
43 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
44 */
45
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010046struct nmk_gpio_chip {
47 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010048 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010049 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010050 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053051 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010052 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053053 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053054 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053055 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010056 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020057 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010058 /* Keep track of configured edges */
59 u32 edge_rising;
60 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053061 u32 real_wake;
62 u32 rwimsc;
63 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053064 u32 rimsc;
65 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020066 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053067 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010068};
69
Jonas Aabergf1671bf2012-10-25 08:40:42 +020070/**
71 * struct nmk_pinctrl - state container for the Nomadik pin controller
72 * @dev: containing device pointer
73 * @pctl: corresponding pin controller device
74 * @soc: SoC data for this specific chip
75 * @prcm_base: PRCM register range virtual base
76 */
Linus Walleije98ea772012-04-26 23:57:25 +020077struct nmk_pinctrl {
78 struct device *dev;
79 struct pinctrl_dev *pctl;
80 const struct nmk_pinctrl_soc_data *soc;
Jonas Aabergf1671bf2012-10-25 08:40:42 +020081 void __iomem *prcm_base;
Linus Walleije98ea772012-04-26 23:57:25 +020082};
83
Rabin Vincent01727e62010-12-13 12:02:40 +053084static struct nmk_gpio_chip *
85nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
86
87static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
88
89#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
90
Rabin Vincent6f9a9742010-06-02 05:50:28 +010091static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
92 unsigned offset, int gpio_mode)
93{
94 u32 bit = 1 << offset;
95 u32 afunc, bfunc;
96
97 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
98 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
99 if (gpio_mode & NMK_GPIO_ALT_A)
100 afunc |= bit;
101 if (gpio_mode & NMK_GPIO_ALT_B)
102 bfunc |= bit;
103 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
104 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
105}
106
Rabin Vincent81a3c292010-05-27 12:39:23 +0100107static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
108 unsigned offset, enum nmk_gpio_slpm mode)
109{
110 u32 bit = 1 << offset;
111 u32 slpm;
112
113 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
114 if (mode == NMK_GPIO_SLPM_NOCHANGE)
115 slpm |= bit;
116 else
117 slpm &= ~bit;
118 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
119}
120
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100121static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
122 unsigned offset, enum nmk_gpio_pull pull)
123{
124 u32 bit = 1 << offset;
125 u32 pdis;
126
127 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200128 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100129 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200130 nmk_chip->pull_up &= ~bit;
131 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100132 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200133 }
134
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100135 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
136
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200137 if (pull == NMK_GPIO_PULL_UP) {
138 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100139 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200140 } else if (pull == NMK_GPIO_PULL_DOWN) {
141 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100142 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200143 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100144}
145
Rabin Vincentebc61782011-09-28 15:49:11 +0530146static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
147 unsigned offset, bool lowemi)
148{
149 u32 bit = BIT(offset);
150 bool enabled = nmk_chip->lowemi & bit;
151
152 if (lowemi == enabled)
153 return;
154
155 if (lowemi)
156 nmk_chip->lowemi |= bit;
157 else
158 nmk_chip->lowemi &= ~bit;
159
160 writel_relaxed(nmk_chip->lowemi,
161 nmk_chip->addr + NMK_GPIO_LOWEMI);
162}
163
Rabin Vincent378be062010-06-02 06:06:29 +0100164static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
165 unsigned offset)
166{
167 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
168}
169
Rabin Vincent6720db72010-09-02 11:28:48 +0100170static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
171 unsigned offset, int val)
172{
173 if (val)
174 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
175 else
176 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
177}
178
179static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
180 unsigned offset, int val)
181{
182 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
183 __nmk_gpio_set_output(nmk_chip, offset, val);
184}
185
Rabin Vincent01727e62010-12-13 12:02:40 +0530186static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
187 unsigned offset, int gpio_mode,
188 bool glitch)
189{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530190 u32 rwimsc = nmk_chip->rwimsc;
191 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530192
193 if (glitch && nmk_chip->set_ioforce) {
194 u32 bit = BIT(offset);
195
Rabin Vincent01727e62010-12-13 12:02:40 +0530196 /* Prevent spurious wakeups */
197 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
198 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
199
200 nmk_chip->set_ioforce(true);
201 }
202
203 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
204
205 if (glitch && nmk_chip->set_ioforce) {
206 nmk_chip->set_ioforce(false);
207
208 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
209 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
210 }
211}
212
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530213static void
214nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
215{
216 u32 falling = nmk_chip->fimsc & BIT(offset);
217 u32 rising = nmk_chip->rimsc & BIT(offset);
218 int gpio = nmk_chip->chip.base + offset;
219 int irq = NOMADIK_GPIO_TO_IRQ(gpio);
220 struct irq_data *d = irq_get_irq_data(irq);
221
222 if (!rising && !falling)
223 return;
224
225 if (!d || !irqd_irq_disabled(d))
226 return;
227
228 if (rising) {
229 nmk_chip->rimsc &= ~BIT(offset);
230 writel_relaxed(nmk_chip->rimsc,
231 nmk_chip->addr + NMK_GPIO_RIMSC);
232 }
233
234 if (falling) {
235 nmk_chip->fimsc &= ~BIT(offset);
236 writel_relaxed(nmk_chip->fimsc,
237 nmk_chip->addr + NMK_GPIO_FIMSC);
238 }
239
240 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
241}
242
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200243static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
244{
245 u32 val;
246
247 val = readl(reg);
248 val = ((val & ~mask) | (value & mask));
249 writel(val, reg);
250}
251
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200252static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
253 unsigned offset, unsigned alt_num)
254{
255 int i;
256 u16 reg;
257 u8 bit;
258 u8 alt_index;
259 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
260 const u16 *gpiocr_regs;
261
262 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
263 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
264 alt_num);
265 return;
266 }
267
268 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
269 if (npct->soc->altcx_pins[i].pin == offset)
270 break;
271 }
272 if (i == npct->soc->npins_altcx) {
273 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
274 offset);
275 return;
276 }
277
278 pin_desc = npct->soc->altcx_pins + i;
279 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
280
281 /*
282 * If alt_num is NULL, just clear current ALTCx selection
283 * to make sure we come back to a pure ALTC selection
284 */
285 if (!alt_num) {
286 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
287 if (pin_desc->altcx[i].used == true) {
288 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
289 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200290 if (readl(npct->prcm_base + reg) & BIT(bit)) {
291 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200292 dev_dbg(npct->dev,
293 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
294 offset, i+1);
295 }
296 }
297 }
298 return;
299 }
300
301 alt_index = alt_num - 1;
302 if (pin_desc->altcx[alt_index].used == false) {
303 dev_warn(npct->dev,
304 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
305 offset, alt_num);
306 return;
307 }
308
309 /*
310 * Check if any other ALTCx functions are activated on this pin
311 * and disable it first.
312 */
313 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
314 if (i == alt_index)
315 continue;
316 if (pin_desc->altcx[i].used == true) {
317 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
318 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200319 if (readl(npct->prcm_base + reg) & BIT(bit)) {
320 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200321 dev_dbg(npct->dev,
322 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
323 offset, i+1);
324 }
325 }
326 }
327
328 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
329 bit = pin_desc->altcx[alt_index].control_bit;
330 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
331 offset, alt_index+1);
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200332 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200333}
334
Rabin Vincent378be062010-06-02 06:06:29 +0100335static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
Rabin Vincent01727e62010-12-13 12:02:40 +0530336 pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
Rabin Vincent378be062010-06-02 06:06:29 +0100337{
338 static const char *afnames[] = {
339 [NMK_GPIO_ALT_GPIO] = "GPIO",
340 [NMK_GPIO_ALT_A] = "A",
341 [NMK_GPIO_ALT_B] = "B",
342 [NMK_GPIO_ALT_C] = "C"
343 };
344 static const char *pullnames[] = {
345 [NMK_GPIO_PULL_NONE] = "none",
346 [NMK_GPIO_PULL_UP] = "up",
347 [NMK_GPIO_PULL_DOWN] = "down",
348 [3] /* illegal */ = "??"
349 };
350 static const char *slpmnames[] = {
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100351 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
352 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
Rabin Vincent378be062010-06-02 06:06:29 +0100353 };
354
355 int pin = PIN_NUM(cfg);
356 int pull = PIN_PULL(cfg);
357 int af = PIN_ALT(cfg);
358 int slpm = PIN_SLPM(cfg);
Rabin Vincent6720db72010-09-02 11:28:48 +0100359 int output = PIN_DIR(cfg);
360 int val = PIN_VAL(cfg);
Rabin Vincent01727e62010-12-13 12:02:40 +0530361 bool glitch = af == NMK_GPIO_ALT_C;
Rabin Vincent378be062010-06-02 06:06:29 +0100362
Rabin Vincentdacdc962010-12-03 20:35:37 +0530363 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
364 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
Rabin Vincent6720db72010-09-02 11:28:48 +0100365 output ? "output " : "input",
366 output ? (val ? "high" : "low") : "");
Rabin Vincent378be062010-06-02 06:06:29 +0100367
Rabin Vincentdacdc962010-12-03 20:35:37 +0530368 if (sleep) {
369 int slpm_pull = PIN_SLPM_PULL(cfg);
370 int slpm_output = PIN_SLPM_DIR(cfg);
371 int slpm_val = PIN_SLPM_VAL(cfg);
372
Rabin Vincent3546d152010-11-25 11:38:27 +0530373 af = NMK_GPIO_ALT_GPIO;
374
Rabin Vincentdacdc962010-12-03 20:35:37 +0530375 /*
376 * The SLPM_* values are normal values + 1 to allow zero to
377 * mean "same as normal".
378 */
379 if (slpm_pull)
380 pull = slpm_pull - 1;
381 if (slpm_output)
382 output = slpm_output - 1;
383 if (slpm_val)
384 val = slpm_val - 1;
385
386 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
387 pin,
388 slpm_pull ? pullnames[pull] : "same",
389 slpm_output ? (output ? "output" : "input") : "same",
390 slpm_val ? (val ? "high" : "low") : "same");
391 }
392
Rabin Vincent6720db72010-09-02 11:28:48 +0100393 if (output)
394 __nmk_gpio_make_output(nmk_chip, offset, val);
395 else {
396 __nmk_gpio_make_input(nmk_chip, offset);
397 __nmk_gpio_set_pull(nmk_chip, offset, pull);
398 }
399
Rabin Vincentebc61782011-09-28 15:49:11 +0530400 __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
401
Rabin Vincent01727e62010-12-13 12:02:40 +0530402 /*
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530403 * If the pin is switching to altfunc, and there was an interrupt
404 * installed on it which has been lazy disabled, actually mask the
405 * interrupt to prevent spurious interrupts that would occur while the
406 * pin is under control of the peripheral. Only SKE does this.
407 */
408 if (af != NMK_GPIO_ALT_GPIO)
409 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
410
411 /*
Rabin Vincent01727e62010-12-13 12:02:40 +0530412 * If we've backed up the SLPM registers (glitch workaround), modify
413 * the backups since they will be restored.
414 */
415 if (slpmregs) {
416 if (slpm == NMK_GPIO_SLPM_NOCHANGE)
417 slpmregs[nmk_chip->bank] |= BIT(offset);
418 else
419 slpmregs[nmk_chip->bank] &= ~BIT(offset);
420 } else
421 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
422
423 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
424}
425
426/*
427 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
428 * - Save SLPM registers
429 * - Set SLPM=0 for the IOs you want to switch and others to 1
430 * - Configure the GPIO registers for the IOs that are being switched
431 * - Set IOFORCE=1
432 * - Modify the AFLSA/B registers for the IOs that are being switched
433 * - Set IOFORCE=0
434 * - Restore SLPM registers
435 * - Any spurious wake up event during switch sequence to be ignored and
436 * cleared
437 */
438static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
439{
440 int i;
441
442 for (i = 0; i < NUM_BANKS; i++) {
443 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
444 unsigned int temp = slpm[i];
445
446 if (!chip)
447 break;
448
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200449 clk_enable(chip->clk);
450
Rabin Vincent01727e62010-12-13 12:02:40 +0530451 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
452 writel(temp, chip->addr + NMK_GPIO_SLPC);
453 }
454}
455
456static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
457{
458 int i;
459
460 for (i = 0; i < NUM_BANKS; i++) {
461 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
462
463 if (!chip)
464 break;
465
466 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200467
468 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530469 }
470}
471
472static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
473{
474 static unsigned int slpm[NUM_BANKS];
475 unsigned long flags;
476 bool glitch = false;
477 int ret = 0;
478 int i;
479
480 for (i = 0; i < num; i++) {
481 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
482 glitch = true;
483 break;
484 }
485 }
486
487 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
488
489 if (glitch) {
490 memset(slpm, 0xff, sizeof(slpm));
491
492 for (i = 0; i < num; i++) {
493 int pin = PIN_NUM(cfgs[i]);
494 int offset = pin % NMK_GPIO_PER_CHIP;
495
496 if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
497 slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
498 }
499
500 nmk_gpio_glitch_slpm_init(slpm);
501 }
502
503 for (i = 0; i < num; i++) {
504 struct nmk_gpio_chip *nmk_chip;
505 int pin = PIN_NUM(cfgs[i]);
506
Lee Jonesa60b57e2012-04-19 21:36:31 +0100507 nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
Rabin Vincent01727e62010-12-13 12:02:40 +0530508 if (!nmk_chip) {
509 ret = -EINVAL;
510 break;
511 }
512
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200513 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530514 spin_lock(&nmk_chip->lock);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100515 __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
Rabin Vincent01727e62010-12-13 12:02:40 +0530516 cfgs[i], sleep, glitch ? slpm : NULL);
517 spin_unlock(&nmk_chip->lock);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200518 clk_disable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530519 }
520
521 if (glitch)
522 nmk_gpio_glitch_slpm_restore(slpm);
523
524 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
525
526 return ret;
Rabin Vincent378be062010-06-02 06:06:29 +0100527}
528
529/**
530 * nmk_config_pin - configure a pin's mux attributes
531 * @cfg: pin confguration
Linus Walleij50bcd472012-07-04 11:25:36 +0200532 * @sleep: Non-zero to apply the sleep mode configuration
Rabin Vincent378be062010-06-02 06:06:29 +0100533 * Configures a pin's mode (alternate function or GPIO), its pull up status,
534 * and its sleep mode based on the specified configuration. The @cfg is
535 * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
536 * are constructed using, and can be further enhanced with, the macros in
Linus Walleij287f1212012-10-10 14:35:17 +0200537 * <linux/platform_data/pinctrl-nomadik.h>
Rabin Vincent378be062010-06-02 06:06:29 +0100538 *
539 * If a pin's mode is set to GPIO, it is configured as an input to avoid
540 * side-effects. The gpio can be manipulated later using standard GPIO API
541 * calls.
542 */
Rabin Vincentdacdc962010-12-03 20:35:37 +0530543int nmk_config_pin(pin_cfg_t cfg, bool sleep)
Rabin Vincent378be062010-06-02 06:06:29 +0100544{
Rabin Vincent01727e62010-12-13 12:02:40 +0530545 return __nmk_config_pins(&cfg, 1, sleep);
Rabin Vincent378be062010-06-02 06:06:29 +0100546}
547EXPORT_SYMBOL(nmk_config_pin);
548
549/**
550 * nmk_config_pins - configure several pins at once
551 * @cfgs: array of pin configurations
552 * @num: number of elments in the array
553 *
554 * Configures several pins using nmk_config_pin(). Refer to that function for
555 * further information.
556 */
557int nmk_config_pins(pin_cfg_t *cfgs, int num)
558{
Rabin Vincent01727e62010-12-13 12:02:40 +0530559 return __nmk_config_pins(cfgs, num, false);
Rabin Vincent378be062010-06-02 06:06:29 +0100560}
561EXPORT_SYMBOL(nmk_config_pins);
562
Rabin Vincentdacdc962010-12-03 20:35:37 +0530563int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
564{
Rabin Vincent01727e62010-12-13 12:02:40 +0530565 return __nmk_config_pins(cfgs, num, true);
Rabin Vincentdacdc962010-12-03 20:35:37 +0530566}
567EXPORT_SYMBOL(nmk_config_pins_sleep);
568
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100569/**
Rabin Vincent81a3c292010-05-27 12:39:23 +0100570 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
571 * @gpio: pin number
572 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
573 *
Linus Walleij33d78642011-06-09 11:08:47 +0200574 * This register is actually in the pinmux layer, not the GPIO block itself.
575 * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
576 * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
577 * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
578 * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
579 * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
580 * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100581 *
Linus Walleij33d78642011-06-09 11:08:47 +0200582 * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
583 * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
584 * entered) regardless of the altfunction selected. Also wake-up detection is
585 * ENABLED.
586 *
587 * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
588 * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
589 * (for altfunction GPIO) or respective on-chip peripherals (for other
590 * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
591 *
592 * Note that enable_irq_wake() will automatically enable wakeup detection.
Rabin Vincent81a3c292010-05-27 12:39:23 +0100593 */
594int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
595{
596 struct nmk_gpio_chip *nmk_chip;
597 unsigned long flags;
598
Lee Jonesa60b57e2012-04-19 21:36:31 +0100599 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent81a3c292010-05-27 12:39:23 +0100600 if (!nmk_chip)
601 return -EINVAL;
602
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200603 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530604 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
605 spin_lock(&nmk_chip->lock);
606
Lee Jonesa60b57e2012-04-19 21:36:31 +0100607 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
Rabin Vincent01727e62010-12-13 12:02:40 +0530608
609 spin_unlock(&nmk_chip->lock);
610 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200611 clk_disable(nmk_chip->clk);
Rabin Vincent81a3c292010-05-27 12:39:23 +0100612
613 return 0;
614}
615
616/**
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100617 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
618 * @gpio: pin number
619 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
620 *
621 * Enables/disables pull up/down on a specified pin. This only takes effect if
622 * the pin is configured as an input (either explicitly or by the alternate
623 * function).
624 *
625 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
626 * configured as an input. Otherwise, due to the way the controller registers
627 * work, this function will change the value output on the pin.
628 */
629int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
630{
631 struct nmk_gpio_chip *nmk_chip;
632 unsigned long flags;
633
Lee Jonesa60b57e2012-04-19 21:36:31 +0100634 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100635 if (!nmk_chip)
636 return -EINVAL;
637
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200638 clk_enable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100639 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100640 __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100641 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200642 clk_disable(nmk_chip->clk);
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100643
644 return 0;
645}
646
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100647/* Mode functions */
Jonas Aaberg9c66ee62010-10-13 13:14:17 +0200648/**
649 * nmk_gpio_set_mode() - set the mux mode of a gpio pin
650 * @gpio: pin number
651 * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
652 * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
653 *
654 * Sets the mode of the specified pin to one of the alternate functions or
655 * plain GPIO.
656 */
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100657int nmk_gpio_set_mode(int gpio, int gpio_mode)
658{
659 struct nmk_gpio_chip *nmk_chip;
660 unsigned long flags;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100661
Lee Jonesa60b57e2012-04-19 21:36:31 +0100662 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100663 if (!nmk_chip)
664 return -EINVAL;
665
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200666 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667 spin_lock_irqsave(&nmk_chip->lock, flags);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100668 __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100669 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200670 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100671
672 return 0;
673}
674EXPORT_SYMBOL(nmk_gpio_set_mode);
675
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200676static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
677{
678 int i;
679 u16 reg;
680 u8 bit;
681 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
682 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
683 const u16 *gpiocr_regs;
684
685 for (i = 0; i < npct->soc->npins_altcx; i++) {
686 if (npct->soc->altcx_pins[i].pin == gpio)
687 break;
688 }
689 if (i == npct->soc->npins_altcx)
690 return NMK_GPIO_ALT_C;
691
692 pin_desc = npct->soc->altcx_pins + i;
693 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
694 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
695 if (pin_desc->altcx[i].used == true) {
696 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
697 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200698 if (readl(npct->prcm_base + reg) & BIT(bit))
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200699 return NMK_GPIO_ALT_C+i+1;
700 }
701 }
702 return NMK_GPIO_ALT_C;
703}
704
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100705int nmk_gpio_get_mode(int gpio)
706{
707 struct nmk_gpio_chip *nmk_chip;
708 u32 afunc, bfunc, bit;
709
Lee Jonesa60b57e2012-04-19 21:36:31 +0100710 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100711 if (!nmk_chip)
712 return -EINVAL;
713
Lee Jonesa60b57e2012-04-19 21:36:31 +0100714 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100715
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200716 clk_enable(nmk_chip->clk);
717
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100718 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
719 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
720
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200721 clk_disable(nmk_chip->clk);
722
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100723 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
724}
725EXPORT_SYMBOL(nmk_gpio_get_mode);
726
727
728/* IRQ functions */
729static inline int nmk_gpio_get_bitmask(int gpio)
730{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100731 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100732}
733
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100734static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100735{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100736 struct nmk_gpio_chip *nmk_chip;
737
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100738 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100739 if (!nmk_chip)
740 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200741
742 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100743 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200744 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100745}
746
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100747enum nmk_gpio_irq_type {
748 NORMAL,
749 WAKE,
750};
751
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100752static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100753 int gpio, enum nmk_gpio_irq_type which,
754 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100755{
756 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530757 u32 *rimscval;
758 u32 *fimscval;
759 u32 rimscreg;
760 u32 fimscreg;
761
762 if (which == NORMAL) {
763 rimscreg = NMK_GPIO_RIMSC;
764 fimscreg = NMK_GPIO_FIMSC;
765 rimscval = &nmk_chip->rimsc;
766 fimscval = &nmk_chip->fimsc;
767 } else {
768 rimscreg = NMK_GPIO_RWIMSC;
769 fimscreg = NMK_GPIO_FWIMSC;
770 rimscval = &nmk_chip->rwimsc;
771 fimscval = &nmk_chip->fwimsc;
772 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100773
774 /* we must individually set/clear the two edges */
775 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100776 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530777 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100778 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530779 *rimscval &= ~bitmask;
780 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100781 }
782 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100783 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530784 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100785 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530786 *fimscval &= ~bitmask;
787 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100788 }
789}
790
Rabin Vincentb9df4682011-02-10 11:45:58 +0530791static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
792 int gpio, bool on)
793{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530794 /*
795 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
796 * disabled, since setting SLPM to 1 increases power consumption, and
797 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
798 */
799 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200800 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530801 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200802 }
803
Rabin Vincentb9df4682011-02-10 11:45:58 +0530804 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
805}
806
807static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100808{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100809 struct nmk_gpio_chip *nmk_chip;
810 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100811 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100812
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100813 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100814 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100815 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100816 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100817
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200818 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530819 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
820 spin_lock(&nmk_chip->lock);
821
Lee Jonesa60b57e2012-04-19 21:36:31 +0100822 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530823
824 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100825 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530826
827 spin_unlock(&nmk_chip->lock);
828 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200829 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100830
831 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100832}
833
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100834static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100835{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530836 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100837}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100838
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100839static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100840{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530841 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100842}
843
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100844static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100845{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100846 struct nmk_gpio_chip *nmk_chip;
847 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530848 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100849
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100850 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100851 if (!nmk_chip)
852 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100853 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100854
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200855 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530856 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
857 spin_lock(&nmk_chip->lock);
858
Linus Walleij479a0c72011-09-20 10:50:15 +0200859 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100860 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530861
862 if (on)
863 nmk_chip->real_wake |= bitmask;
864 else
865 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530866
867 spin_unlock(&nmk_chip->lock);
868 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200869 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100870
871 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100872}
873
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100874static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100875{
Linus Walleij479a0c72011-09-20 10:50:15 +0200876 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200877 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100878 struct nmk_gpio_chip *nmk_chip;
879 unsigned long flags;
880 u32 bitmask;
881
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100882 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100883 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100884 if (!nmk_chip)
885 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100886 if (type & IRQ_TYPE_LEVEL_HIGH)
887 return -EINVAL;
888 if (type & IRQ_TYPE_LEVEL_LOW)
889 return -EINVAL;
890
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200891 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100892 spin_lock_irqsave(&nmk_chip->lock, flags);
893
Rabin Vincent7a852d82010-05-06 10:43:55 +0100894 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100895 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100896
Rabin Vincentb9df4682011-02-10 11:45:58 +0530897 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100898 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100899
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100900 nmk_chip->edge_rising &= ~bitmask;
901 if (type & IRQ_TYPE_EDGE_RISING)
902 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100903
904 nmk_chip->edge_falling &= ~bitmask;
905 if (type & IRQ_TYPE_EDGE_FALLING)
906 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100907
908 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100909 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100910
Rabin Vincentb9df4682011-02-10 11:45:58 +0530911 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100912 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100913
914 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200915 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100916
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100917 return 0;
918}
919
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200920static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
921{
922 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
923
924 clk_enable(nmk_chip->clk);
925 nmk_gpio_irq_unmask(d);
926 return 0;
927}
928
929static void nmk_gpio_irq_shutdown(struct irq_data *d)
930{
931 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
932
933 nmk_gpio_irq_mask(d);
934 clk_disable(nmk_chip->clk);
935}
936
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100937static struct irq_chip nmk_gpio_irq_chip = {
938 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100939 .irq_ack = nmk_gpio_irq_ack,
940 .irq_mask = nmk_gpio_irq_mask,
941 .irq_unmask = nmk_gpio_irq_unmask,
942 .irq_set_type = nmk_gpio_irq_set_type,
943 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200944 .irq_startup = nmk_gpio_irq_startup,
945 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200946 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100947};
948
Rabin Vincent33b744b2010-10-14 10:38:03 +0530949static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
950 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100951{
952 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100953 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100954
Will Deaconadfed152011-02-28 10:12:29 +0000955 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100956
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100957 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530958 while (status) {
959 int bit = __ffs(status);
960
Linus Walleij95f0bc92012-09-27 14:14:09 +0200961 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530962 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100963 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100964
Will Deaconadfed152011-02-28 10:12:29 +0000965 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100966}
967
Rabin Vincent33b744b2010-10-14 10:38:03 +0530968static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
969{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100970 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200971 u32 status;
972
973 clk_enable(nmk_chip->clk);
974 status = readl(nmk_chip->addr + NMK_GPIO_IS);
975 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530976
977 __nmk_gpio_irq_handler(irq, desc, status);
978}
979
980static void nmk_gpio_secondary_irq_handler(unsigned int irq,
981 struct irq_desc *desc)
982{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100983 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530984 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
985
986 __nmk_gpio_irq_handler(irq, desc, status);
987}
988
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100989static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
990{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100991 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
992 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530993
994 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100995 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530996 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100997 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530998 }
999
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001000 return 0;
1001}
1002
1003/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001004
1005static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
1006{
1007 /*
1008 * Map back to global GPIO space and request muxing, the direction
1009 * parameter does not matter for this controller.
1010 */
1011 int gpio = chip->base + offset;
1012
1013 return pinctrl_request_gpio(gpio);
1014}
1015
1016static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
1017{
1018 int gpio = chip->base + offset;
1019
1020 pinctrl_free_gpio(gpio);
1021}
1022
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001023static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
1024{
1025 struct nmk_gpio_chip *nmk_chip =
1026 container_of(chip, struct nmk_gpio_chip, chip);
1027
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001028 clk_enable(nmk_chip->clk);
1029
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001030 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001031
1032 clk_disable(nmk_chip->clk);
1033
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001034 return 0;
1035}
1036
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001037static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
1038{
1039 struct nmk_gpio_chip *nmk_chip =
1040 container_of(chip, struct nmk_gpio_chip, chip);
1041 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001042 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001043
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001044 clk_enable(nmk_chip->clk);
1045
1046 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
1047
1048 clk_disable(nmk_chip->clk);
1049
1050 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001051}
1052
1053static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
1054 int val)
1055{
1056 struct nmk_gpio_chip *nmk_chip =
1057 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001058
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001059 clk_enable(nmk_chip->clk);
1060
Rabin Vincent6720db72010-09-02 11:28:48 +01001061 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001062
1063 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001064}
1065
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001066static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
1067 int val)
1068{
1069 struct nmk_gpio_chip *nmk_chip =
1070 container_of(chip, struct nmk_gpio_chip, chip);
1071
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001072 clk_enable(nmk_chip->clk);
1073
Rabin Vincent6720db72010-09-02 11:28:48 +01001074 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001075
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001076 clk_disable(nmk_chip->clk);
1077
Rabin Vincent6647c6c2010-05-27 12:22:42 +01001078 return 0;
1079}
1080
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001081static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1082{
1083 struct nmk_gpio_chip *nmk_chip =
1084 container_of(chip, struct nmk_gpio_chip, chip);
1085
Linus Walleij268300b2012-10-19 17:06:54 +02001086 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001087}
1088
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301089#ifdef CONFIG_DEBUG_FS
1090
1091#include <linux/seq_file.h>
1092
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001093static void nmk_gpio_dbg_show_one(struct seq_file *s,
1094 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
1095 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301096{
Linus Walleij6f4350a2012-05-02 21:06:13 +02001097 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301098 struct nmk_gpio_chip *nmk_chip =
1099 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001100 int mode;
1101 bool is_out;
1102 bool pull;
1103 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301104 const char *modes[] = {
1105 [NMK_GPIO_ALT_GPIO] = "gpio",
1106 [NMK_GPIO_ALT_A] = "altA",
1107 [NMK_GPIO_ALT_B] = "altB",
1108 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001109 [NMK_GPIO_ALT_C+1] = "altC1",
1110 [NMK_GPIO_ALT_C+2] = "altC2",
1111 [NMK_GPIO_ALT_C+3] = "altC3",
1112 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301113 };
1114
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001115 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001116 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
1117 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
1118 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001119 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1120 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001121
Linus Walleij6f4350a2012-05-02 21:06:13 +02001122 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
1123 gpio, label ?: "(none)",
1124 is_out ? "out" : "in ",
1125 chip->get
1126 ? (chip->get(chip, offset) ? "hi" : "lo")
1127 : "? ",
1128 (mode < 0) ? "unknown" : modes[mode],
1129 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301130
Linus Walleij6f4350a2012-05-02 21:06:13 +02001131 if (label && !is_out) {
1132 int irq = gpio_to_irq(gpio);
1133 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001134
Linus Walleij6f4350a2012-05-02 21:06:13 +02001135 /* This races with request_irq(), set_irq_type(),
1136 * and set_irq_wake() ... but those are "rare".
1137 */
1138 if (irq >= 0 && desc->action) {
1139 char *trigger;
1140 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001141
Linus Walleij6f4350a2012-05-02 21:06:13 +02001142 if (nmk_chip->edge_rising & bitmask)
1143 trigger = "edge-rising";
1144 else if (nmk_chip->edge_falling & bitmask)
1145 trigger = "edge-falling";
1146 else
1147 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001148
Linus Walleij6f4350a2012-05-02 21:06:13 +02001149 seq_printf(s, " irq-%d %s%s",
1150 irq, trigger,
1151 irqd_is_wakeup_set(&desc->irq_data)
1152 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001153 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301154 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001155 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301156}
1157
Linus Walleij6f4350a2012-05-02 21:06:13 +02001158static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1159{
1160 unsigned i;
1161 unsigned gpio = chip->base;
1162
1163 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001164 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001165 seq_printf(s, "\n");
1166 }
1167}
1168
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301169#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001170static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001171 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +02001172 struct gpio_chip *chip,
1173 unsigned offset, unsigned gpio)
1174{
1175}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301176#define nmk_gpio_dbg_show NULL
1177#endif
1178
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001179/* This structure is replicated for each GPIO block allocated at probe time */
1180static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001181 .request = nmk_gpio_request,
1182 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001183 .direction_input = nmk_gpio_make_input,
1184 .get = nmk_gpio_get_input,
1185 .direction_output = nmk_gpio_make_output,
1186 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +01001187 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301188 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001189 .can_sleep = 0,
1190};
1191
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001192void nmk_gpio_clocks_enable(void)
1193{
1194 int i;
1195
1196 for (i = 0; i < NUM_BANKS; i++) {
1197 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1198
1199 if (!chip)
1200 continue;
1201
1202 clk_enable(chip->clk);
1203 }
1204}
1205
1206void nmk_gpio_clocks_disable(void)
1207{
1208 int i;
1209
1210 for (i = 0; i < NUM_BANKS; i++) {
1211 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1212
1213 if (!chip)
1214 continue;
1215
1216 clk_disable(chip->clk);
1217 }
1218}
1219
Rabin Vincentb9df4682011-02-10 11:45:58 +05301220/*
1221 * Called from the suspend/resume path to only keep the real wakeup interrupts
1222 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1223 * and not the rest of the interrupts which we needed to have as wakeups for
1224 * cpuidle.
1225 *
1226 * PM ops are not used since this needs to be done at the end, after all the
1227 * other drivers are done with their suspend callbacks.
1228 */
1229void nmk_gpio_wakeups_suspend(void)
1230{
1231 int i;
1232
1233 for (i = 0; i < NUM_BANKS; i++) {
1234 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1235
1236 if (!chip)
1237 break;
1238
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001239 clk_enable(chip->clk);
1240
Rabin Vincentb9df4682011-02-10 11:45:58 +05301241 writel(chip->rwimsc & chip->real_wake,
1242 chip->addr + NMK_GPIO_RWIMSC);
1243 writel(chip->fwimsc & chip->real_wake,
1244 chip->addr + NMK_GPIO_FWIMSC);
1245
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001246 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301247 }
1248}
1249
1250void nmk_gpio_wakeups_resume(void)
1251{
1252 int i;
1253
1254 for (i = 0; i < NUM_BANKS; i++) {
1255 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1256
1257 if (!chip)
1258 break;
1259
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001260 clk_enable(chip->clk);
1261
Rabin Vincentb9df4682011-02-10 11:45:58 +05301262 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1263 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1264
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001265 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301266 }
1267}
1268
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001269/*
1270 * Read the pull up/pull down status.
1271 * A bit set in 'pull_up' means that pull up
1272 * is selected if pull is enabled in PDIS register.
1273 * Note: only pull up/down set via this driver can
1274 * be detected due to HW limitations.
1275 */
1276void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1277{
1278 if (gpio_bank < NUM_BANKS) {
1279 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1280
1281 if (!chip)
1282 return;
1283
1284 *pull_up = chip->pull_up;
1285 }
1286}
1287
Axel Lin5212d092012-11-16 00:01:35 +08001288static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1289 irq_hw_number_t hwirq)
Lee Jonesa60b57e2012-04-19 21:36:31 +01001290{
1291 struct nmk_gpio_chip *nmk_chip = d->host_data;
1292
1293 if (!nmk_chip)
1294 return -EINVAL;
1295
1296 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1297 set_irq_flags(irq, IRQF_VALID);
1298 irq_set_chip_data(irq, nmk_chip);
1299 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1300
1301 return 0;
1302}
1303
1304const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
1305 .map = nmk_gpio_irq_map,
1306 .xlate = irq_domain_xlate_twocell,
1307};
1308
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001309static int nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001310{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001311 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001312 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001313 struct nmk_gpio_chip *nmk_chip;
1314 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001315 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001316 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301317 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001318 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001319 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001320 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001321 int ret;
1322
Lee Jones513c27f2012-04-13 15:05:05 +01001323 if (!pdata && !np) {
1324 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001325 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001326 }
1327
1328 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001329 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001330 if (!pdata)
1331 return -ENOMEM;
1332
Lee Jones612e1d52012-06-14 11:27:56 +01001333 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001334 pdata->supports_sleepmode = true;
1335
1336 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1337 dev_err(&dev->dev, "gpio-bank property not found\n");
1338 ret = -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001339 goto out;
Lee Jones513c27f2012-04-13 15:05:05 +01001340 }
1341
1342 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1343 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1344 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001345
1346 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1347 if (!res) {
1348 ret = -ENOENT;
1349 goto out;
1350 }
1351
1352 irq = platform_get_irq(dev, 0);
1353 if (irq < 0) {
1354 ret = irq;
1355 goto out;
1356 }
1357
Rabin Vincent33b744b2010-10-14 10:38:03 +05301358 secondary_irq = platform_get_irq(dev, 1);
1359 if (secondary_irq >= 0 && !pdata->get_secondary_status) {
1360 ret = -EINVAL;
1361 goto out;
1362 }
1363
Linus Walleij5e754f32012-07-03 23:05:14 +02001364 base = devm_request_and_ioremap(&dev->dev, res);
1365 if (!base) {
1366 ret = -ENOMEM;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001367 goto out;
1368 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001369
Linus Walleij5e754f32012-07-03 23:05:14 +02001370 clk = devm_clk_get(&dev->dev, NULL);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001371 if (IS_ERR(clk)) {
1372 ret = PTR_ERR(clk);
Linus Walleij5e754f32012-07-03 23:05:14 +02001373 goto out;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001374 }
Linus Walleijefec3812012-06-06 22:50:41 +02001375 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001376
Linus Walleij5e754f32012-07-03 23:05:14 +02001377 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001378 if (!nmk_chip) {
1379 ret = -ENOMEM;
Linus Walleij5e754f32012-07-03 23:05:14 +02001380 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001381 }
Lee Jones513c27f2012-04-13 15:05:05 +01001382
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001383 /*
1384 * The virt address in nmk_chip->addr is in the nomadik register space,
1385 * so we can simply convert the resource address, without remapping
1386 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301387 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001388 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001389 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001390 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001391 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301392 nmk_chip->secondary_parent_irq = secondary_irq;
1393 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301394 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001395 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001396 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001397
1398 chip = &nmk_chip->chip;
1399 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301400 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301401 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001402 chip->dev = &dev->dev;
1403 chip->owner = THIS_MODULE;
1404
Rabin Vincentebc61782011-09-28 15:49:11 +05301405 clk_enable(nmk_chip->clk);
1406 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1407 clk_disable(nmk_chip->clk);
1408
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001409#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001410 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001411#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001412
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001413 ret = gpiochip_add(&nmk_chip->chip);
1414 if (ret)
Linus Walleij5e754f32012-07-03 23:05:14 +02001415 goto out;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001416
Rabin Vincent01727e62010-12-13 12:02:40 +05301417 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1418
1419 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001420
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001421 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001422
Linus Walleij51f58c62012-10-11 16:33:44 +02001423 if (!np)
Linus Walleij6054b9c2012-09-26 19:03:51 +02001424 irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
Linus Walleij38843e22012-10-23 11:44:42 +02001425 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001426 NMK_GPIO_PER_CHIP, irq_start,
1427 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001428 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001429 dev_err(&dev->dev, "failed to create irqdomain\n");
Lee Jonesa60b57e2012-04-19 21:36:31 +01001430 ret = -ENOSYS;
Linus Walleij5e754f32012-07-03 23:05:14 +02001431 goto out;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001432 }
1433
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001434 nmk_gpio_init_irq(nmk_chip);
1435
Lee Jones513c27f2012-04-13 15:05:05 +01001436 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1437
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001438 return 0;
1439
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001440out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001441 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
1442 pdata->first_gpio, pdata->first_gpio+31);
Lee Jones513c27f2012-04-13 15:05:05 +01001443
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001444 return ret;
1445}
1446
Linus Walleije98ea772012-04-26 23:57:25 +02001447static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1448{
1449 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1450
1451 return npct->soc->ngroups;
1452}
1453
1454static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1455 unsigned selector)
1456{
1457 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1458
1459 return npct->soc->groups[selector].name;
1460}
1461
1462static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1463 const unsigned **pins,
1464 unsigned *num_pins)
1465{
1466 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1467
1468 *pins = npct->soc->groups[selector].pins;
1469 *num_pins = npct->soc->groups[selector].npins;
1470 return 0;
1471}
1472
Linus Walleij24cbdd72012-05-02 21:28:00 +02001473static struct pinctrl_gpio_range *
1474nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1475{
1476 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1477 int i;
1478
1479 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1480 struct pinctrl_gpio_range *range;
1481
1482 range = &npct->soc->gpio_ranges[i];
1483 if (offset >= range->pin_base &&
1484 offset <= (range->pin_base + range->npins - 1))
1485 return range;
1486 }
1487 return NULL;
1488}
1489
Linus Walleije98ea772012-04-26 23:57:25 +02001490static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1491 unsigned offset)
1492{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001493 struct pinctrl_gpio_range *range;
1494 struct gpio_chip *chip;
1495
1496 range = nmk_match_gpio_range(pctldev, offset);
1497 if (!range || !range->gc) {
1498 seq_printf(s, "invalid pin offset");
1499 return;
1500 }
1501 chip = range->gc;
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001502 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001503}
1504
1505static struct pinctrl_ops nmk_pinctrl_ops = {
1506 .get_groups_count = nmk_get_groups_cnt,
1507 .get_group_name = nmk_get_group_name,
1508 .get_group_pins = nmk_get_group_pins,
1509 .pin_dbg_show = nmk_pin_dbg_show,
1510};
1511
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001512static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1513{
1514 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1515
1516 return npct->soc->nfunctions;
1517}
1518
1519static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1520 unsigned function)
1521{
1522 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1523
1524 return npct->soc->functions[function].name;
1525}
1526
1527static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1528 unsigned function,
1529 const char * const **groups,
1530 unsigned * const num_groups)
1531{
1532 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1533
1534 *groups = npct->soc->functions[function].groups;
1535 *num_groups = npct->soc->functions[function].ngroups;
1536
1537 return 0;
1538}
1539
1540static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1541 unsigned group)
1542{
1543 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1544 const struct nmk_pingroup *g;
1545 static unsigned int slpm[NUM_BANKS];
1546 unsigned long flags;
1547 bool glitch;
1548 int ret = -EINVAL;
1549 int i;
1550
1551 g = &npct->soc->groups[group];
1552
1553 if (g->altsetting < 0)
1554 return -EINVAL;
1555
1556 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1557
Linus Walleijdaf73172012-05-22 11:46:45 +02001558 /*
1559 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1560 * we may pass through an undesired state. In this case we take
1561 * some extra care.
1562 *
1563 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1564 * - Save SLPM registers (since we have a shadow register in the
1565 * nmk_chip we're using that as backup)
1566 * - Set SLPM=0 for the IOs you want to switch and others to 1
1567 * - Configure the GPIO registers for the IOs that are being switched
1568 * - Set IOFORCE=1
1569 * - Modify the AFLSA/B registers for the IOs that are being switched
1570 * - Set IOFORCE=0
1571 * - Restore SLPM registers
1572 * - Any spurious wake up event during switch sequence to be ignored
1573 * and cleared
1574 *
1575 * We REALLY need to save ALL slpm registers, because the external
1576 * IOFORCE will switch *all* ports to their sleepmode setting to as
1577 * to avoid glitches. (Not just one port!)
1578 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001579 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001580
1581 if (glitch) {
1582 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1583
1584 /* Initially don't put any pins to sleep when switching */
1585 memset(slpm, 0xff, sizeof(slpm));
1586
1587 /*
1588 * Then mask the pins that need to be sleeping now when we're
1589 * switching to the ALT C function.
1590 */
1591 for (i = 0; i < g->npins; i++)
1592 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1593 nmk_gpio_glitch_slpm_init(slpm);
1594 }
1595
1596 for (i = 0; i < g->npins; i++) {
1597 struct pinctrl_gpio_range *range;
1598 struct nmk_gpio_chip *nmk_chip;
1599 struct gpio_chip *chip;
1600 unsigned bit;
1601
1602 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1603 if (!range) {
1604 dev_err(npct->dev,
1605 "invalid pin offset %d in group %s at index %d\n",
1606 g->pins[i], g->name, i);
1607 goto out_glitch;
1608 }
1609 if (!range->gc) {
1610 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1611 g->pins[i], g->name, i);
1612 goto out_glitch;
1613 }
1614 chip = range->gc;
1615 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1616 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1617
1618 clk_enable(nmk_chip->clk);
1619 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1620 /*
1621 * If the pin is switching to altfunc, and there was an
1622 * interrupt installed on it which has been lazy disabled,
1623 * actually mask the interrupt to prevent spurious interrupts
1624 * that would occur while the pin is under control of the
1625 * peripheral. Only SKE does this.
1626 */
1627 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1628
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001629 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1630 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001631 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001632
1633 /*
1634 * Call PRCM GPIOCR config function in case ALTC
1635 * has been selected:
1636 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1637 * must be set.
1638 * - If selection is pure ALTC and previous selection was ALTCx,
1639 * then some bits in PRCM GPIOCR registers must be cleared.
1640 */
1641 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1642 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1643 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001644 }
1645
1646 /* When all pins are successfully reconfigured we get here */
1647 ret = 0;
1648
1649out_glitch:
1650 if (glitch) {
1651 nmk_gpio_glitch_slpm_restore(slpm);
1652 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1653 }
1654
1655 return ret;
1656}
1657
1658static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1659 unsigned function, unsigned group)
1660{
1661 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1662 const struct nmk_pingroup *g;
1663
1664 g = &npct->soc->groups[group];
1665
1666 if (g->altsetting < 0)
1667 return;
1668
1669 /* Poke out the mux, set the pin to some default state? */
1670 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1671}
1672
Axel Lin5212d092012-11-16 00:01:35 +08001673static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1674 struct pinctrl_gpio_range *range,
1675 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001676{
1677 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1678 struct nmk_gpio_chip *nmk_chip;
1679 struct gpio_chip *chip;
1680 unsigned bit;
1681
1682 if (!range) {
1683 dev_err(npct->dev, "invalid range\n");
1684 return -EINVAL;
1685 }
1686 if (!range->gc) {
1687 dev_err(npct->dev, "missing GPIO chip in range\n");
1688 return -EINVAL;
1689 }
1690 chip = range->gc;
1691 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1692
1693 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1694
1695 clk_enable(nmk_chip->clk);
1696 bit = offset % NMK_GPIO_PER_CHIP;
1697 /* There is no glitch when converting any pin to GPIO */
1698 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1699 clk_disable(nmk_chip->clk);
1700
1701 return 0;
1702}
1703
Axel Lin5212d092012-11-16 00:01:35 +08001704static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1705 struct pinctrl_gpio_range *range,
1706 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001707{
1708 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1709
1710 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1711 /* Set the pin to some default state, GPIO is usually default */
1712}
1713
1714static struct pinmux_ops nmk_pinmux_ops = {
1715 .get_functions_count = nmk_pmx_get_funcs_cnt,
1716 .get_function_name = nmk_pmx_get_func_name,
1717 .get_function_groups = nmk_pmx_get_func_groups,
1718 .enable = nmk_pmx_enable,
1719 .disable = nmk_pmx_disable,
1720 .gpio_request_enable = nmk_gpio_request_enable,
1721 .gpio_disable_free = nmk_gpio_disable_free,
1722};
1723
Axel Lin5212d092012-11-16 00:01:35 +08001724static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1725 unsigned long *config)
Linus Walleijd41af622012-05-03 15:58:12 +02001726{
1727 /* Not implemented */
1728 return -EINVAL;
1729}
1730
Axel Lin5212d092012-11-16 00:01:35 +08001731static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1732 unsigned long config)
Linus Walleijd41af622012-05-03 15:58:12 +02001733{
1734 static const char *pullnames[] = {
1735 [NMK_GPIO_PULL_NONE] = "none",
1736 [NMK_GPIO_PULL_UP] = "up",
1737 [NMK_GPIO_PULL_DOWN] = "down",
1738 [3] /* illegal */ = "??"
1739 };
1740 static const char *slpmnames[] = {
1741 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1742 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1743 };
1744 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1745 struct nmk_gpio_chip *nmk_chip;
1746 struct pinctrl_gpio_range *range;
1747 struct gpio_chip *chip;
1748 unsigned bit;
1749
1750 /*
1751 * The pin config contains pin number and altfunction fields, here
1752 * we just ignore that part. It's being handled by the framework and
1753 * pinmux callback respectively.
1754 */
1755 pin_cfg_t cfg = (pin_cfg_t) config;
1756 int pull = PIN_PULL(cfg);
1757 int slpm = PIN_SLPM(cfg);
1758 int output = PIN_DIR(cfg);
1759 int val = PIN_VAL(cfg);
1760 bool lowemi = PIN_LOWEMI(cfg);
1761 bool gpiomode = PIN_GPIOMODE(cfg);
1762 bool sleep = PIN_SLEEPMODE(cfg);
1763
1764 range = nmk_match_gpio_range(pctldev, pin);
1765 if (!range) {
1766 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1767 return -EINVAL;
1768 }
1769 if (!range->gc) {
1770 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1771 pin);
1772 return -EINVAL;
1773 }
1774 chip = range->gc;
1775 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1776
1777 if (sleep) {
1778 int slpm_pull = PIN_SLPM_PULL(cfg);
1779 int slpm_output = PIN_SLPM_DIR(cfg);
1780 int slpm_val = PIN_SLPM_VAL(cfg);
1781
1782 /* All pins go into GPIO mode at sleep */
1783 gpiomode = true;
1784
1785 /*
1786 * The SLPM_* values are normal values + 1 to allow zero to
1787 * mean "same as normal".
1788 */
1789 if (slpm_pull)
1790 pull = slpm_pull - 1;
1791 if (slpm_output)
1792 output = slpm_output - 1;
1793 if (slpm_val)
1794 val = slpm_val - 1;
1795
1796 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1797 pin,
1798 slpm_pull ? pullnames[pull] : "same",
1799 slpm_output ? (output ? "output" : "input") : "same",
1800 slpm_val ? (val ? "high" : "low") : "same");
1801 }
1802
1803 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1804 pin, cfg, pullnames[pull], slpmnames[slpm],
1805 output ? "output " : "input",
1806 output ? (val ? "high" : "low") : "",
1807 lowemi ? "on" : "off" );
1808
1809 clk_enable(nmk_chip->clk);
1810 bit = pin % NMK_GPIO_PER_CHIP;
1811 if (gpiomode)
1812 /* No glitch when going to GPIO mode */
1813 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1814 if (output)
1815 __nmk_gpio_make_output(nmk_chip, bit, val);
1816 else {
1817 __nmk_gpio_make_input(nmk_chip, bit);
1818 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1819 }
1820 /* TODO: isn't this only applicable on output pins? */
1821 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1822
1823 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1824 clk_disable(nmk_chip->clk);
1825 return 0;
1826}
1827
1828static struct pinconf_ops nmk_pinconf_ops = {
1829 .pin_config_get = nmk_pin_config_get,
1830 .pin_config_set = nmk_pin_config_set,
1831};
1832
Linus Walleije98ea772012-04-26 23:57:25 +02001833static struct pinctrl_desc nmk_pinctrl_desc = {
1834 .name = "pinctrl-nomadik",
1835 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001836 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001837 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001838 .owner = THIS_MODULE,
1839};
1840
Lee Jones855f80c2012-05-26 06:09:29 +01001841static const struct of_device_id nmk_pinctrl_match[] = {
1842 {
1843 .compatible = "stericsson,nmk_pinctrl",
1844 .data = (void *)PINCTRL_NMK_DB8500,
1845 },
1846 {},
1847};
1848
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001849static int nmk_pinctrl_probe(struct platform_device *pdev)
Linus Walleije98ea772012-04-26 23:57:25 +02001850{
1851 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001852 struct device_node *np = pdev->dev.of_node;
Linus Walleije98ea772012-04-26 23:57:25 +02001853 struct nmk_pinctrl *npct;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001854 struct resource *res;
Lee Jones855f80c2012-05-26 06:09:29 +01001855 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02001856 int i;
1857
1858 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
1859 if (!npct)
1860 return -ENOMEM;
1861
Lee Jones855f80c2012-05-26 06:09:29 +01001862 if (platid)
1863 version = platid->driver_data;
Axel Lin953e9e92012-11-15 12:56:05 +08001864 else if (np) {
1865 const struct of_device_id *match;
1866
1867 match = of_match_device(nmk_pinctrl_match, &pdev->dev);
1868 if (!match)
1869 return -ENODEV;
1870 version = (unsigned int) match->data;
1871 }
Lee Jones855f80c2012-05-26 06:09:29 +01001872
Linus Walleije98ea772012-04-26 23:57:25 +02001873 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02001874 if (version == PINCTRL_NMK_STN8815)
1875 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01001876 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02001877 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02001878 if (version == PINCTRL_NMK_DB8540)
1879 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02001880
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1882 if (res) {
1883 npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
1884 resource_size(res));
1885 if (!npct->prcm_base) {
1886 dev_err(&pdev->dev,
1887 "failed to ioremap PRCM registers\n");
1888 return -ENOMEM;
1889 }
1890 } else {
1891 dev_info(&pdev->dev,
1892 "No PRCM base, assume no ALT-Cx control is available\n");
1893 }
1894
Linus Walleije98ea772012-04-26 23:57:25 +02001895 /*
1896 * We need all the GPIO drivers to probe FIRST, or we will not be able
1897 * to obtain references to the struct gpio_chip * for them, and we
1898 * need this to proceed.
1899 */
1900 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001901 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001902 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001903 return -EPROBE_DEFER;
1904 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001905 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001906 }
1907
1908 nmk_pinctrl_desc.pins = npct->soc->pins;
1909 nmk_pinctrl_desc.npins = npct->soc->npins;
1910 npct->dev = &pdev->dev;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001911
Linus Walleije98ea772012-04-26 23:57:25 +02001912 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1913 if (!npct->pctl) {
1914 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1915 return -EINVAL;
1916 }
1917
1918 /* We will handle a range of GPIO pins */
1919 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1920 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1921
1922 platform_set_drvdata(pdev, npct);
1923 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1924
1925 return 0;
1926}
1927
Lee Jones513c27f2012-04-13 15:05:05 +01001928static const struct of_device_id nmk_gpio_match[] = {
1929 { .compatible = "st,nomadik-gpio", },
1930 {}
1931};
1932
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001933static struct platform_driver nmk_gpio_driver = {
1934 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001935 .owner = THIS_MODULE,
1936 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001937 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301938 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001939 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001940};
1941
Linus Walleije98ea772012-04-26 23:57:25 +02001942static const struct platform_device_id nmk_pinctrl_id[] = {
1943 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1944 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001945 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Axel Lin8c995d62012-11-04 23:30:42 +08001946 { }
Linus Walleije98ea772012-04-26 23:57:25 +02001947};
1948
1949static struct platform_driver nmk_pinctrl_driver = {
1950 .driver = {
1951 .owner = THIS_MODULE,
1952 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001953 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001954 },
1955 .probe = nmk_pinctrl_probe,
1956 .id_table = nmk_pinctrl_id,
1957};
1958
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001959static int __init nmk_gpio_init(void)
1960{
Linus Walleije98ea772012-04-26 23:57:25 +02001961 int ret;
1962
1963 ret = platform_driver_register(&nmk_gpio_driver);
1964 if (ret)
1965 return ret;
1966 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001967}
1968
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001969core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001970
1971MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1972MODULE_DESCRIPTION("Nomadik GPIO Driver");
1973MODULE_LICENSE("GPL");