blob: 181fa546b2987f5cb9cdebebc3b7f34707b5e851 [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 Walleijf4b3f522013-11-19 23:21:04 +01007 * Copyright (C) 2011-2013 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Lee Jones855f80c2012-05-26 06:09:29 +010025#include <linux/of_device.h>
Lee Jones32e67ee2013-01-11 15:45:29 +000026#include <linux/of_address.h>
Gabriel Fernandeze32af882012-12-17 15:53:24 +010027#include <linux/pinctrl/machine.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 Walleije98ea772012-04-26 23:57:25 +020033#include "pinctrl-nomadik.h"
Linus Walleij3a198052014-07-11 14:57:06 +020034#include "../core.h"
Linus Walleijba388292014-09-29 15:17:04 +020035#include "../pinctrl-utils.h"
Linus Walleije98ea772012-04-26 23:57:25 +020036
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010037/*
38 * The GPIO module in the Nomadik family of Systems-on-Chip is an
39 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020040 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010041 *
42 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
43 */
44
Linus Walleij8d993392013-11-19 23:02:11 +010045/*
46 * pin configurations are represented by 32-bit integers:
47 *
48 * bit 0.. 8 - Pin Number (512 Pins Maximum)
49 * bit 9..10 - Alternate Function Selection
50 * bit 11..12 - Pull up/down state
51 * bit 13 - Sleep mode behaviour
52 * bit 14 - Direction
53 * bit 15 - Value (if output)
54 * bit 16..18 - SLPM pull up/down state
55 * bit 19..20 - SLPM direction
56 * bit 21..22 - SLPM Value (if output)
57 * bit 23..25 - PDIS value (if input)
58 * bit 26 - Gpio mode
59 * bit 27 - Sleep mode
60 *
61 * to facilitate the definition, the following macros are provided
62 *
63 * PIN_CFG_DEFAULT - default config (0):
64 * pull up/down = disabled
65 * sleep mode = input/wakeup
66 * direction = input
67 * value = low
68 * SLPM direction = same as normal
69 * SLPM pull = same as normal
70 * SLPM value = same as normal
71 *
72 * PIN_CFG - default config with alternate function
73 */
74
75typedef unsigned long pin_cfg_t;
76
77#define PIN_NUM_MASK 0x1ff
78#define PIN_NUM(x) ((x) & PIN_NUM_MASK)
79
80#define PIN_ALT_SHIFT 9
81#define PIN_ALT_MASK (0x3 << PIN_ALT_SHIFT)
82#define PIN_ALT(x) (((x) & PIN_ALT_MASK) >> PIN_ALT_SHIFT)
83#define PIN_GPIO (NMK_GPIO_ALT_GPIO << PIN_ALT_SHIFT)
84#define PIN_ALT_A (NMK_GPIO_ALT_A << PIN_ALT_SHIFT)
85#define PIN_ALT_B (NMK_GPIO_ALT_B << PIN_ALT_SHIFT)
86#define PIN_ALT_C (NMK_GPIO_ALT_C << PIN_ALT_SHIFT)
87
88#define PIN_PULL_SHIFT 11
89#define PIN_PULL_MASK (0x3 << PIN_PULL_SHIFT)
90#define PIN_PULL(x) (((x) & PIN_PULL_MASK) >> PIN_PULL_SHIFT)
91#define PIN_PULL_NONE (NMK_GPIO_PULL_NONE << PIN_PULL_SHIFT)
92#define PIN_PULL_UP (NMK_GPIO_PULL_UP << PIN_PULL_SHIFT)
93#define PIN_PULL_DOWN (NMK_GPIO_PULL_DOWN << PIN_PULL_SHIFT)
94
95#define PIN_SLPM_SHIFT 13
96#define PIN_SLPM_MASK (0x1 << PIN_SLPM_SHIFT)
97#define PIN_SLPM(x) (((x) & PIN_SLPM_MASK) >> PIN_SLPM_SHIFT)
98#define PIN_SLPM_MAKE_INPUT (NMK_GPIO_SLPM_INPUT << PIN_SLPM_SHIFT)
99#define PIN_SLPM_NOCHANGE (NMK_GPIO_SLPM_NOCHANGE << PIN_SLPM_SHIFT)
100/* These two replace the above in DB8500v2+ */
101#define PIN_SLPM_WAKEUP_ENABLE (NMK_GPIO_SLPM_WAKEUP_ENABLE << PIN_SLPM_SHIFT)
102#define PIN_SLPM_WAKEUP_DISABLE (NMK_GPIO_SLPM_WAKEUP_DISABLE << PIN_SLPM_SHIFT)
103#define PIN_SLPM_USE_MUX_SETTINGS_IN_SLEEP PIN_SLPM_WAKEUP_DISABLE
104
105#define PIN_SLPM_GPIO PIN_SLPM_WAKEUP_ENABLE /* In SLPM, pin is a gpio */
106#define PIN_SLPM_ALTFUNC PIN_SLPM_WAKEUP_DISABLE /* In SLPM, pin is altfunc */
107
108#define PIN_DIR_SHIFT 14
109#define PIN_DIR_MASK (0x1 << PIN_DIR_SHIFT)
110#define PIN_DIR(x) (((x) & PIN_DIR_MASK) >> PIN_DIR_SHIFT)
111#define PIN_DIR_INPUT (0 << PIN_DIR_SHIFT)
112#define PIN_DIR_OUTPUT (1 << PIN_DIR_SHIFT)
113
114#define PIN_VAL_SHIFT 15
115#define PIN_VAL_MASK (0x1 << PIN_VAL_SHIFT)
116#define PIN_VAL(x) (((x) & PIN_VAL_MASK) >> PIN_VAL_SHIFT)
117#define PIN_VAL_LOW (0 << PIN_VAL_SHIFT)
118#define PIN_VAL_HIGH (1 << PIN_VAL_SHIFT)
119
120#define PIN_SLPM_PULL_SHIFT 16
121#define PIN_SLPM_PULL_MASK (0x7 << PIN_SLPM_PULL_SHIFT)
122#define PIN_SLPM_PULL(x) \
123 (((x) & PIN_SLPM_PULL_MASK) >> PIN_SLPM_PULL_SHIFT)
124#define PIN_SLPM_PULL_NONE \
125 ((1 + NMK_GPIO_PULL_NONE) << PIN_SLPM_PULL_SHIFT)
126#define PIN_SLPM_PULL_UP \
127 ((1 + NMK_GPIO_PULL_UP) << PIN_SLPM_PULL_SHIFT)
128#define PIN_SLPM_PULL_DOWN \
129 ((1 + NMK_GPIO_PULL_DOWN) << PIN_SLPM_PULL_SHIFT)
130
131#define PIN_SLPM_DIR_SHIFT 19
132#define PIN_SLPM_DIR_MASK (0x3 << PIN_SLPM_DIR_SHIFT)
133#define PIN_SLPM_DIR(x) \
134 (((x) & PIN_SLPM_DIR_MASK) >> PIN_SLPM_DIR_SHIFT)
135#define PIN_SLPM_DIR_INPUT ((1 + 0) << PIN_SLPM_DIR_SHIFT)
136#define PIN_SLPM_DIR_OUTPUT ((1 + 1) << PIN_SLPM_DIR_SHIFT)
137
138#define PIN_SLPM_VAL_SHIFT 21
139#define PIN_SLPM_VAL_MASK (0x3 << PIN_SLPM_VAL_SHIFT)
140#define PIN_SLPM_VAL(x) \
141 (((x) & PIN_SLPM_VAL_MASK) >> PIN_SLPM_VAL_SHIFT)
142#define PIN_SLPM_VAL_LOW ((1 + 0) << PIN_SLPM_VAL_SHIFT)
143#define PIN_SLPM_VAL_HIGH ((1 + 1) << PIN_SLPM_VAL_SHIFT)
144
145#define PIN_SLPM_PDIS_SHIFT 23
146#define PIN_SLPM_PDIS_MASK (0x3 << PIN_SLPM_PDIS_SHIFT)
147#define PIN_SLPM_PDIS(x) \
148 (((x) & PIN_SLPM_PDIS_MASK) >> PIN_SLPM_PDIS_SHIFT)
149#define PIN_SLPM_PDIS_NO_CHANGE (0 << PIN_SLPM_PDIS_SHIFT)
150#define PIN_SLPM_PDIS_DISABLED (1 << PIN_SLPM_PDIS_SHIFT)
151#define PIN_SLPM_PDIS_ENABLED (2 << PIN_SLPM_PDIS_SHIFT)
152
153#define PIN_LOWEMI_SHIFT 25
154#define PIN_LOWEMI_MASK (0x1 << PIN_LOWEMI_SHIFT)
155#define PIN_LOWEMI(x) (((x) & PIN_LOWEMI_MASK) >> PIN_LOWEMI_SHIFT)
156#define PIN_LOWEMI_DISABLED (0 << PIN_LOWEMI_SHIFT)
157#define PIN_LOWEMI_ENABLED (1 << PIN_LOWEMI_SHIFT)
158
159#define PIN_GPIOMODE_SHIFT 26
160#define PIN_GPIOMODE_MASK (0x1 << PIN_GPIOMODE_SHIFT)
161#define PIN_GPIOMODE(x) (((x) & PIN_GPIOMODE_MASK) >> PIN_GPIOMODE_SHIFT)
162#define PIN_GPIOMODE_DISABLED (0 << PIN_GPIOMODE_SHIFT)
163#define PIN_GPIOMODE_ENABLED (1 << PIN_GPIOMODE_SHIFT)
164
165#define PIN_SLEEPMODE_SHIFT 27
166#define PIN_SLEEPMODE_MASK (0x1 << PIN_SLEEPMODE_SHIFT)
167#define PIN_SLEEPMODE(x) (((x) & PIN_SLEEPMODE_MASK) >> PIN_SLEEPMODE_SHIFT)
168#define PIN_SLEEPMODE_DISABLED (0 << PIN_SLEEPMODE_SHIFT)
169#define PIN_SLEEPMODE_ENABLED (1 << PIN_SLEEPMODE_SHIFT)
170
171
172/* Shortcuts. Use these instead of separate DIR, PULL, and VAL. */
173#define PIN_INPUT_PULLDOWN (PIN_DIR_INPUT | PIN_PULL_DOWN)
174#define PIN_INPUT_PULLUP (PIN_DIR_INPUT | PIN_PULL_UP)
175#define PIN_INPUT_NOPULL (PIN_DIR_INPUT | PIN_PULL_NONE)
176#define PIN_OUTPUT_LOW (PIN_DIR_OUTPUT | PIN_VAL_LOW)
177#define PIN_OUTPUT_HIGH (PIN_DIR_OUTPUT | PIN_VAL_HIGH)
178
179#define PIN_SLPM_INPUT_PULLDOWN (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_DOWN)
180#define PIN_SLPM_INPUT_PULLUP (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_UP)
181#define PIN_SLPM_INPUT_NOPULL (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_NONE)
182#define PIN_SLPM_OUTPUT_LOW (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_LOW)
183#define PIN_SLPM_OUTPUT_HIGH (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_HIGH)
184
185#define PIN_CFG_DEFAULT (0)
186
187#define PIN_CFG(num, alt) \
188 (PIN_CFG_DEFAULT |\
189 (PIN_NUM(num) | PIN_##alt))
190
191#define PIN_CFG_INPUT(num, alt, pull) \
192 (PIN_CFG_DEFAULT |\
193 (PIN_NUM(num) | PIN_##alt | PIN_INPUT_##pull))
194
195#define PIN_CFG_OUTPUT(num, alt, val) \
196 (PIN_CFG_DEFAULT |\
197 (PIN_NUM(num) | PIN_##alt | PIN_OUTPUT_##val))
198
199/*
200 * "nmk_gpio" and "NMK_GPIO" stand for "Nomadik GPIO", leaving
201 * the "gpio" namespace for generic and cross-machine functions
202 */
203
204#define GPIO_BLOCK_SHIFT 5
205#define NMK_GPIO_PER_CHIP (1 << GPIO_BLOCK_SHIFT)
Linus Walleijbc222ef2015-06-17 15:45:41 +0200206#define NMK_MAX_BANKS DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)
Linus Walleij8d993392013-11-19 23:02:11 +0100207
208/* Register in the logic block */
209#define NMK_GPIO_DAT 0x00
210#define NMK_GPIO_DATS 0x04
211#define NMK_GPIO_DATC 0x08
212#define NMK_GPIO_PDIS 0x0c
213#define NMK_GPIO_DIR 0x10
214#define NMK_GPIO_DIRS 0x14
215#define NMK_GPIO_DIRC 0x18
216#define NMK_GPIO_SLPC 0x1c
217#define NMK_GPIO_AFSLA 0x20
218#define NMK_GPIO_AFSLB 0x24
219#define NMK_GPIO_LOWEMI 0x28
220
221#define NMK_GPIO_RIMSC 0x40
222#define NMK_GPIO_FIMSC 0x44
223#define NMK_GPIO_IS 0x48
224#define NMK_GPIO_IC 0x4c
225#define NMK_GPIO_RWIMSC 0x50
226#define NMK_GPIO_FWIMSC 0x54
227#define NMK_GPIO_WKS 0x58
228/* These appear in DB8540 and later ASICs */
229#define NMK_GPIO_EDGELEVEL 0x5C
230#define NMK_GPIO_LEVEL 0x60
231
232
233/* Pull up/down values */
234enum nmk_gpio_pull {
235 NMK_GPIO_PULL_NONE,
236 NMK_GPIO_PULL_UP,
237 NMK_GPIO_PULL_DOWN,
238};
239
240/* Sleep mode */
241enum nmk_gpio_slpm {
242 NMK_GPIO_SLPM_INPUT,
243 NMK_GPIO_SLPM_WAKEUP_ENABLE = NMK_GPIO_SLPM_INPUT,
244 NMK_GPIO_SLPM_NOCHANGE,
245 NMK_GPIO_SLPM_WAKEUP_DISABLE = NMK_GPIO_SLPM_NOCHANGE,
246};
247
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100248struct nmk_gpio_chip {
249 struct gpio_chip chip;
Linus Walleij3007d942015-05-06 14:46:40 +0200250 struct irq_chip irqchip;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100251 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100252 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +0530253 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100254 unsigned int parent_irq;
Linus Walleij194e15b2014-03-21 10:24:42 +0100255 int latent_parent_irq;
256 u32 (*get_latent_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +0530257 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +0100258 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +0200259 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100260 /* Keep track of configured edges */
261 u32 edge_rising;
262 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530263 u32 real_wake;
264 u32 rwimsc;
265 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530266 u32 rimsc;
267 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200268 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +0530269 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100270};
271
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200272/**
273 * struct nmk_pinctrl - state container for the Nomadik pin controller
274 * @dev: containing device pointer
275 * @pctl: corresponding pin controller device
276 * @soc: SoC data for this specific chip
277 * @prcm_base: PRCM register range virtual base
278 */
Linus Walleije98ea772012-04-26 23:57:25 +0200279struct nmk_pinctrl {
280 struct device *dev;
281 struct pinctrl_dev *pctl;
282 const struct nmk_pinctrl_soc_data *soc;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200283 void __iomem *prcm_base;
Linus Walleije98ea772012-04-26 23:57:25 +0200284};
285
Linus Walleijbc222ef2015-06-17 15:45:41 +0200286static struct nmk_gpio_chip *nmk_gpio_chips[NMK_MAX_BANKS];
Rabin Vincent01727e62010-12-13 12:02:40 +0530287
288static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
289
290#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
291
Rabin Vincent6f9a9742010-06-02 05:50:28 +0100292static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
293 unsigned offset, int gpio_mode)
294{
295 u32 bit = 1 << offset;
296 u32 afunc, bfunc;
297
298 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
299 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
300 if (gpio_mode & NMK_GPIO_ALT_A)
301 afunc |= bit;
302 if (gpio_mode & NMK_GPIO_ALT_B)
303 bfunc |= bit;
304 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
305 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
306}
307
Rabin Vincent81a3c292010-05-27 12:39:23 +0100308static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
309 unsigned offset, enum nmk_gpio_slpm mode)
310{
311 u32 bit = 1 << offset;
312 u32 slpm;
313
314 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
315 if (mode == NMK_GPIO_SLPM_NOCHANGE)
316 slpm |= bit;
317 else
318 slpm &= ~bit;
319 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
320}
321
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100322static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
323 unsigned offset, enum nmk_gpio_pull pull)
324{
325 u32 bit = 1 << offset;
326 u32 pdis;
327
328 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200329 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100330 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200331 nmk_chip->pull_up &= ~bit;
332 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100333 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200334 }
335
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100336 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
337
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200338 if (pull == NMK_GPIO_PULL_UP) {
339 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100340 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200341 } else if (pull == NMK_GPIO_PULL_DOWN) {
342 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100343 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200344 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100345}
346
Rabin Vincentebc61782011-09-28 15:49:11 +0530347static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
348 unsigned offset, bool lowemi)
349{
350 u32 bit = BIT(offset);
351 bool enabled = nmk_chip->lowemi & bit;
352
353 if (lowemi == enabled)
354 return;
355
356 if (lowemi)
357 nmk_chip->lowemi |= bit;
358 else
359 nmk_chip->lowemi &= ~bit;
360
361 writel_relaxed(nmk_chip->lowemi,
362 nmk_chip->addr + NMK_GPIO_LOWEMI);
363}
364
Rabin Vincent378be062010-06-02 06:06:29 +0100365static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
366 unsigned offset)
367{
368 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
369}
370
Rabin Vincent6720db72010-09-02 11:28:48 +0100371static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
372 unsigned offset, int val)
373{
374 if (val)
375 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
376 else
377 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
378}
379
380static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
381 unsigned offset, int val)
382{
383 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
384 __nmk_gpio_set_output(nmk_chip, offset, val);
385}
386
Rabin Vincent01727e62010-12-13 12:02:40 +0530387static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
388 unsigned offset, int gpio_mode,
389 bool glitch)
390{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530391 u32 rwimsc = nmk_chip->rwimsc;
392 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530393
394 if (glitch && nmk_chip->set_ioforce) {
395 u32 bit = BIT(offset);
396
Rabin Vincent01727e62010-12-13 12:02:40 +0530397 /* Prevent spurious wakeups */
398 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
399 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
400
401 nmk_chip->set_ioforce(true);
402 }
403
404 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
405
406 if (glitch && nmk_chip->set_ioforce) {
407 nmk_chip->set_ioforce(false);
408
409 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
410 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
411 }
412}
413
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530414static void
415nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
416{
417 u32 falling = nmk_chip->fimsc & BIT(offset);
418 u32 rising = nmk_chip->rimsc & BIT(offset);
419 int gpio = nmk_chip->chip.base + offset;
Linus Walleije0bc34a2014-03-25 10:44:09 +0100420 int irq = irq_find_mapping(nmk_chip->chip.irqdomain, offset);
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530421 struct irq_data *d = irq_get_irq_data(irq);
422
423 if (!rising && !falling)
424 return;
425
426 if (!d || !irqd_irq_disabled(d))
427 return;
428
429 if (rising) {
430 nmk_chip->rimsc &= ~BIT(offset);
431 writel_relaxed(nmk_chip->rimsc,
432 nmk_chip->addr + NMK_GPIO_RIMSC);
433 }
434
435 if (falling) {
436 nmk_chip->fimsc &= ~BIT(offset);
437 writel_relaxed(nmk_chip->fimsc,
438 nmk_chip->addr + NMK_GPIO_FIMSC);
439 }
440
441 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
442}
443
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200444static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
445{
446 u32 val;
447
448 val = readl(reg);
449 val = ((val & ~mask) | (value & mask));
450 writel(val, reg);
451}
452
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200453static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
454 unsigned offset, unsigned alt_num)
455{
456 int i;
457 u16 reg;
458 u8 bit;
459 u8 alt_index;
460 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
461 const u16 *gpiocr_regs;
462
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100463 if (!npct->prcm_base)
464 return;
465
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200466 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
467 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
468 alt_num);
469 return;
470 }
471
472 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
473 if (npct->soc->altcx_pins[i].pin == offset)
474 break;
475 }
476 if (i == npct->soc->npins_altcx) {
477 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
478 offset);
479 return;
480 }
481
482 pin_desc = npct->soc->altcx_pins + i;
483 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
484
485 /*
486 * If alt_num is NULL, just clear current ALTCx selection
487 * to make sure we come back to a pure ALTC selection
488 */
489 if (!alt_num) {
490 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
491 if (pin_desc->altcx[i].used == true) {
492 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
493 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200494 if (readl(npct->prcm_base + reg) & BIT(bit)) {
495 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200496 dev_dbg(npct->dev,
497 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
498 offset, i+1);
499 }
500 }
501 }
502 return;
503 }
504
505 alt_index = alt_num - 1;
506 if (pin_desc->altcx[alt_index].used == false) {
507 dev_warn(npct->dev,
508 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
509 offset, alt_num);
510 return;
511 }
512
513 /*
514 * Check if any other ALTCx functions are activated on this pin
515 * and disable it first.
516 */
517 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
518 if (i == alt_index)
519 continue;
520 if (pin_desc->altcx[i].used == true) {
521 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
522 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200523 if (readl(npct->prcm_base + reg) & BIT(bit)) {
524 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200525 dev_dbg(npct->dev,
526 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
527 offset, i+1);
528 }
529 }
530 }
531
532 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
533 bit = pin_desc->altcx[alt_index].control_bit;
534 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
535 offset, alt_index+1);
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200536 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200537}
538
Rabin Vincent01727e62010-12-13 12:02:40 +0530539/*
540 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
541 * - Save SLPM registers
542 * - Set SLPM=0 for the IOs you want to switch and others to 1
543 * - Configure the GPIO registers for the IOs that are being switched
544 * - Set IOFORCE=1
545 * - Modify the AFLSA/B registers for the IOs that are being switched
546 * - Set IOFORCE=0
547 * - Restore SLPM registers
548 * - Any spurious wake up event during switch sequence to be ignored and
549 * cleared
550 */
551static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
552{
553 int i;
554
555 for (i = 0; i < NUM_BANKS; i++) {
556 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
557 unsigned int temp = slpm[i];
558
559 if (!chip)
560 break;
561
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200562 clk_enable(chip->clk);
563
Rabin Vincent01727e62010-12-13 12:02:40 +0530564 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
565 writel(temp, chip->addr + NMK_GPIO_SLPC);
566 }
567}
568
569static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
570{
571 int i;
572
573 for (i = 0; i < NUM_BANKS; i++) {
574 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
575
576 if (!chip)
577 break;
578
579 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200580
581 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530582 }
583}
584
Arnd Bergmann0fafd502013-01-25 14:14:30 +0000585static int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200586{
587 int i;
588 u16 reg;
589 u8 bit;
590 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
591 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
592 const u16 *gpiocr_regs;
593
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100594 if (!npct->prcm_base)
595 return NMK_GPIO_ALT_C;
596
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200597 for (i = 0; i < npct->soc->npins_altcx; i++) {
598 if (npct->soc->altcx_pins[i].pin == gpio)
599 break;
600 }
601 if (i == npct->soc->npins_altcx)
602 return NMK_GPIO_ALT_C;
603
604 pin_desc = npct->soc->altcx_pins + i;
605 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
606 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
607 if (pin_desc->altcx[i].used == true) {
608 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
609 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200610 if (readl(npct->prcm_base + reg) & BIT(bit))
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200611 return NMK_GPIO_ALT_C+i+1;
612 }
613 }
614 return NMK_GPIO_ALT_C;
615}
616
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100617int nmk_gpio_get_mode(int gpio)
618{
619 struct nmk_gpio_chip *nmk_chip;
620 u32 afunc, bfunc, bit;
621
Lee Jonesa60b57e2012-04-19 21:36:31 +0100622 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100623 if (!nmk_chip)
624 return -EINVAL;
625
Lee Jonesa60b57e2012-04-19 21:36:31 +0100626 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100627
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200628 clk_enable(nmk_chip->clk);
629
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100630 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
631 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
632
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200633 clk_disable(nmk_chip->clk);
634
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100635 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
636}
637EXPORT_SYMBOL(nmk_gpio_get_mode);
638
639
640/* IRQ functions */
641static inline int nmk_gpio_get_bitmask(int gpio)
642{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100643 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100644}
645
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100646static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100647{
Linus Walleije0bc34a2014-03-25 10:44:09 +0100648 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
649 struct nmk_gpio_chip *nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200650
651 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100652 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200653 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100654}
655
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100656enum nmk_gpio_irq_type {
657 NORMAL,
658 WAKE,
659};
660
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100661static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100662 int gpio, enum nmk_gpio_irq_type which,
663 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100664{
665 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530666 u32 *rimscval;
667 u32 *fimscval;
668 u32 rimscreg;
669 u32 fimscreg;
670
671 if (which == NORMAL) {
672 rimscreg = NMK_GPIO_RIMSC;
673 fimscreg = NMK_GPIO_FIMSC;
674 rimscval = &nmk_chip->rimsc;
675 fimscval = &nmk_chip->fimsc;
676 } else {
677 rimscreg = NMK_GPIO_RWIMSC;
678 fimscreg = NMK_GPIO_FWIMSC;
679 rimscval = &nmk_chip->rwimsc;
680 fimscval = &nmk_chip->fwimsc;
681 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100682
683 /* we must individually set/clear the two edges */
684 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100685 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530686 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100687 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530688 *rimscval &= ~bitmask;
689 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100690 }
691 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100692 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530693 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100694 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530695 *fimscval &= ~bitmask;
696 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100697 }
698}
699
Rabin Vincentb9df4682011-02-10 11:45:58 +0530700static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
701 int gpio, bool on)
702{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530703 /*
704 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
705 * disabled, since setting SLPM to 1 increases power consumption, and
706 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
707 */
708 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200709 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530710 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200711 }
712
Rabin Vincentb9df4682011-02-10 11:45:58 +0530713 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
714}
715
716static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100717{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100718 struct nmk_gpio_chip *nmk_chip;
719 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100720 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100721
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100722 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100723 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100724 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100725 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100726
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200727 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530728 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
729 spin_lock(&nmk_chip->lock);
730
Lee Jonesa60b57e2012-04-19 21:36:31 +0100731 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530732
733 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100734 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530735
736 spin_unlock(&nmk_chip->lock);
737 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200738 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100739
740 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100741}
742
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100743static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100744{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530745 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100746}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100747
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100748static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100749{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530750 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100751}
752
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100753static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100754{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100755 struct nmk_gpio_chip *nmk_chip;
756 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530757 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100758
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100759 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100760 if (!nmk_chip)
761 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100762 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100763
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200764 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530765 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
766 spin_lock(&nmk_chip->lock);
767
Linus Walleij479a0c72011-09-20 10:50:15 +0200768 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100769 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530770
771 if (on)
772 nmk_chip->real_wake |= bitmask;
773 else
774 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530775
776 spin_unlock(&nmk_chip->lock);
777 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200778 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100779
780 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100781}
782
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100783static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100784{
Linus Walleij479a0c72011-09-20 10:50:15 +0200785 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200786 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100787 struct nmk_gpio_chip *nmk_chip;
788 unsigned long flags;
789 u32 bitmask;
790
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100791 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100792 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100793 if (!nmk_chip)
794 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100795 if (type & IRQ_TYPE_LEVEL_HIGH)
796 return -EINVAL;
797 if (type & IRQ_TYPE_LEVEL_LOW)
798 return -EINVAL;
799
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200800 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100801 spin_lock_irqsave(&nmk_chip->lock, flags);
802
Rabin Vincent7a852d82010-05-06 10:43:55 +0100803 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100804 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100805
Rabin Vincentb9df4682011-02-10 11:45:58 +0530806 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100807 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100808
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100809 nmk_chip->edge_rising &= ~bitmask;
810 if (type & IRQ_TYPE_EDGE_RISING)
811 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100812
813 nmk_chip->edge_falling &= ~bitmask;
814 if (type & IRQ_TYPE_EDGE_FALLING)
815 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100816
817 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100818 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100819
Rabin Vincentb9df4682011-02-10 11:45:58 +0530820 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100821 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100822
823 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200824 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100825
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100826 return 0;
827}
828
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200829static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
830{
831 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
832
833 clk_enable(nmk_chip->clk);
834 nmk_gpio_irq_unmask(d);
835 return 0;
836}
837
838static void nmk_gpio_irq_shutdown(struct irq_data *d)
839{
840 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
841
842 nmk_gpio_irq_mask(d);
843 clk_disable(nmk_chip->clk);
844}
845
Jiang Liu5663bb22015-06-04 12:13:16 +0800846static void __nmk_gpio_irq_handler(struct irq_desc *desc, u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100847{
Jiang Liu5663bb22015-06-04 12:13:16 +0800848 struct irq_chip *host_chip = irq_desc_get_chip(desc);
Linus Walleije0bc34a2014-03-25 10:44:09 +0100849 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100850
Will Deaconadfed152011-02-28 10:12:29 +0000851 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100852
Rabin Vincent33b744b2010-10-14 10:38:03 +0530853 while (status) {
854 int bit = __ffs(status);
855
Linus Walleije0bc34a2014-03-25 10:44:09 +0100856 generic_handle_irq(irq_find_mapping(chip->irqdomain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530857 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100858 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100859
Will Deaconadfed152011-02-28 10:12:29 +0000860 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100861}
862
Rabin Vincent33b744b2010-10-14 10:38:03 +0530863static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
864{
Linus Walleije0bc34a2014-03-25 10:44:09 +0100865 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
866 struct nmk_gpio_chip *nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200867 u32 status;
868
869 clk_enable(nmk_chip->clk);
870 status = readl(nmk_chip->addr + NMK_GPIO_IS);
871 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530872
Jiang Liu5663bb22015-06-04 12:13:16 +0800873 __nmk_gpio_irq_handler(desc, status);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530874}
875
Jiang Liu5663bb22015-06-04 12:13:16 +0800876static void nmk_gpio_latent_irq_handler(unsigned int irq, struct irq_desc *desc)
Rabin Vincent33b744b2010-10-14 10:38:03 +0530877{
Linus Walleije0bc34a2014-03-25 10:44:09 +0100878 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
879 struct nmk_gpio_chip *nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij194e15b2014-03-21 10:24:42 +0100880 u32 status = nmk_chip->get_latent_status(nmk_chip->bank);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530881
Jiang Liu5663bb22015-06-04 12:13:16 +0800882 __nmk_gpio_irq_handler(desc, status);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530883}
884
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100885/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200886
887static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
888{
889 /*
890 * Map back to global GPIO space and request muxing, the direction
891 * parameter does not matter for this controller.
892 */
893 int gpio = chip->base + offset;
894
895 return pinctrl_request_gpio(gpio);
896}
897
898static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
899{
900 int gpio = chip->base + offset;
901
902 pinctrl_free_gpio(gpio);
903}
904
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100905static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
906{
907 struct nmk_gpio_chip *nmk_chip =
908 container_of(chip, struct nmk_gpio_chip, chip);
909
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200910 clk_enable(nmk_chip->clk);
911
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100912 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200913
914 clk_disable(nmk_chip->clk);
915
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100916 return 0;
917}
918
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100919static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
920{
921 struct nmk_gpio_chip *nmk_chip =
922 container_of(chip, struct nmk_gpio_chip, chip);
923 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200924 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100925
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200926 clk_enable(nmk_chip->clk);
927
928 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
929
930 clk_disable(nmk_chip->clk);
931
932 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100933}
934
935static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
936 int val)
937{
938 struct nmk_gpio_chip *nmk_chip =
939 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100940
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200941 clk_enable(nmk_chip->clk);
942
Rabin Vincent6720db72010-09-02 11:28:48 +0100943 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200944
945 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100946}
947
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100948static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
949 int val)
950{
951 struct nmk_gpio_chip *nmk_chip =
952 container_of(chip, struct nmk_gpio_chip, chip);
953
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200954 clk_enable(nmk_chip->clk);
955
Rabin Vincent6720db72010-09-02 11:28:48 +0100956 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100957
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200958 clk_disable(nmk_chip->clk);
959
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100960 return 0;
961}
962
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530963#ifdef CONFIG_DEBUG_FS
964
965#include <linux/seq_file.h>
966
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200967static void nmk_gpio_dbg_show_one(struct seq_file *s,
968 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
969 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530970{
Linus Walleij6f4350a2012-05-02 21:06:13 +0200971 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530972 struct nmk_gpio_chip *nmk_chip =
973 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200974 int mode;
975 bool is_out;
Linus Walleij8f1774a2014-09-30 15:05:21 +0200976 bool data_out;
Linus Walleij6f4350a2012-05-02 21:06:13 +0200977 bool pull;
978 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530979 const char *modes[] = {
980 [NMK_GPIO_ALT_GPIO] = "gpio",
981 [NMK_GPIO_ALT_A] = "altA",
982 [NMK_GPIO_ALT_B] = "altB",
983 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200984 [NMK_GPIO_ALT_C+1] = "altC1",
985 [NMK_GPIO_ALT_C+2] = "altC2",
986 [NMK_GPIO_ALT_C+3] = "altC3",
987 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530988 };
Linus Walleij8f1774a2014-09-30 15:05:21 +0200989 const char *pulls[] = {
990 "none ",
991 "pull down",
992 "pull up ",
993 };
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530994
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200995 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200996 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
997 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
Linus Walleij8f1774a2014-09-30 15:05:21 +0200998 data_out = !!(readl(nmk_chip->addr + NMK_GPIO_DAT) & bit);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200999 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001000 if ((mode == NMK_GPIO_ALT_C) && pctldev)
1001 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001002
Linus Walleij8f1774a2014-09-30 15:05:21 +02001003 if (is_out) {
1004 seq_printf(s, " gpio-%-3d (%-20.20s) out %s %s",
1005 gpio,
1006 label ?: "(none)",
1007 data_out ? "hi" : "lo",
1008 (mode < 0) ? "unknown" : modes[mode]);
1009 } else {
Linus Walleij47058452013-11-14 19:51:18 +01001010 int irq = gpio_to_irq(gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001011 struct irq_desc *desc = irq_to_desc(irq);
Linus Walleij8f1774a2014-09-30 15:05:21 +02001012 int pullidx = 0;
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001013
Linus Walleij8f1774a2014-09-30 15:05:21 +02001014 if (pull)
1015 pullidx = data_out ? 1 : 2;
1016
1017 seq_printf(s, " gpio-%-3d (%-20.20s) in %s %s",
1018 gpio,
1019 label ?: "(none)",
1020 pulls[pullidx],
1021 (mode < 0) ? "unknown" : modes[mode]);
1022 /*
1023 * This races with request_irq(), set_irq_type(),
Linus Walleij6f4350a2012-05-02 21:06:13 +02001024 * and set_irq_wake() ... but those are "rare".
1025 */
Linus Walleij47058452013-11-14 19:51:18 +01001026 if (irq > 0 && desc && desc->action) {
Linus Walleij6f4350a2012-05-02 21:06:13 +02001027 char *trigger;
1028 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001029
Linus Walleij6f4350a2012-05-02 21:06:13 +02001030 if (nmk_chip->edge_rising & bitmask)
1031 trigger = "edge-rising";
1032 else if (nmk_chip->edge_falling & bitmask)
1033 trigger = "edge-falling";
1034 else
1035 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001036
Linus Walleij6f4350a2012-05-02 21:06:13 +02001037 seq_printf(s, " irq-%d %s%s",
1038 irq, trigger,
1039 irqd_is_wakeup_set(&desc->irq_data)
1040 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +02001041 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301042 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001043 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301044}
1045
Linus Walleij6f4350a2012-05-02 21:06:13 +02001046static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1047{
1048 unsigned i;
1049 unsigned gpio = chip->base;
1050
1051 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001052 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +02001053 seq_printf(s, "\n");
1054 }
1055}
1056
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301057#else
Linus Walleij6f4350a2012-05-02 21:06:13 +02001058static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001059 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +02001060 struct gpio_chip *chip,
1061 unsigned offset, unsigned gpio)
1062{
1063}
Rabin Vincentd0b543c2010-03-04 17:39:05 +05301064#define nmk_gpio_dbg_show NULL
1065#endif
1066
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001067void nmk_gpio_clocks_enable(void)
1068{
1069 int i;
1070
1071 for (i = 0; i < NUM_BANKS; i++) {
1072 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1073
1074 if (!chip)
1075 continue;
1076
1077 clk_enable(chip->clk);
1078 }
1079}
1080
1081void nmk_gpio_clocks_disable(void)
1082{
1083 int i;
1084
1085 for (i = 0; i < NUM_BANKS; i++) {
1086 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1087
1088 if (!chip)
1089 continue;
1090
1091 clk_disable(chip->clk);
1092 }
1093}
1094
Rabin Vincentb9df4682011-02-10 11:45:58 +05301095/*
1096 * Called from the suspend/resume path to only keep the real wakeup interrupts
1097 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
1098 * and not the rest of the interrupts which we needed to have as wakeups for
1099 * cpuidle.
1100 *
1101 * PM ops are not used since this needs to be done at the end, after all the
1102 * other drivers are done with their suspend callbacks.
1103 */
1104void nmk_gpio_wakeups_suspend(void)
1105{
1106 int i;
1107
1108 for (i = 0; i < NUM_BANKS; i++) {
1109 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1110
1111 if (!chip)
1112 break;
1113
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001114 clk_enable(chip->clk);
1115
Rabin Vincentb9df4682011-02-10 11:45:58 +05301116 writel(chip->rwimsc & chip->real_wake,
1117 chip->addr + NMK_GPIO_RWIMSC);
1118 writel(chip->fwimsc & chip->real_wake,
1119 chip->addr + NMK_GPIO_FWIMSC);
1120
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001121 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301122 }
1123}
1124
1125void nmk_gpio_wakeups_resume(void)
1126{
1127 int i;
1128
1129 for (i = 0; i < NUM_BANKS; i++) {
1130 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
1131
1132 if (!chip)
1133 break;
1134
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001135 clk_enable(chip->clk);
1136
Rabin Vincentb9df4682011-02-10 11:45:58 +05301137 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
1138 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
1139
Rabin Vincent3c0227d2011-09-20 10:50:03 +02001140 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +05301141 }
1142}
1143
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +02001144/*
1145 * Read the pull up/pull down status.
1146 * A bit set in 'pull_up' means that pull up
1147 * is selected if pull is enabled in PDIS register.
1148 * Note: only pull up/down set via this driver can
1149 * be detected due to HW limitations.
1150 */
1151void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
1152{
1153 if (gpio_bank < NUM_BANKS) {
1154 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
1155
1156 if (!chip)
1157 return;
1158
1159 *pull_up = chip->pull_up;
1160 }
1161}
1162
Linus Walleijbc222ef2015-06-17 15:45:41 +02001163/*
1164 * We will allocate memory for the state container using devm* allocators
1165 * binding to the first device reaching this point, it doesn't matter if
1166 * it is the pin controller or GPIO driver. However we need to use the right
1167 * platform device when looking up resources so pay attention to pdev.
1168 */
1169static struct nmk_gpio_chip *nmk_gpio_populate_chip(struct device_node *np,
1170 struct platform_device *pdev)
1171{
1172 struct nmk_gpio_chip *nmk_chip;
1173 struct platform_device *gpio_pdev;
1174 struct gpio_chip *chip;
1175 struct resource *res;
1176 struct clk *clk;
1177 void __iomem *base;
1178 u32 id;
1179
1180 gpio_pdev = of_find_device_by_node(np);
1181 if (!gpio_pdev) {
1182 pr_err("populate \"%s\": device not found\n", np->name);
1183 return ERR_PTR(-ENODEV);
1184 }
1185 if (of_property_read_u32(np, "gpio-bank", &id)) {
1186 dev_err(&pdev->dev, "populate: gpio-bank property not found\n");
1187 return ERR_PTR(-EINVAL);
1188 }
1189
1190 /* Already populated? */
1191 nmk_chip = nmk_gpio_chips[id];
1192 if (nmk_chip)
1193 return nmk_chip;
1194
1195 nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL);
1196 if (!nmk_chip)
1197 return ERR_PTR(-ENOMEM);
1198
1199 nmk_chip->bank = id;
1200 chip = &nmk_chip->chip;
1201 chip->base = id * NMK_GPIO_PER_CHIP;
1202 chip->ngpio = NMK_GPIO_PER_CHIP;
1203 chip->label = dev_name(&gpio_pdev->dev);
1204 chip->dev = &gpio_pdev->dev;
1205
1206 res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
1207 base = devm_ioremap_resource(&pdev->dev, res);
1208 if (IS_ERR(base))
1209 return base;
1210 nmk_chip->addr = base;
1211
1212 clk = clk_get(&gpio_pdev->dev, NULL);
1213 if (IS_ERR(clk))
1214 return (void *) clk;
1215 clk_prepare(clk);
1216 nmk_chip->clk = clk;
1217
1218 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1219 nmk_gpio_chips[id] = nmk_chip;
1220 return nmk_chip;
1221}
1222
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001223static int nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001224{
Lee Jones513c27f2012-04-13 15:05:05 +01001225 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001226 struct nmk_gpio_chip *nmk_chip;
1227 struct gpio_chip *chip;
Linus Walleij3007d942015-05-06 14:46:40 +02001228 struct irq_chip *irqchip;
Linus Walleij194e15b2014-03-21 10:24:42 +01001229 int latent_irq;
Linus Walleij8f18bcf2014-03-21 10:40:24 +01001230 bool supports_sleepmode;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001231 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001232 int ret;
1233
Linus Walleijbc222ef2015-06-17 15:45:41 +02001234 nmk_chip = nmk_gpio_populate_chip(np, dev);
1235 if (IS_ERR(nmk_chip)) {
1236 dev_err(&dev->dev, "could not populate nmk chip struct\n");
1237 return PTR_ERR(nmk_chip);
1238 }
1239
Linus Walleijf4b3f522013-11-19 23:21:04 +01001240 if (of_get_property(np, "st,supports-sleepmode", NULL))
Linus Walleij8f18bcf2014-03-21 10:40:24 +01001241 supports_sleepmode = true;
1242 else
1243 supports_sleepmode = false;
Linus Walleijf4b3f522013-11-19 23:21:04 +01001244
Linus Walleijbc222ef2015-06-17 15:45:41 +02001245 /* Correct platform device ID */
1246 dev->id = nmk_chip->bank;
Lee Jones513c27f2012-04-13 15:05:05 +01001247
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001248 irq = platform_get_irq(dev, 0);
Linus Walleij50f690d2013-01-07 14:04:56 +01001249 if (irq < 0)
1250 return irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001251
Linus Walleij8f18bcf2014-03-21 10:40:24 +01001252 /* It's OK for this IRQ not to be present */
Linus Walleij194e15b2014-03-21 10:24:42 +01001253 latent_irq = platform_get_irq(dev, 1);
Rabin Vincent33b744b2010-10-14 10:38:03 +05301254
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001255 /*
1256 * The virt address in nmk_chip->addr is in the nomadik register space,
1257 * so we can simply convert the resource address, without remapping
1258 */
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001259 nmk_chip->parent_irq = irq;
Linus Walleij194e15b2014-03-21 10:24:42 +01001260 nmk_chip->latent_parent_irq = latent_irq;
Linus Walleij8f18bcf2014-03-21 10:40:24 +01001261 nmk_chip->sleepmode = supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001262 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001263
1264 chip = &nmk_chip->chip;
Linus Walleij3007d942015-05-06 14:46:40 +02001265 chip->request = nmk_gpio_request;
1266 chip->free = nmk_gpio_free;
1267 chip->direction_input = nmk_gpio_make_input;
1268 chip->get = nmk_gpio_get_input;
1269 chip->direction_output = nmk_gpio_make_output;
1270 chip->set = nmk_gpio_set_output;
1271 chip->dbg_show = nmk_gpio_dbg_show;
1272 chip->can_sleep = false;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001273 chip->owner = THIS_MODULE;
1274
Linus Walleij3007d942015-05-06 14:46:40 +02001275 irqchip = &nmk_chip->irqchip;
1276 irqchip->irq_ack = nmk_gpio_irq_ack;
1277 irqchip->irq_mask = nmk_gpio_irq_mask;
1278 irqchip->irq_unmask = nmk_gpio_irq_unmask;
1279 irqchip->irq_set_type = nmk_gpio_irq_set_type;
1280 irqchip->irq_set_wake = nmk_gpio_irq_set_wake;
1281 irqchip->irq_startup = nmk_gpio_irq_startup;
1282 irqchip->irq_shutdown = nmk_gpio_irq_shutdown;
1283 irqchip->flags = IRQCHIP_MASK_ON_SUSPEND;
1284 irqchip->name = kasprintf(GFP_KERNEL, "nmk%u-%u-%u",
1285 dev->id,
1286 chip->base,
1287 chip->base + chip->ngpio - 1);
1288
Rabin Vincentebc61782011-09-28 15:49:11 +05301289 clk_enable(nmk_chip->clk);
1290 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1291 clk_disable(nmk_chip->clk);
Lee Jones513c27f2012-04-13 15:05:05 +01001292 chip->of_node = np;
1293
Linus Walleijbc222ef2015-06-17 15:45:41 +02001294 ret = gpiochip_add(chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001295 if (ret)
Linus Walleij50f690d2013-01-07 14:04:56 +01001296 return ret;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001297
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001298 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001299
Linus Walleije0bc34a2014-03-25 10:44:09 +01001300 /*
1301 * Let the generic code handle this edge IRQ, the the chained
1302 * handler will perform the actual work of handling the parent
1303 * interrupt.
1304 */
Linus Walleij3007d942015-05-06 14:46:40 +02001305 ret = gpiochip_irqchip_add(chip,
1306 irqchip,
Linus Walleije0bc34a2014-03-25 10:44:09 +01001307 0,
1308 handle_edge_irq,
1309 IRQ_TYPE_EDGE_FALLING);
1310 if (ret) {
1311 dev_err(&dev->dev, "could not add irqchip\n");
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001312 gpiochip_remove(&nmk_chip->chip);
Linus Walleije0bc34a2014-03-25 10:44:09 +01001313 return -ENODEV;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001314 }
Linus Walleije0bc34a2014-03-25 10:44:09 +01001315 /* Then register the chain on the parent IRQ */
Linus Walleij3007d942015-05-06 14:46:40 +02001316 gpiochip_set_chained_irqchip(chip,
1317 irqchip,
Linus Walleije0bc34a2014-03-25 10:44:09 +01001318 nmk_chip->parent_irq,
1319 nmk_gpio_irq_handler);
1320 if (nmk_chip->latent_parent_irq > 0)
Linus Walleij3007d942015-05-06 14:46:40 +02001321 gpiochip_set_chained_irqchip(chip,
1322 irqchip,
Linus Walleije0bc34a2014-03-25 10:44:09 +01001323 nmk_chip->latent_parent_irq,
1324 nmk_gpio_latent_irq_handler);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001325
Lee Jones513c27f2012-04-13 15:05:05 +01001326 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1327
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001328 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001329}
1330
Linus Walleije98ea772012-04-26 23:57:25 +02001331static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1332{
1333 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1334
1335 return npct->soc->ngroups;
1336}
1337
1338static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1339 unsigned selector)
1340{
1341 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1342
1343 return npct->soc->groups[selector].name;
1344}
1345
1346static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1347 const unsigned **pins,
1348 unsigned *num_pins)
1349{
1350 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1351
1352 *pins = npct->soc->groups[selector].pins;
1353 *num_pins = npct->soc->groups[selector].npins;
1354 return 0;
1355}
1356
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001357static struct nmk_gpio_chip *find_nmk_gpio_from_pin(unsigned pin)
Linus Walleij24cbdd72012-05-02 21:28:00 +02001358{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001359 int i;
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001360 struct nmk_gpio_chip *nmk_gpio;
Linus Walleij24cbdd72012-05-02 21:28:00 +02001361
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001362 for(i = 0; i < NMK_MAX_BANKS; i++) {
1363 nmk_gpio = nmk_gpio_chips[i];
1364 if (!nmk_gpio)
1365 continue;
1366 if (pin >= nmk_gpio->chip.base &&
1367 pin < nmk_gpio->chip.base + nmk_gpio->chip.ngpio)
1368 return nmk_gpio;
Linus Walleij24cbdd72012-05-02 21:28:00 +02001369 }
1370 return NULL;
1371}
1372
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001373static struct gpio_chip *find_gc_from_pin(unsigned pin)
1374{
1375 struct nmk_gpio_chip *nmk_gpio = find_nmk_gpio_from_pin(pin);
1376
1377 if (nmk_gpio)
1378 return &nmk_gpio->chip;
1379 return NULL;
1380}
1381
Linus Walleije98ea772012-04-26 23:57:25 +02001382static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1383 unsigned offset)
1384{
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001385 struct gpio_chip *chip = find_gc_from_pin(offset);
Linus Walleij24cbdd72012-05-02 21:28:00 +02001386
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001387 if (!chip) {
Linus Walleij24cbdd72012-05-02 21:28:00 +02001388 seq_printf(s, "invalid pin offset");
1389 return;
1390 }
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001391 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001392}
1393
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001394static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
1395 unsigned *num_maps, const char *group,
1396 const char *function)
1397{
1398 if (*num_maps == *reserved_maps)
1399 return -ENOSPC;
1400
1401 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
1402 (*map)[*num_maps].data.mux.group = group;
1403 (*map)[*num_maps].data.mux.function = function;
1404 (*num_maps)++;
1405
1406 return 0;
1407}
1408
1409static int nmk_dt_add_map_configs(struct pinctrl_map **map,
1410 unsigned *reserved_maps,
1411 unsigned *num_maps, const char *group,
1412 unsigned long *configs, unsigned num_configs)
1413{
1414 unsigned long *dup_configs;
1415
1416 if (*num_maps == *reserved_maps)
1417 return -ENOSPC;
1418
1419 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
1420 GFP_KERNEL);
1421 if (!dup_configs)
1422 return -ENOMEM;
1423
1424 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
1425
1426 (*map)[*num_maps].data.configs.group_or_pin = group;
1427 (*map)[*num_maps].data.configs.configs = dup_configs;
1428 (*map)[*num_maps].data.configs.num_configs = num_configs;
1429 (*num_maps)++;
1430
1431 return 0;
1432}
1433
Sachin Kamat87ff9342013-03-14 17:24:44 +05301434#define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
1435#define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001436 .size = ARRAY_SIZE(y), }
1437
1438static const unsigned long nmk_pin_input_modes[] = {
1439 PIN_INPUT_NOPULL,
1440 PIN_INPUT_PULLUP,
1441 PIN_INPUT_PULLDOWN,
1442};
1443
1444static const unsigned long nmk_pin_output_modes[] = {
1445 PIN_OUTPUT_LOW,
1446 PIN_OUTPUT_HIGH,
1447 PIN_DIR_OUTPUT,
1448};
1449
1450static const unsigned long nmk_pin_sleep_modes[] = {
1451 PIN_SLEEPMODE_DISABLED,
1452 PIN_SLEEPMODE_ENABLED,
1453};
1454
1455static const unsigned long nmk_pin_sleep_input_modes[] = {
1456 PIN_SLPM_INPUT_NOPULL,
1457 PIN_SLPM_INPUT_PULLUP,
1458 PIN_SLPM_INPUT_PULLDOWN,
1459 PIN_SLPM_DIR_INPUT,
1460};
1461
1462static const unsigned long nmk_pin_sleep_output_modes[] = {
1463 PIN_SLPM_OUTPUT_LOW,
1464 PIN_SLPM_OUTPUT_HIGH,
1465 PIN_SLPM_DIR_OUTPUT,
1466};
1467
1468static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
1469 PIN_SLPM_WAKEUP_DISABLE,
1470 PIN_SLPM_WAKEUP_ENABLE,
1471};
1472
1473static const unsigned long nmk_pin_gpio_modes[] = {
1474 PIN_GPIOMODE_DISABLED,
1475 PIN_GPIOMODE_ENABLED,
1476};
1477
1478static const unsigned long nmk_pin_sleep_pdis_modes[] = {
1479 PIN_SLPM_PDIS_DISABLED,
1480 PIN_SLPM_PDIS_ENABLED,
1481};
1482
1483struct nmk_cfg_param {
1484 const char *property;
1485 unsigned long config;
1486 const unsigned long *choice;
1487 int size;
1488};
1489
1490static const struct nmk_cfg_param nmk_cfg_params[] = {
1491 NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes),
1492 NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes),
1493 NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes),
1494 NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes),
1495 NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes),
1496 NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes),
1497 NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes),
1498 NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes),
1499};
1500
1501static int nmk_dt_pin_config(int index, int val, unsigned long *config)
1502{
1503 int ret = 0;
1504
1505 if (nmk_cfg_params[index].choice == NULL)
1506 *config = nmk_cfg_params[index].config;
1507 else {
1508 /* test if out of range */
1509 if (val < nmk_cfg_params[index].size) {
1510 *config = nmk_cfg_params[index].config |
1511 nmk_cfg_params[index].choice[val];
1512 }
1513 }
1514 return ret;
1515}
1516
1517static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
1518{
1519 int i, pin_number;
1520 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1521
1522 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
1523 for (i = 0; i < npct->soc->npins; i++)
1524 if (npct->soc->pins[i].number == pin_number)
1525 return npct->soc->pins[i].name;
1526 return NULL;
1527}
1528
1529static bool nmk_pinctrl_dt_get_config(struct device_node *np,
1530 unsigned long *configs)
1531{
1532 bool has_config = 0;
1533 unsigned long cfg = 0;
1534 int i, val, ret;
1535
1536 for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
1537 ret = of_property_read_u32(np,
1538 nmk_cfg_params[i].property, &val);
1539 if (ret != -EINVAL) {
1540 if (nmk_dt_pin_config(i, val, &cfg) == 0) {
1541 *configs |= cfg;
1542 has_config = 1;
1543 }
1544 }
1545 }
1546
1547 return has_config;
1548}
1549
Sachin Kamat2230a36e2013-06-18 14:34:25 +05301550static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001551 struct device_node *np,
1552 struct pinctrl_map **map,
1553 unsigned *reserved_maps,
1554 unsigned *num_maps)
1555{
1556 int ret;
1557 const char *function = NULL;
1558 unsigned long configs = 0;
1559 bool has_config = 0;
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001560 struct property *prop;
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001561 struct device_node *np_config;
1562
Linus Walleij68d41f22014-09-29 17:21:56 +02001563 ret = of_property_read_string(np, "function", &function);
Linus Walleijc2f6d052014-09-29 16:54:14 +02001564 if (ret >= 0) {
Linus Walleij68d41f22014-09-29 17:21:56 +02001565 const char *group;
1566
1567 ret = of_property_count_strings(np, "groups");
Linus Walleijc2f6d052014-09-29 16:54:14 +02001568 if (ret < 0)
1569 goto exit;
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001570
Linus Walleijc2f6d052014-09-29 16:54:14 +02001571 ret = pinctrl_utils_reserve_map(pctldev, map,
1572 reserved_maps,
1573 num_maps, ret);
1574 if (ret < 0)
1575 goto exit;
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001576
Linus Walleij68d41f22014-09-29 17:21:56 +02001577 of_property_for_each_string(np, "groups", prop, group) {
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001578 ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
1579 group, function);
1580 if (ret < 0)
1581 goto exit;
1582 }
Linus Walleijc2f6d052014-09-29 16:54:14 +02001583 }
1584
1585 has_config = nmk_pinctrl_dt_get_config(np, &configs);
1586 np_config = of_parse_phandle(np, "ste,config", 0);
1587 if (np_config)
1588 has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
1589 if (has_config) {
Linus Walleij68d41f22014-09-29 17:21:56 +02001590 const char *gpio_name;
1591 const char *pin;
1592
Linus Walleij1637d482014-09-30 12:16:25 +02001593 ret = of_property_count_strings(np, "pins");
Linus Walleijc2f6d052014-09-29 16:54:14 +02001594 if (ret < 0)
1595 goto exit;
1596 ret = pinctrl_utils_reserve_map(pctldev, map,
1597 reserved_maps,
1598 num_maps, ret);
1599 if (ret < 0)
1600 goto exit;
1601
Linus Walleij1637d482014-09-30 12:16:25 +02001602 of_property_for_each_string(np, "pins", prop, pin) {
Linus Walleij68d41f22014-09-29 17:21:56 +02001603 gpio_name = nmk_find_pin_name(pctldev, pin);
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001604
Linus Walleijc2f6d052014-09-29 16:54:14 +02001605 ret = nmk_dt_add_map_configs(map, reserved_maps,
1606 num_maps,
1607 gpio_name, &configs, 1);
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001608 if (ret < 0)
1609 goto exit;
1610 }
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001611 }
Linus Walleijc2f6d052014-09-29 16:54:14 +02001612
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001613exit:
1614 return ret;
1615}
1616
Sachin Kamat2230a36e2013-06-18 14:34:25 +05301617static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001618 struct device_node *np_config,
1619 struct pinctrl_map **map, unsigned *num_maps)
1620{
1621 unsigned reserved_maps;
1622 struct device_node *np;
1623 int ret;
1624
1625 reserved_maps = 0;
1626 *map = NULL;
1627 *num_maps = 0;
1628
1629 for_each_child_of_node(np_config, np) {
1630 ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
1631 &reserved_maps, num_maps);
1632 if (ret < 0) {
Linus Walleij6e9b1c32014-09-29 15:22:20 +02001633 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001634 return ret;
1635 }
1636 }
1637
1638 return 0;
1639}
1640
Laurent Pinchart022ab142013-02-16 10:25:07 +01001641static const struct pinctrl_ops nmk_pinctrl_ops = {
Linus Walleije98ea772012-04-26 23:57:25 +02001642 .get_groups_count = nmk_get_groups_cnt,
1643 .get_group_name = nmk_get_group_name,
1644 .get_group_pins = nmk_get_group_pins,
1645 .pin_dbg_show = nmk_pin_dbg_show,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001646 .dt_node_to_map = nmk_pinctrl_dt_node_to_map,
Linus Walleij6e9b1c32014-09-29 15:22:20 +02001647 .dt_free_map = pinctrl_utils_dt_free_map,
Linus Walleije98ea772012-04-26 23:57:25 +02001648};
1649
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001650static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1651{
1652 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1653
1654 return npct->soc->nfunctions;
1655}
1656
1657static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1658 unsigned function)
1659{
1660 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1661
1662 return npct->soc->functions[function].name;
1663}
1664
1665static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1666 unsigned function,
1667 const char * const **groups,
1668 unsigned * const num_groups)
1669{
1670 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1671
1672 *groups = npct->soc->functions[function].groups;
1673 *num_groups = npct->soc->functions[function].ngroups;
1674
1675 return 0;
1676}
1677
Linus Walleij03e9f0c2014-09-03 13:02:56 +02001678static int nmk_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
1679 unsigned group)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001680{
1681 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1682 const struct nmk_pingroup *g;
1683 static unsigned int slpm[NUM_BANKS];
Linus Walleijf84b4172013-08-15 21:26:26 +02001684 unsigned long flags = 0;
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001685 bool glitch;
1686 int ret = -EINVAL;
1687 int i;
1688
1689 g = &npct->soc->groups[group];
1690
1691 if (g->altsetting < 0)
1692 return -EINVAL;
1693
1694 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1695
Linus Walleijdaf73172012-05-22 11:46:45 +02001696 /*
1697 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1698 * we may pass through an undesired state. In this case we take
1699 * some extra care.
1700 *
1701 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1702 * - Save SLPM registers (since we have a shadow register in the
1703 * nmk_chip we're using that as backup)
1704 * - Set SLPM=0 for the IOs you want to switch and others to 1
1705 * - Configure the GPIO registers for the IOs that are being switched
1706 * - Set IOFORCE=1
1707 * - Modify the AFLSA/B registers for the IOs that are being switched
1708 * - Set IOFORCE=0
1709 * - Restore SLPM registers
1710 * - Any spurious wake up event during switch sequence to be ignored
1711 * and cleared
1712 *
1713 * We REALLY need to save ALL slpm registers, because the external
1714 * IOFORCE will switch *all* ports to their sleepmode setting to as
1715 * to avoid glitches. (Not just one port!)
1716 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001717 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001718
1719 if (glitch) {
1720 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1721
1722 /* Initially don't put any pins to sleep when switching */
1723 memset(slpm, 0xff, sizeof(slpm));
1724
1725 /*
1726 * Then mask the pins that need to be sleeping now when we're
1727 * switching to the ALT C function.
1728 */
1729 for (i = 0; i < g->npins; i++)
1730 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1731 nmk_gpio_glitch_slpm_init(slpm);
1732 }
1733
1734 for (i = 0; i < g->npins; i++) {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001735 struct nmk_gpio_chip *nmk_chip;
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001736 unsigned bit;
1737
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001738 nmk_chip = find_nmk_gpio_from_pin(g->pins[i]);
1739 if (!nmk_chip) {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001740 dev_err(npct->dev,
1741 "invalid pin offset %d in group %s at index %d\n",
1742 g->pins[i], g->name, i);
1743 goto out_glitch;
1744 }
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001745 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1746
1747 clk_enable(nmk_chip->clk);
1748 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1749 /*
1750 * If the pin is switching to altfunc, and there was an
1751 * interrupt installed on it which has been lazy disabled,
1752 * actually mask the interrupt to prevent spurious interrupts
1753 * that would occur while the pin is under control of the
1754 * peripheral. Only SKE does this.
1755 */
1756 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1757
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001758 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1759 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001760 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001761
1762 /*
1763 * Call PRCM GPIOCR config function in case ALTC
1764 * has been selected:
1765 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1766 * must be set.
1767 * - If selection is pure ALTC and previous selection was ALTCx,
1768 * then some bits in PRCM GPIOCR registers must be cleared.
1769 */
1770 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1771 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1772 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001773 }
1774
1775 /* When all pins are successfully reconfigured we get here */
1776 ret = 0;
1777
1778out_glitch:
1779 if (glitch) {
1780 nmk_gpio_glitch_slpm_restore(slpm);
1781 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1782 }
1783
1784 return ret;
1785}
1786
Axel Lin5212d092012-11-16 00:01:35 +08001787static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1788 struct pinctrl_gpio_range *range,
1789 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001790{
1791 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1792 struct nmk_gpio_chip *nmk_chip;
1793 struct gpio_chip *chip;
1794 unsigned bit;
1795
1796 if (!range) {
1797 dev_err(npct->dev, "invalid range\n");
1798 return -EINVAL;
1799 }
1800 if (!range->gc) {
1801 dev_err(npct->dev, "missing GPIO chip in range\n");
1802 return -EINVAL;
1803 }
1804 chip = range->gc;
1805 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1806
1807 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1808
1809 clk_enable(nmk_chip->clk);
1810 bit = offset % NMK_GPIO_PER_CHIP;
1811 /* There is no glitch when converting any pin to GPIO */
1812 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1813 clk_disable(nmk_chip->clk);
1814
1815 return 0;
1816}
1817
Axel Lin5212d092012-11-16 00:01:35 +08001818static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
1819 struct pinctrl_gpio_range *range,
1820 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001821{
1822 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1823
1824 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1825 /* Set the pin to some default state, GPIO is usually default */
1826}
1827
Laurent Pinchart022ab142013-02-16 10:25:07 +01001828static const struct pinmux_ops nmk_pinmux_ops = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001829 .get_functions_count = nmk_pmx_get_funcs_cnt,
1830 .get_function_name = nmk_pmx_get_func_name,
1831 .get_function_groups = nmk_pmx_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +02001832 .set_mux = nmk_pmx_set,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001833 .gpio_request_enable = nmk_gpio_request_enable,
1834 .gpio_disable_free = nmk_gpio_disable_free,
Linus Walleija21763a2015-05-06 14:43:45 +02001835 .strict = true,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001836};
1837
Axel Lin5212d092012-11-16 00:01:35 +08001838static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1839 unsigned long *config)
Linus Walleijd41af622012-05-03 15:58:12 +02001840{
1841 /* Not implemented */
1842 return -EINVAL;
1843}
1844
Axel Lin5212d092012-11-16 00:01:35 +08001845static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
Sherman Yin03b054e2013-08-27 11:32:12 -07001846 unsigned long *configs, unsigned num_configs)
Linus Walleijd41af622012-05-03 15:58:12 +02001847{
1848 static const char *pullnames[] = {
1849 [NMK_GPIO_PULL_NONE] = "none",
1850 [NMK_GPIO_PULL_UP] = "up",
1851 [NMK_GPIO_PULL_DOWN] = "down",
1852 [3] /* illegal */ = "??"
1853 };
1854 static const char *slpmnames[] = {
1855 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1856 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1857 };
1858 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1859 struct nmk_gpio_chip *nmk_chip;
Linus Walleijd41af622012-05-03 15:58:12 +02001860 unsigned bit;
Sherman Yin03b054e2013-08-27 11:32:12 -07001861 pin_cfg_t cfg;
1862 int pull, slpm, output, val, i;
1863 bool lowemi, gpiomode, sleep;
Linus Walleijd41af622012-05-03 15:58:12 +02001864
Linus Walleij6ca7d2e2015-06-17 16:05:47 +02001865 nmk_chip = find_nmk_gpio_from_pin(pin);
1866 if (!nmk_chip) {
1867 dev_err(npct->dev,
1868 "invalid pin offset %d\n", pin);
Linus Walleijd41af622012-05-03 15:58:12 +02001869 return -EINVAL;
1870 }
Linus Walleijd41af622012-05-03 15:58:12 +02001871
Sherman Yin03b054e2013-08-27 11:32:12 -07001872 for (i = 0; i < num_configs; i++) {
Linus Walleijd41af622012-05-03 15:58:12 +02001873 /*
Sherman Yin03b054e2013-08-27 11:32:12 -07001874 * The pin config contains pin number and altfunction fields,
1875 * here we just ignore that part. It's being handled by the
1876 * framework and pinmux callback respectively.
Linus Walleijd41af622012-05-03 15:58:12 +02001877 */
Sherman Yin03b054e2013-08-27 11:32:12 -07001878 cfg = (pin_cfg_t) configs[i];
1879 pull = PIN_PULL(cfg);
1880 slpm = PIN_SLPM(cfg);
1881 output = PIN_DIR(cfg);
1882 val = PIN_VAL(cfg);
1883 lowemi = PIN_LOWEMI(cfg);
1884 gpiomode = PIN_GPIOMODE(cfg);
1885 sleep = PIN_SLEEPMODE(cfg);
Linus Walleijd41af622012-05-03 15:58:12 +02001886
Sherman Yin03b054e2013-08-27 11:32:12 -07001887 if (sleep) {
1888 int slpm_pull = PIN_SLPM_PULL(cfg);
1889 int slpm_output = PIN_SLPM_DIR(cfg);
1890 int slpm_val = PIN_SLPM_VAL(cfg);
Linus Walleijd41af622012-05-03 15:58:12 +02001891
Sherman Yin03b054e2013-08-27 11:32:12 -07001892 /* All pins go into GPIO mode at sleep */
1893 gpiomode = true;
Linus Walleijd41af622012-05-03 15:58:12 +02001894
Sherman Yin03b054e2013-08-27 11:32:12 -07001895 /*
1896 * The SLPM_* values are normal values + 1 to allow zero
1897 * to mean "same as normal".
1898 */
1899 if (slpm_pull)
1900 pull = slpm_pull - 1;
1901 if (slpm_output)
1902 output = slpm_output - 1;
1903 if (slpm_val)
1904 val = slpm_val - 1;
Linus Walleijd41af622012-05-03 15:58:12 +02001905
Sherman Yin03b054e2013-08-27 11:32:12 -07001906 dev_dbg(nmk_chip->chip.dev,
1907 "pin %d: sleep pull %s, dir %s, val %s\n",
1908 pin,
1909 slpm_pull ? pullnames[pull] : "same",
1910 slpm_output ? (output ? "output" : "input")
1911 : "same",
1912 slpm_val ? (val ? "high" : "low") : "same");
1913 }
1914
1915 dev_dbg(nmk_chip->chip.dev,
1916 "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1917 pin, cfg, pullnames[pull], slpmnames[slpm],
1918 output ? "output " : "input",
1919 output ? (val ? "high" : "low") : "",
1920 lowemi ? "on" : "off");
1921
1922 clk_enable(nmk_chip->clk);
1923 bit = pin % NMK_GPIO_PER_CHIP;
1924 if (gpiomode)
1925 /* No glitch when going to GPIO mode */
1926 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1927 if (output)
1928 __nmk_gpio_make_output(nmk_chip, bit, val);
1929 else {
1930 __nmk_gpio_make_input(nmk_chip, bit);
1931 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1932 }
1933 /* TODO: isn't this only applicable on output pins? */
1934 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1935
1936 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1937 clk_disable(nmk_chip->clk);
1938 } /* for each config */
1939
Linus Walleijd41af622012-05-03 15:58:12 +02001940 return 0;
1941}
1942
Laurent Pinchart022ab142013-02-16 10:25:07 +01001943static const struct pinconf_ops nmk_pinconf_ops = {
Linus Walleijd41af622012-05-03 15:58:12 +02001944 .pin_config_get = nmk_pin_config_get,
1945 .pin_config_set = nmk_pin_config_set,
1946};
1947
Linus Walleije98ea772012-04-26 23:57:25 +02001948static struct pinctrl_desc nmk_pinctrl_desc = {
1949 .name = "pinctrl-nomadik",
1950 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001951 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001952 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001953 .owner = THIS_MODULE,
1954};
1955
Lee Jones855f80c2012-05-26 06:09:29 +01001956static const struct of_device_id nmk_pinctrl_match[] = {
1957 {
Lee Jones3fd765a2013-05-22 15:22:59 +01001958 .compatible = "stericsson,stn8815-pinctrl",
Linus Walleij6010d402013-01-05 23:10:09 +01001959 .data = (void *)PINCTRL_NMK_STN8815,
1960 },
1961 {
Lee Jones6b09a832013-05-22 15:23:00 +01001962 .compatible = "stericsson,db8500-pinctrl",
Lee Jones855f80c2012-05-26 06:09:29 +01001963 .data = (void *)PINCTRL_NMK_DB8500,
1964 },
Gabriel Fernandez356d3e42013-01-25 16:39:14 +01001965 {
Lee Jones6b09a832013-05-22 15:23:00 +01001966 .compatible = "stericsson,db8540-pinctrl",
Gabriel Fernandez356d3e42013-01-25 16:39:14 +01001967 .data = (void *)PINCTRL_NMK_DB8540,
1968 },
Lee Jones855f80c2012-05-26 06:09:29 +01001969 {},
1970};
1971
Ulf Hansson131d85b2014-02-12 13:59:38 +01001972#ifdef CONFIG_PM_SLEEP
Ulf Hanssonc003eed2014-02-12 13:59:39 +01001973static int nmk_pinctrl_suspend(struct device *dev)
Julien Delacou8d99b322012-12-11 09:17:47 +01001974{
1975 struct nmk_pinctrl *npct;
1976
Ulf Hanssonc003eed2014-02-12 13:59:39 +01001977 npct = dev_get_drvdata(dev);
Julien Delacou8d99b322012-12-11 09:17:47 +01001978 if (!npct)
1979 return -EINVAL;
1980
1981 return pinctrl_force_sleep(npct->pctl);
1982}
1983
Ulf Hanssonc003eed2014-02-12 13:59:39 +01001984static int nmk_pinctrl_resume(struct device *dev)
Julien Delacou8d99b322012-12-11 09:17:47 +01001985{
1986 struct nmk_pinctrl *npct;
1987
Ulf Hanssonc003eed2014-02-12 13:59:39 +01001988 npct = dev_get_drvdata(dev);
Julien Delacou8d99b322012-12-11 09:17:47 +01001989 if (!npct)
1990 return -EINVAL;
1991
1992 return pinctrl_force_default(npct->pctl);
1993}
Ulf Hansson131d85b2014-02-12 13:59:38 +01001994#endif
Julien Delacou8d99b322012-12-11 09:17:47 +01001995
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001996static int nmk_pinctrl_probe(struct platform_device *pdev)
Linus Walleije98ea772012-04-26 23:57:25 +02001997{
Linus Walleijf4b3f522013-11-19 23:21:04 +01001998 const struct of_device_id *match;
Lee Jones855f80c2012-05-26 06:09:29 +01001999 struct device_node *np = pdev->dev.of_node;
Lee Jones32e67ee2013-01-11 15:45:29 +00002000 struct device_node *prcm_np;
Linus Walleije98ea772012-04-26 23:57:25 +02002001 struct nmk_pinctrl *npct;
Lee Jones855f80c2012-05-26 06:09:29 +01002002 unsigned int version = 0;
Linus Walleije98ea772012-04-26 23:57:25 +02002003 int i;
2004
2005 npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
2006 if (!npct)
2007 return -ENOMEM;
2008
Linus Walleijf4b3f522013-11-19 23:21:04 +01002009 match = of_match_device(nmk_pinctrl_match, &pdev->dev);
2010 if (!match)
2011 return -ENODEV;
2012 version = (unsigned int) match->data;
Lee Jones855f80c2012-05-26 06:09:29 +01002013
Linus Walleije98ea772012-04-26 23:57:25 +02002014 /* Poke in other ASIC variants here */
Linus Walleijf79c5ed2012-08-10 00:43:28 +02002015 if (version == PINCTRL_NMK_STN8815)
2016 nmk_pinctrl_stn8815_init(&npct->soc);
Lee Jones855f80c2012-05-26 06:09:29 +01002017 if (version == PINCTRL_NMK_DB8500)
Linus Walleije98ea772012-04-26 23:57:25 +02002018 nmk_pinctrl_db8500_init(&npct->soc);
Patrice Chotard45a1b532012-07-20 15:45:22 +02002019 if (version == PINCTRL_NMK_DB8540)
2020 nmk_pinctrl_db8540_init(&npct->soc);
Linus Walleije98ea772012-04-26 23:57:25 +02002021
Linus Walleijab4a9362015-06-17 23:10:21 +02002022 /*
2023 * Since we depend on the GPIO chips to provide clock and register base
2024 * for the pin control operations, make sure that we have these
2025 * populated before we continue. Follow the phandles to instantiate
2026 * them. The GPIO portion of the actual hardware may be probed before
2027 * or after this point: it shouldn't matter as the APIs are orthogonal.
2028 */
2029 for (i = 0; i < NMK_MAX_BANKS; i++) {
2030 struct device_node *gpio_np;
2031 struct nmk_gpio_chip *nmk_chip;
2032
2033 gpio_np = of_parse_phandle(np, "nomadik-gpio-chips", i);
2034 if (gpio_np) {
2035 dev_info(&pdev->dev,
2036 "populate NMK GPIO %d \"%s\"\n",
2037 i, gpio_np->name);
2038 nmk_chip = nmk_gpio_populate_chip(gpio_np, pdev);
2039 if (IS_ERR(nmk_chip))
2040 dev_err(&pdev->dev,
2041 "could not populate nmk chip struct "
2042 "- continue anyway\n");
2043 of_node_put(gpio_np);
2044 }
2045 }
2046
Linus Walleijf4b3f522013-11-19 23:21:04 +01002047 prcm_np = of_parse_phandle(np, "prcm", 0);
2048 if (prcm_np)
2049 npct->prcm_base = of_iomap(prcm_np, 0);
Lee Jones32e67ee2013-01-11 15:45:29 +00002050 if (!npct->prcm_base) {
2051 if (version == PINCTRL_NMK_STN8815) {
2052 dev_info(&pdev->dev,
2053 "No PRCM base, "
2054 "assuming no ALT-Cx control is available\n");
2055 } else {
2056 dev_err(&pdev->dev, "missing PRCM base address\n");
2057 return -EINVAL;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02002058 }
Jonas Aabergf1671bf2012-10-25 08:40:42 +02002059 }
2060
Linus Walleije98ea772012-04-26 23:57:25 +02002061 nmk_pinctrl_desc.pins = npct->soc->pins;
2062 nmk_pinctrl_desc.npins = npct->soc->npins;
2063 npct->dev = &pdev->dev;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02002064
Linus Walleije98ea772012-04-26 23:57:25 +02002065 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002066 if (IS_ERR(npct->pctl)) {
Linus Walleije98ea772012-04-26 23:57:25 +02002067 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09002068 return PTR_ERR(npct->pctl);
Linus Walleije98ea772012-04-26 23:57:25 +02002069 }
2070
Linus Walleije98ea772012-04-26 23:57:25 +02002071 platform_set_drvdata(pdev, npct);
2072 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
2073
2074 return 0;
2075}
2076
Lee Jones513c27f2012-04-13 15:05:05 +01002077static const struct of_device_id nmk_gpio_match[] = {
2078 { .compatible = "st,nomadik-gpio", },
2079 {}
2080};
2081
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01002082static struct platform_driver nmk_gpio_driver = {
2083 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002084 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01002085 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05302086 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002087 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002088};
2089
Ulf Hanssonc003eed2014-02-12 13:59:39 +01002090static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops,
2091 nmk_pinctrl_suspend,
2092 nmk_pinctrl_resume);
2093
Linus Walleije98ea772012-04-26 23:57:25 +02002094static struct platform_driver nmk_pinctrl_driver = {
2095 .driver = {
Linus Walleije98ea772012-04-26 23:57:25 +02002096 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01002097 .of_match_table = nmk_pinctrl_match,
Ulf Hanssonc003eed2014-02-12 13:59:39 +01002098 .pm = &nmk_pinctrl_pm_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02002099 },
2100 .probe = nmk_pinctrl_probe,
Linus Walleije98ea772012-04-26 23:57:25 +02002101};
2102
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002103static int __init nmk_gpio_init(void)
2104{
Linus Walleij802bb9b2015-07-22 19:21:13 +02002105 return platform_driver_register(&nmk_gpio_driver);
2106}
2107subsys_initcall(nmk_gpio_init);
Linus Walleije98ea772012-04-26 23:57:25 +02002108
Linus Walleij802bb9b2015-07-22 19:21:13 +02002109static int __init nmk_pinctrl_init(void)
2110{
Linus Walleije98ea772012-04-26 23:57:25 +02002111 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002112}
Linus Walleij802bb9b2015-07-22 19:21:13 +02002113core_initcall(nmk_pinctrl_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01002114
2115MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
2116MODULE_DESCRIPTION("Nomadik GPIO Driver");
2117MODULE_LICENSE("GPL");