blob: a8b8a5377b77a43c9112fe8b1124a222187c1a8b [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>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000026#include <linux/irqchip/chained_irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Lee Jones855f80c2012-05-26 06:09:29 +010028#include <linux/of_device.h>
Lee Jones32e67ee2013-01-11 15:45:29 +000029#include <linux/of_address.h>
Gabriel Fernandeze32af882012-12-17 15:53:24 +010030#include <linux/pinctrl/machine.h>
Linus Walleije98ea772012-04-26 23:57:25 +020031#include <linux/pinctrl/pinctrl.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020032#include <linux/pinctrl/pinmux.h>
Linus Walleijd41af622012-05-03 15:58:12 +020033#include <linux/pinctrl/pinconf.h>
Linus Walleijdbfe8ca2012-05-02 22:56:47 +020034/* Since we request GPIOs from ourself */
35#include <linux/pinctrl/consumer.h>
Linus Walleijbb16bd92012-10-10 14:27:58 +020036#include <linux/platform_data/pinctrl-nomadik.h>
Linus Walleije98ea772012-04-26 23:57:25 +020037#include "pinctrl-nomadik.h"
Julien Delacou8d99b322012-12-11 09:17:47 +010038#include "core.h"
Linus Walleije98ea772012-04-26 23:57:25 +020039
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010040/*
41 * The GPIO module in the Nomadik family of Systems-on-Chip is an
42 * AMBA device, managing 32 pins and alternate functions. The logic block
Jonas Aaberg9c66ee62010-10-13 13:14:17 +020043 * is currently used in the Nomadik and ux500.
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010044 *
45 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
46 */
47
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010048struct nmk_gpio_chip {
49 struct gpio_chip chip;
Lee Jonesa60b57e2012-04-19 21:36:31 +010050 struct irq_domain *domain;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010051 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010052 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +053053 unsigned int bank;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010054 unsigned int parent_irq;
Virupax Sadashivpetimath2c8bb0e2010-11-11 14:10:38 +053055 int secondary_parent_irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +053056 u32 (*get_secondary_status)(unsigned int bank);
Rabin Vincent01727e62010-12-13 12:02:40 +053057 void (*set_ioforce)(bool enable);
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010058 spinlock_t lock;
Linus Walleij33d78642011-06-09 11:08:47 +020059 bool sleepmode;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010060 /* Keep track of configured edges */
61 u32 edge_rising;
62 u32 edge_falling;
Rabin Vincentb9df4682011-02-10 11:45:58 +053063 u32 real_wake;
64 u32 rwimsc;
65 u32 fwimsc;
Rabin Vincent6c12fe82011-05-23 12:13:33 +053066 u32 rimsc;
67 u32 fimsc;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +020068 u32 pull_up;
Rabin Vincentebc61782011-09-28 15:49:11 +053069 u32 lowemi;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010070};
71
Jonas Aabergf1671bf2012-10-25 08:40:42 +020072/**
73 * struct nmk_pinctrl - state container for the Nomadik pin controller
74 * @dev: containing device pointer
75 * @pctl: corresponding pin controller device
76 * @soc: SoC data for this specific chip
77 * @prcm_base: PRCM register range virtual base
78 */
Linus Walleije98ea772012-04-26 23:57:25 +020079struct nmk_pinctrl {
80 struct device *dev;
81 struct pinctrl_dev *pctl;
82 const struct nmk_pinctrl_soc_data *soc;
Jonas Aabergf1671bf2012-10-25 08:40:42 +020083 void __iomem *prcm_base;
Linus Walleije98ea772012-04-26 23:57:25 +020084};
85
Rabin Vincent01727e62010-12-13 12:02:40 +053086static struct nmk_gpio_chip *
87nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
88
89static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
90
91#define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
92
Rabin Vincent6f9a9742010-06-02 05:50:28 +010093static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
94 unsigned offset, int gpio_mode)
95{
96 u32 bit = 1 << offset;
97 u32 afunc, bfunc;
98
99 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
100 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
101 if (gpio_mode & NMK_GPIO_ALT_A)
102 afunc |= bit;
103 if (gpio_mode & NMK_GPIO_ALT_B)
104 bfunc |= bit;
105 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
106 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
107}
108
Rabin Vincent81a3c292010-05-27 12:39:23 +0100109static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
110 unsigned offset, enum nmk_gpio_slpm mode)
111{
112 u32 bit = 1 << offset;
113 u32 slpm;
114
115 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
116 if (mode == NMK_GPIO_SLPM_NOCHANGE)
117 slpm |= bit;
118 else
119 slpm &= ~bit;
120 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
121}
122
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100123static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
124 unsigned offset, enum nmk_gpio_pull pull)
125{
126 u32 bit = 1 << offset;
127 u32 pdis;
128
129 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200130 if (pull == NMK_GPIO_PULL_NONE) {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100131 pdis |= bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200132 nmk_chip->pull_up &= ~bit;
133 } else {
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100134 pdis &= ~bit;
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200135 }
136
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100137 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
138
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200139 if (pull == NMK_GPIO_PULL_UP) {
140 nmk_chip->pull_up |= bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100141 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200142 } else if (pull == NMK_GPIO_PULL_DOWN) {
143 nmk_chip->pull_up &= ~bit;
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100144 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200145 }
Rabin Vincent5b327ed2010-05-27 12:29:50 +0100146}
147
Rabin Vincentebc61782011-09-28 15:49:11 +0530148static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
149 unsigned offset, bool lowemi)
150{
151 u32 bit = BIT(offset);
152 bool enabled = nmk_chip->lowemi & bit;
153
154 if (lowemi == enabled)
155 return;
156
157 if (lowemi)
158 nmk_chip->lowemi |= bit;
159 else
160 nmk_chip->lowemi &= ~bit;
161
162 writel_relaxed(nmk_chip->lowemi,
163 nmk_chip->addr + NMK_GPIO_LOWEMI);
164}
165
Rabin Vincent378be062010-06-02 06:06:29 +0100166static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
167 unsigned offset)
168{
169 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
170}
171
Rabin Vincent6720db72010-09-02 11:28:48 +0100172static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
173 unsigned offset, int val)
174{
175 if (val)
176 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
177 else
178 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
179}
180
181static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
182 unsigned offset, int val)
183{
184 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
185 __nmk_gpio_set_output(nmk_chip, offset, val);
186}
187
Rabin Vincent01727e62010-12-13 12:02:40 +0530188static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
189 unsigned offset, int gpio_mode,
190 bool glitch)
191{
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530192 u32 rwimsc = nmk_chip->rwimsc;
193 u32 fwimsc = nmk_chip->fwimsc;
Rabin Vincent01727e62010-12-13 12:02:40 +0530194
195 if (glitch && nmk_chip->set_ioforce) {
196 u32 bit = BIT(offset);
197
Rabin Vincent01727e62010-12-13 12:02:40 +0530198 /* Prevent spurious wakeups */
199 writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
200 writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
201
202 nmk_chip->set_ioforce(true);
203 }
204
205 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
206
207 if (glitch && nmk_chip->set_ioforce) {
208 nmk_chip->set_ioforce(false);
209
210 writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
211 writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
212 }
213}
214
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530215static void
216nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
217{
218 u32 falling = nmk_chip->fimsc & BIT(offset);
219 u32 rising = nmk_chip->rimsc & BIT(offset);
220 int gpio = nmk_chip->chip.base + offset;
Linus Walleijaa6e3792013-01-07 14:54:39 +0100221 int irq = irq_find_mapping(nmk_chip->domain, offset);
Rabin Vincent6c42ad12011-05-23 12:22:18 +0530222 struct irq_data *d = irq_get_irq_data(irq);
223
224 if (!rising && !falling)
225 return;
226
227 if (!d || !irqd_irq_disabled(d))
228 return;
229
230 if (rising) {
231 nmk_chip->rimsc &= ~BIT(offset);
232 writel_relaxed(nmk_chip->rimsc,
233 nmk_chip->addr + NMK_GPIO_RIMSC);
234 }
235
236 if (falling) {
237 nmk_chip->fimsc &= ~BIT(offset);
238 writel_relaxed(nmk_chip->fimsc,
239 nmk_chip->addr + NMK_GPIO_FIMSC);
240 }
241
242 dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
243}
244
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200245static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
246{
247 u32 val;
248
249 val = readl(reg);
250 val = ((val & ~mask) | (value & mask));
251 writel(val, reg);
252}
253
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200254static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
255 unsigned offset, unsigned alt_num)
256{
257 int i;
258 u16 reg;
259 u8 bit;
260 u8 alt_index;
261 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
262 const u16 *gpiocr_regs;
263
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100264 if (!npct->prcm_base)
265 return;
266
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200267 if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
268 dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
269 alt_num);
270 return;
271 }
272
273 for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
274 if (npct->soc->altcx_pins[i].pin == offset)
275 break;
276 }
277 if (i == npct->soc->npins_altcx) {
278 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
279 offset);
280 return;
281 }
282
283 pin_desc = npct->soc->altcx_pins + i;
284 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
285
286 /*
287 * If alt_num is NULL, just clear current ALTCx selection
288 * to make sure we come back to a pure ALTC selection
289 */
290 if (!alt_num) {
291 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
292 if (pin_desc->altcx[i].used == true) {
293 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
294 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200295 if (readl(npct->prcm_base + reg) & BIT(bit)) {
296 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200297 dev_dbg(npct->dev,
298 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
299 offset, i+1);
300 }
301 }
302 }
303 return;
304 }
305
306 alt_index = alt_num - 1;
307 if (pin_desc->altcx[alt_index].used == false) {
308 dev_warn(npct->dev,
309 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
310 offset, alt_num);
311 return;
312 }
313
314 /*
315 * Check if any other ALTCx functions are activated on this pin
316 * and disable it first.
317 */
318 for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
319 if (i == alt_index)
320 continue;
321 if (pin_desc->altcx[i].used == true) {
322 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
323 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200324 if (readl(npct->prcm_base + reg) & BIT(bit)) {
325 nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200326 dev_dbg(npct->dev,
327 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
328 offset, i+1);
329 }
330 }
331 }
332
333 reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
334 bit = pin_desc->altcx[alt_index].control_bit;
335 dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
336 offset, alt_index+1);
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200337 nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +0200338}
339
Rabin Vincent01727e62010-12-13 12:02:40 +0530340/*
341 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
342 * - Save SLPM registers
343 * - Set SLPM=0 for the IOs you want to switch and others to 1
344 * - Configure the GPIO registers for the IOs that are being switched
345 * - Set IOFORCE=1
346 * - Modify the AFLSA/B registers for the IOs that are being switched
347 * - Set IOFORCE=0
348 * - Restore SLPM registers
349 * - Any spurious wake up event during switch sequence to be ignored and
350 * cleared
351 */
352static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
353{
354 int i;
355
356 for (i = 0; i < NUM_BANKS; i++) {
357 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
358 unsigned int temp = slpm[i];
359
360 if (!chip)
361 break;
362
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200363 clk_enable(chip->clk);
364
Rabin Vincent01727e62010-12-13 12:02:40 +0530365 slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
366 writel(temp, chip->addr + NMK_GPIO_SLPC);
367 }
368}
369
370static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
371{
372 int i;
373
374 for (i = 0; i < NUM_BANKS; i++) {
375 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
376
377 if (!chip)
378 break;
379
380 writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200381
382 clk_disable(chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530383 }
384}
385
Arnd Bergmann0fafd502013-01-25 14:14:30 +0000386static int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200387{
388 int i;
389 u16 reg;
390 u8 bit;
391 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
392 const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
393 const u16 *gpiocr_regs;
394
Fabio Baltieri4ca075d2012-12-18 10:12:11 +0100395 if (!npct->prcm_base)
396 return NMK_GPIO_ALT_C;
397
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200398 for (i = 0; i < npct->soc->npins_altcx; i++) {
399 if (npct->soc->altcx_pins[i].pin == gpio)
400 break;
401 }
402 if (i == npct->soc->npins_altcx)
403 return NMK_GPIO_ALT_C;
404
405 pin_desc = npct->soc->altcx_pins + i;
406 gpiocr_regs = npct->soc->prcm_gpiocr_registers;
407 for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
408 if (pin_desc->altcx[i].used == true) {
409 reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
410 bit = pin_desc->altcx[i].control_bit;
Jonas Aabergf1671bf2012-10-25 08:40:42 +0200411 if (readl(npct->prcm_base + reg) & BIT(bit))
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200412 return NMK_GPIO_ALT_C+i+1;
413 }
414 }
415 return NMK_GPIO_ALT_C;
416}
417
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100418int nmk_gpio_get_mode(int gpio)
419{
420 struct nmk_gpio_chip *nmk_chip;
421 u32 afunc, bfunc, bit;
422
Lee Jonesa60b57e2012-04-19 21:36:31 +0100423 nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100424 if (!nmk_chip)
425 return -EINVAL;
426
Lee Jonesa60b57e2012-04-19 21:36:31 +0100427 bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100428
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200429 clk_enable(nmk_chip->clk);
430
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100431 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
432 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
433
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200434 clk_disable(nmk_chip->clk);
435
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100436 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
437}
438EXPORT_SYMBOL(nmk_gpio_get_mode);
439
440
441/* IRQ functions */
442static inline int nmk_gpio_get_bitmask(int gpio)
443{
Lee Jonesa60b57e2012-04-19 21:36:31 +0100444 return 1 << (gpio % NMK_GPIO_PER_CHIP);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100445}
446
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100447static void nmk_gpio_irq_ack(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100448{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100449 struct nmk_gpio_chip *nmk_chip;
450
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100451 nmk_chip = irq_data_get_irq_chip_data(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100452 if (!nmk_chip)
453 return;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200454
455 clk_enable(nmk_chip->clk);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100456 writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200457 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100458}
459
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100460enum nmk_gpio_irq_type {
461 NORMAL,
462 WAKE,
463};
464
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100465static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100466 int gpio, enum nmk_gpio_irq_type which,
467 bool enable)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100468{
469 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530470 u32 *rimscval;
471 u32 *fimscval;
472 u32 rimscreg;
473 u32 fimscreg;
474
475 if (which == NORMAL) {
476 rimscreg = NMK_GPIO_RIMSC;
477 fimscreg = NMK_GPIO_FIMSC;
478 rimscval = &nmk_chip->rimsc;
479 fimscval = &nmk_chip->fimsc;
480 } else {
481 rimscreg = NMK_GPIO_RWIMSC;
482 fimscreg = NMK_GPIO_FWIMSC;
483 rimscval = &nmk_chip->rwimsc;
484 fimscval = &nmk_chip->fwimsc;
485 }
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100486
487 /* we must individually set/clear the two edges */
488 if (nmk_chip->edge_rising & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100489 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530490 *rimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100491 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530492 *rimscval &= ~bitmask;
493 writel(*rimscval, nmk_chip->addr + rimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100494 }
495 if (nmk_chip->edge_falling & bitmask) {
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100496 if (enable)
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530497 *fimscval |= bitmask;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100498 else
Rabin Vincent6c12fe82011-05-23 12:13:33 +0530499 *fimscval &= ~bitmask;
500 writel(*fimscval, nmk_chip->addr + fimscreg);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100501 }
502}
503
Rabin Vincentb9df4682011-02-10 11:45:58 +0530504static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
505 int gpio, bool on)
506{
Rabin Vincentb982ff02011-04-26 09:03:27 +0530507 /*
508 * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
509 * disabled, since setting SLPM to 1 increases power consumption, and
510 * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
511 */
512 if (nmk_chip->sleepmode && on) {
Linus Walleije85bbc12012-06-12 12:43:06 +0200513 __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
Rabin Vincentb982ff02011-04-26 09:03:27 +0530514 NMK_GPIO_SLPM_WAKEUP_ENABLE);
Linus Walleij33d78642011-06-09 11:08:47 +0200515 }
516
Rabin Vincentb9df4682011-02-10 11:45:58 +0530517 __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
518}
519
520static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100521{
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100522 struct nmk_gpio_chip *nmk_chip;
523 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100524 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100525
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100526 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100527 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100528 if (!nmk_chip)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100529 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100530
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200531 clk_enable(nmk_chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530532 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
533 spin_lock(&nmk_chip->lock);
534
Lee Jonesa60b57e2012-04-19 21:36:31 +0100535 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530536
537 if (!(nmk_chip->real_wake & bitmask))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100538 __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530539
540 spin_unlock(&nmk_chip->lock);
541 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200542 clk_disable(nmk_chip->clk);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100543
544 return 0;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100545}
546
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100547static void nmk_gpio_irq_mask(struct irq_data *d)
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100548{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530549 nmk_gpio_irq_maskunmask(d, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100550}
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100551
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100552static void nmk_gpio_irq_unmask(struct irq_data *d)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100553{
Rabin Vincentb9df4682011-02-10 11:45:58 +0530554 nmk_gpio_irq_maskunmask(d, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100555}
556
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100557static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100558{
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100559 struct nmk_gpio_chip *nmk_chip;
560 unsigned long flags;
Rabin Vincentb9df4682011-02-10 11:45:58 +0530561 u32 bitmask;
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100562
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100563 nmk_chip = irq_data_get_irq_chip_data(d);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100564 if (!nmk_chip)
565 return -EINVAL;
Lee Jonesa60b57e2012-04-19 21:36:31 +0100566 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100567
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200568 clk_enable(nmk_chip->clk);
Rabin Vincent01727e62010-12-13 12:02:40 +0530569 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
570 spin_lock(&nmk_chip->lock);
571
Linus Walleij479a0c72011-09-20 10:50:15 +0200572 if (irqd_irq_disabled(d))
Lee Jonesa60b57e2012-04-19 21:36:31 +0100573 __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530574
575 if (on)
576 nmk_chip->real_wake |= bitmask;
577 else
578 nmk_chip->real_wake &= ~bitmask;
Rabin Vincent01727e62010-12-13 12:02:40 +0530579
580 spin_unlock(&nmk_chip->lock);
581 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200582 clk_disable(nmk_chip->clk);
Rabin Vincent7e3f7e52010-09-02 11:28:05 +0100583
584 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100585}
586
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100587static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100588{
Linus Walleij479a0c72011-09-20 10:50:15 +0200589 bool enabled = !irqd_irq_disabled(d);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200590 bool wake = irqd_is_wakeup_set(d);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100591 struct nmk_gpio_chip *nmk_chip;
592 unsigned long flags;
593 u32 bitmask;
594
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100595 nmk_chip = irq_data_get_irq_chip_data(d);
Lee Jonesa60b57e2012-04-19 21:36:31 +0100596 bitmask = nmk_gpio_get_bitmask(d->hwirq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100597 if (!nmk_chip)
598 return -EINVAL;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100599 if (type & IRQ_TYPE_LEVEL_HIGH)
600 return -EINVAL;
601 if (type & IRQ_TYPE_LEVEL_LOW)
602 return -EINVAL;
603
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200604 clk_enable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100605 spin_lock_irqsave(&nmk_chip->lock, flags);
606
Rabin Vincent7a852d82010-05-06 10:43:55 +0100607 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100608 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100609
Rabin Vincentb9df4682011-02-10 11:45:58 +0530610 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100611 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
Rabin Vincent7a852d82010-05-06 10:43:55 +0100612
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100613 nmk_chip->edge_rising &= ~bitmask;
614 if (type & IRQ_TYPE_EDGE_RISING)
615 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100616
617 nmk_chip->edge_falling &= ~bitmask;
618 if (type & IRQ_TYPE_EDGE_FALLING)
619 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100620
621 if (enabled)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100622 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
Rabin Vincent4d4e20f2010-06-16 06:09:34 +0100623
Rabin Vincentb9df4682011-02-10 11:45:58 +0530624 if (enabled || wake)
Lee Jonesa60b57e2012-04-19 21:36:31 +0100625 __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100626
627 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200628 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100629
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100630 return 0;
631}
632
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200633static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
634{
635 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
636
637 clk_enable(nmk_chip->clk);
638 nmk_gpio_irq_unmask(d);
639 return 0;
640}
641
642static void nmk_gpio_irq_shutdown(struct irq_data *d)
643{
644 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
645
646 nmk_gpio_irq_mask(d);
647 clk_disable(nmk_chip->clk);
648}
649
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100650static struct irq_chip nmk_gpio_irq_chip = {
651 .name = "Nomadik-GPIO",
Lennert Buytenhekf272c002010-11-29 11:16:48 +0100652 .irq_ack = nmk_gpio_irq_ack,
653 .irq_mask = nmk_gpio_irq_mask,
654 .irq_unmask = nmk_gpio_irq_unmask,
655 .irq_set_type = nmk_gpio_irq_set_type,
656 .irq_set_wake = nmk_gpio_irq_set_wake,
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200657 .irq_startup = nmk_gpio_irq_startup,
658 .irq_shutdown = nmk_gpio_irq_shutdown,
Etienne Carriere4921e7452012-08-22 10:44:16 +0200659 .flags = IRQCHIP_MASK_ON_SUSPEND,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100660};
661
Rabin Vincent33b744b2010-10-14 10:38:03 +0530662static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
663 u32 status)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100664{
665 struct nmk_gpio_chip *nmk_chip;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100666 struct irq_chip *host_chip = irq_get_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100667
Will Deaconadfed152011-02-28 10:12:29 +0000668 chained_irq_enter(host_chip, desc);
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100669
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100670 nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530671 while (status) {
672 int bit = __ffs(status);
673
Linus Walleij95f0bc92012-09-27 14:14:09 +0200674 generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
Rabin Vincent33b744b2010-10-14 10:38:03 +0530675 status &= ~BIT(bit);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100676 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100677
Will Deaconadfed152011-02-28 10:12:29 +0000678 chained_irq_exit(host_chip, desc);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100679}
680
Rabin Vincent33b744b2010-10-14 10:38:03 +0530681static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
682{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100683 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200684 u32 status;
685
686 clk_enable(nmk_chip->clk);
687 status = readl(nmk_chip->addr + NMK_GPIO_IS);
688 clk_disable(nmk_chip->clk);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530689
690 __nmk_gpio_irq_handler(irq, desc, status);
691}
692
693static void nmk_gpio_secondary_irq_handler(unsigned int irq,
694 struct irq_desc *desc)
695{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100696 struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530697 u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
698
699 __nmk_gpio_irq_handler(irq, desc, status);
700}
701
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100702static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
703{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100704 irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
705 irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530706
707 if (nmk_chip->secondary_parent_irq >= 0) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100708 irq_set_chained_handler(nmk_chip->secondary_parent_irq,
Rabin Vincent33b744b2010-10-14 10:38:03 +0530709 nmk_gpio_secondary_irq_handler);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100710 irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
Rabin Vincent33b744b2010-10-14 10:38:03 +0530711 }
712
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100713 return 0;
714}
715
716/* I/O Functions */
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200717
718static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
719{
720 /*
721 * Map back to global GPIO space and request muxing, the direction
722 * parameter does not matter for this controller.
723 */
724 int gpio = chip->base + offset;
725
726 return pinctrl_request_gpio(gpio);
727}
728
729static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
730{
731 int gpio = chip->base + offset;
732
733 pinctrl_free_gpio(gpio);
734}
735
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100736static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
737{
738 struct nmk_gpio_chip *nmk_chip =
739 container_of(chip, struct nmk_gpio_chip, chip);
740
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200741 clk_enable(nmk_chip->clk);
742
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100743 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200744
745 clk_disable(nmk_chip->clk);
746
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100747 return 0;
748}
749
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100750static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
751{
752 struct nmk_gpio_chip *nmk_chip =
753 container_of(chip, struct nmk_gpio_chip, chip);
754 u32 bit = 1 << offset;
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200755 int value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100756
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200757 clk_enable(nmk_chip->clk);
758
759 value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
760
761 clk_disable(nmk_chip->clk);
762
763 return value;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100764}
765
766static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
767 int val)
768{
769 struct nmk_gpio_chip *nmk_chip =
770 container_of(chip, struct nmk_gpio_chip, chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100771
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200772 clk_enable(nmk_chip->clk);
773
Rabin Vincent6720db72010-09-02 11:28:48 +0100774 __nmk_gpio_set_output(nmk_chip, offset, val);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200775
776 clk_disable(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100777}
778
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100779static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
780 int val)
781{
782 struct nmk_gpio_chip *nmk_chip =
783 container_of(chip, struct nmk_gpio_chip, chip);
784
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200785 clk_enable(nmk_chip->clk);
786
Rabin Vincent6720db72010-09-02 11:28:48 +0100787 __nmk_gpio_make_output(nmk_chip, offset, val);
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100788
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200789 clk_disable(nmk_chip->clk);
790
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100791 return 0;
792}
793
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100794static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
795{
796 struct nmk_gpio_chip *nmk_chip =
797 container_of(chip, struct nmk_gpio_chip, chip);
798
Linus Walleij268300b2012-10-19 17:06:54 +0200799 return irq_create_mapping(nmk_chip->domain, offset);
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100800}
801
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530802#ifdef CONFIG_DEBUG_FS
803
804#include <linux/seq_file.h>
805
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200806static void nmk_gpio_dbg_show_one(struct seq_file *s,
807 struct pinctrl_dev *pctldev, struct gpio_chip *chip,
808 unsigned offset, unsigned gpio)
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530809{
Linus Walleij6f4350a2012-05-02 21:06:13 +0200810 const char *label = gpiochip_is_requested(chip, offset);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530811 struct nmk_gpio_chip *nmk_chip =
812 container_of(chip, struct nmk_gpio_chip, chip);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200813 int mode;
814 bool is_out;
815 bool pull;
816 u32 bit = 1 << offset;
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530817 const char *modes[] = {
818 [NMK_GPIO_ALT_GPIO] = "gpio",
819 [NMK_GPIO_ALT_A] = "altA",
820 [NMK_GPIO_ALT_B] = "altB",
821 [NMK_GPIO_ALT_C] = "altC",
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200822 [NMK_GPIO_ALT_C+1] = "altC1",
823 [NMK_GPIO_ALT_C+2] = "altC2",
824 [NMK_GPIO_ALT_C+3] = "altC3",
825 [NMK_GPIO_ALT_C+4] = "altC4",
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530826 };
827
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200828 clk_enable(nmk_chip->clk);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200829 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
830 pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
831 mode = nmk_gpio_get_mode(gpio);
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200832 if ((mode == NMK_GPIO_ALT_C) && pctldev)
833 mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200834
Linus Walleij6f4350a2012-05-02 21:06:13 +0200835 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
836 gpio, label ?: "(none)",
837 is_out ? "out" : "in ",
838 chip->get
839 ? (chip->get(chip, offset) ? "hi" : "lo")
840 : "? ",
841 (mode < 0) ? "unknown" : modes[mode],
842 pull ? "pull" : "none");
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530843
Linus Walleij6f4350a2012-05-02 21:06:13 +0200844 if (label && !is_out) {
845 int irq = gpio_to_irq(gpio);
846 struct irq_desc *desc = irq_to_desc(irq);
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200847
Linus Walleij6f4350a2012-05-02 21:06:13 +0200848 /* This races with request_irq(), set_irq_type(),
849 * and set_irq_wake() ... but those are "rare".
850 */
851 if (irq >= 0 && desc->action) {
852 char *trigger;
853 u32 bitmask = nmk_gpio_get_bitmask(gpio);
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200854
Linus Walleij6f4350a2012-05-02 21:06:13 +0200855 if (nmk_chip->edge_rising & bitmask)
856 trigger = "edge-rising";
857 else if (nmk_chip->edge_falling & bitmask)
858 trigger = "edge-falling";
859 else
860 trigger = "edge-undefined";
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200861
Linus Walleij6f4350a2012-05-02 21:06:13 +0200862 seq_printf(s, " irq-%d %s%s",
863 irq, trigger,
864 irqd_is_wakeup_set(&desc->irq_data)
865 ? " wakeup" : "");
Rabin Vincent8ea72a32011-05-24 23:07:09 +0200866 }
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530867 }
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200868 clk_disable(nmk_chip->clk);
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530869}
870
Linus Walleij6f4350a2012-05-02 21:06:13 +0200871static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
872{
873 unsigned i;
874 unsigned gpio = chip->base;
875
876 for (i = 0; i < chip->ngpio; i++, gpio++) {
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200877 nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Linus Walleij6f4350a2012-05-02 21:06:13 +0200878 seq_printf(s, "\n");
879 }
880}
881
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530882#else
Linus Walleij6f4350a2012-05-02 21:06:13 +0200883static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +0200884 struct pinctrl_dev *pctldev,
Linus Walleij6f4350a2012-05-02 21:06:13 +0200885 struct gpio_chip *chip,
886 unsigned offset, unsigned gpio)
887{
888}
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530889#define nmk_gpio_dbg_show NULL
890#endif
891
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100892/* This structure is replicated for each GPIO block allocated at probe time */
893static struct gpio_chip nmk_gpio_template = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +0200894 .request = nmk_gpio_request,
895 .free = nmk_gpio_free,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100896 .direction_input = nmk_gpio_make_input,
897 .get = nmk_gpio_get_input,
898 .direction_output = nmk_gpio_make_output,
899 .set = nmk_gpio_set_output,
Rabin Vincent0d2aec92010-06-16 06:10:43 +0100900 .to_irq = nmk_gpio_to_irq,
Rabin Vincentd0b543c2010-03-04 17:39:05 +0530901 .dbg_show = nmk_gpio_dbg_show,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100902 .can_sleep = 0,
903};
904
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200905void nmk_gpio_clocks_enable(void)
906{
907 int i;
908
909 for (i = 0; i < NUM_BANKS; i++) {
910 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
911
912 if (!chip)
913 continue;
914
915 clk_enable(chip->clk);
916 }
917}
918
919void nmk_gpio_clocks_disable(void)
920{
921 int i;
922
923 for (i = 0; i < NUM_BANKS; i++) {
924 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
925
926 if (!chip)
927 continue;
928
929 clk_disable(chip->clk);
930 }
931}
932
Rabin Vincentb9df4682011-02-10 11:45:58 +0530933/*
934 * Called from the suspend/resume path to only keep the real wakeup interrupts
935 * (those that have had set_irq_wake() called on them) as wakeup interrupts,
936 * and not the rest of the interrupts which we needed to have as wakeups for
937 * cpuidle.
938 *
939 * PM ops are not used since this needs to be done at the end, after all the
940 * other drivers are done with their suspend callbacks.
941 */
942void nmk_gpio_wakeups_suspend(void)
943{
944 int i;
945
946 for (i = 0; i < NUM_BANKS; i++) {
947 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
948
949 if (!chip)
950 break;
951
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200952 clk_enable(chip->clk);
953
Rabin Vincentb9df4682011-02-10 11:45:58 +0530954 writel(chip->rwimsc & chip->real_wake,
955 chip->addr + NMK_GPIO_RWIMSC);
956 writel(chip->fwimsc & chip->real_wake,
957 chip->addr + NMK_GPIO_FWIMSC);
958
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200959 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530960 }
961}
962
963void nmk_gpio_wakeups_resume(void)
964{
965 int i;
966
967 for (i = 0; i < NUM_BANKS; i++) {
968 struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
969
970 if (!chip)
971 break;
972
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200973 clk_enable(chip->clk);
974
Rabin Vincentb9df4682011-02-10 11:45:58 +0530975 writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
976 writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
977
Rabin Vincent3c0227d2011-09-20 10:50:03 +0200978 clk_disable(chip->clk);
Rabin Vincentb9df4682011-02-10 11:45:58 +0530979 }
980}
981
Rickard Anderssonbc6f5cf2011-05-24 23:07:17 +0200982/*
983 * Read the pull up/pull down status.
984 * A bit set in 'pull_up' means that pull up
985 * is selected if pull is enabled in PDIS register.
986 * Note: only pull up/down set via this driver can
987 * be detected due to HW limitations.
988 */
989void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
990{
991 if (gpio_bank < NUM_BANKS) {
992 struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
993
994 if (!chip)
995 return;
996
997 *pull_up = chip->pull_up;
998 }
999}
1000
Axel Lin5212d092012-11-16 00:01:35 +08001001static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
1002 irq_hw_number_t hwirq)
Lee Jonesa60b57e2012-04-19 21:36:31 +01001003{
1004 struct nmk_gpio_chip *nmk_chip = d->host_data;
1005
1006 if (!nmk_chip)
1007 return -EINVAL;
1008
1009 irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
1010 set_irq_flags(irq, IRQF_VALID);
1011 irq_set_chip_data(irq, nmk_chip);
1012 irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
1013
1014 return 0;
1015}
1016
Sachin Kamat2230a36e2013-06-18 14:34:25 +05301017static const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
Lee Jonesa60b57e2012-04-19 21:36:31 +01001018 .map = nmk_gpio_irq_map,
1019 .xlate = irq_domain_xlate_twocell,
1020};
1021
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001022static int nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001023{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001024 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Lee Jones513c27f2012-04-13 15:05:05 +01001025 struct device_node *np = dev->dev.of_node;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001026 struct nmk_gpio_chip *nmk_chip;
1027 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001028 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001029 struct clk *clk;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301030 int secondary_irq;
Linus Walleij8d917712012-04-17 10:15:54 +02001031 void __iomem *base;
Linus Walleij832b6cd2012-10-23 09:50:17 +02001032 int irq_start = 0;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001033 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001034 int ret;
1035
Lee Jones513c27f2012-04-13 15:05:05 +01001036 if (!pdata && !np) {
1037 dev_err(&dev->dev, "No platform data or device tree found\n");
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001038 return -ENODEV;
Lee Jones513c27f2012-04-13 15:05:05 +01001039 }
1040
1041 if (np) {
Linus Walleij5e754f32012-07-03 23:05:14 +02001042 pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
Lee Jones513c27f2012-04-13 15:05:05 +01001043 if (!pdata)
1044 return -ENOMEM;
1045
Lee Jones612e1d52012-06-14 11:27:56 +01001046 if (of_get_property(np, "st,supports-sleepmode", NULL))
Lee Jones513c27f2012-04-13 15:05:05 +01001047 pdata->supports_sleepmode = true;
1048
1049 if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
1050 dev_err(&dev->dev, "gpio-bank property not found\n");
Linus Walleij50f690d2013-01-07 14:04:56 +01001051 return -EINVAL;
Lee Jones513c27f2012-04-13 15:05:05 +01001052 }
1053
1054 pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
1055 pdata->num_gpio = NMK_GPIO_PER_CHIP;
1056 }
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001057
1058 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Linus Walleij50f690d2013-01-07 14:04:56 +01001059 if (!res)
1060 return -ENOENT;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001061
1062 irq = platform_get_irq(dev, 0);
Linus Walleij50f690d2013-01-07 14:04:56 +01001063 if (irq < 0)
1064 return irq;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001065
Rabin Vincent33b744b2010-10-14 10:38:03 +05301066 secondary_irq = platform_get_irq(dev, 1);
Linus Walleij50f690d2013-01-07 14:04:56 +01001067 if (secondary_irq >= 0 && !pdata->get_secondary_status)
1068 return -EINVAL;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301069
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001070 base = devm_ioremap_resource(&dev->dev, res);
Linus Torvalds06991c22013-02-21 12:05:51 -08001071 if (IS_ERR(base))
1072 return PTR_ERR(base);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001073
Linus Walleij5e754f32012-07-03 23:05:14 +02001074 clk = devm_clk_get(&dev->dev, NULL);
Linus Walleij50f690d2013-01-07 14:04:56 +01001075 if (IS_ERR(clk))
1076 return PTR_ERR(clk);
Linus Walleijefec3812012-06-06 22:50:41 +02001077 clk_prepare(clk);
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001078
Linus Walleij5e754f32012-07-03 23:05:14 +02001079 nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
Linus Walleij50f690d2013-01-07 14:04:56 +01001080 if (!nmk_chip)
1081 return -ENOMEM;
Lee Jones513c27f2012-04-13 15:05:05 +01001082
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001083 /*
1084 * The virt address in nmk_chip->addr is in the nomadik register space,
1085 * so we can simply convert the resource address, without remapping
1086 */
Rabin Vincent33b744b2010-10-14 10:38:03 +05301087 nmk_chip->bank = dev->id;
Rabin Vincentaf7dc222010-05-06 11:14:17 +01001088 nmk_chip->clk = clk;
Linus Walleij8d917712012-04-17 10:15:54 +02001089 nmk_chip->addr = base;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001090 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001091 nmk_chip->parent_irq = irq;
Rabin Vincent33b744b2010-10-14 10:38:03 +05301092 nmk_chip->secondary_parent_irq = secondary_irq;
1093 nmk_chip->get_secondary_status = pdata->get_secondary_status;
Rabin Vincent01727e62010-12-13 12:02:40 +05301094 nmk_chip->set_ioforce = pdata->set_ioforce;
Linus Walleij33d78642011-06-09 11:08:47 +02001095 nmk_chip->sleepmode = pdata->supports_sleepmode;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +01001096 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001097
1098 chip = &nmk_chip->chip;
1099 chip->base = pdata->first_gpio;
Rabin Vincente493e062010-03-18 12:35:22 +05301100 chip->ngpio = pdata->num_gpio;
Rabin Vincent8d568ae2010-12-08 11:07:54 +05301101 chip->label = pdata->name ?: dev_name(&dev->dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001102 chip->dev = &dev->dev;
1103 chip->owner = THIS_MODULE;
1104
Rabin Vincentebc61782011-09-28 15:49:11 +05301105 clk_enable(nmk_chip->clk);
1106 nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
1107 clk_disable(nmk_chip->clk);
1108
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001109#ifdef CONFIG_OF_GPIO
Lee Jones513c27f2012-04-13 15:05:05 +01001110 chip->of_node = np;
Arnd Bergmann072e82a2012-05-10 13:39:52 +02001111#endif
Lee Jones513c27f2012-04-13 15:05:05 +01001112
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001113 ret = gpiochip_add(&nmk_chip->chip);
1114 if (ret)
Linus Walleij50f690d2013-01-07 14:04:56 +01001115 return ret;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001116
Rabin Vincent01727e62010-12-13 12:02:40 +05301117 BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
1118
1119 nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
Lee Jones513c27f2012-04-13 15:05:05 +01001120
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001121 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001122
Linus Walleij51f58c62012-10-11 16:33:44 +02001123 if (!np)
Linus Walleijaa6e3792013-01-07 14:54:39 +01001124 irq_start = pdata->first_irq;
Linus Walleij38843e22012-10-23 11:44:42 +02001125 nmk_chip->domain = irq_domain_add_simple(np,
Linus Walleij6054b9c2012-09-26 19:03:51 +02001126 NMK_GPIO_PER_CHIP, irq_start,
1127 &nmk_gpio_irq_simple_ops, nmk_chip);
Lee Jonesa60b57e2012-04-19 21:36:31 +01001128 if (!nmk_chip->domain) {
Linus Walleij2ee38d42012-08-10 11:07:51 +02001129 dev_err(&dev->dev, "failed to create irqdomain\n");
Linus Walleij50f690d2013-01-07 14:04:56 +01001130 /* Just do this, no matter if it fails */
1131 ret = gpiochip_remove(&nmk_chip->chip);
1132 return -ENOSYS;
Lee Jonesa60b57e2012-04-19 21:36:31 +01001133 }
1134
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001135 nmk_gpio_init_irq(nmk_chip);
1136
Lee Jones513c27f2012-04-13 15:05:05 +01001137 dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
1138
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001139 return 0;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001140}
1141
Linus Walleije98ea772012-04-26 23:57:25 +02001142static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
1143{
1144 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1145
1146 return npct->soc->ngroups;
1147}
1148
1149static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
1150 unsigned selector)
1151{
1152 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1153
1154 return npct->soc->groups[selector].name;
1155}
1156
1157static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
1158 const unsigned **pins,
1159 unsigned *num_pins)
1160{
1161 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1162
1163 *pins = npct->soc->groups[selector].pins;
1164 *num_pins = npct->soc->groups[selector].npins;
1165 return 0;
1166}
1167
Linus Walleij24cbdd72012-05-02 21:28:00 +02001168static struct pinctrl_gpio_range *
1169nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
1170{
1171 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1172 int i;
1173
1174 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
1175 struct pinctrl_gpio_range *range;
1176
1177 range = &npct->soc->gpio_ranges[i];
1178 if (offset >= range->pin_base &&
1179 offset <= (range->pin_base + range->npins - 1))
1180 return range;
1181 }
1182 return NULL;
1183}
1184
Linus Walleije98ea772012-04-26 23:57:25 +02001185static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
1186 unsigned offset)
1187{
Linus Walleij24cbdd72012-05-02 21:28:00 +02001188 struct pinctrl_gpio_range *range;
1189 struct gpio_chip *chip;
1190
1191 range = nmk_match_gpio_range(pctldev, offset);
1192 if (!range || !range->gc) {
1193 seq_printf(s, "invalid pin offset");
1194 return;
1195 }
1196 chip = range->gc;
Jean-Nicolas Graux2249b192012-10-15 09:47:05 +02001197 nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
Linus Walleije98ea772012-04-26 23:57:25 +02001198}
1199
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001200static void nmk_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
1201 struct pinctrl_map *map, unsigned num_maps)
1202{
1203 int i;
1204
1205 for (i = 0; i < num_maps; i++)
1206 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
1207 kfree(map[i].data.configs.configs);
1208 kfree(map);
1209}
1210
1211static int nmk_dt_reserve_map(struct pinctrl_map **map, unsigned *reserved_maps,
1212 unsigned *num_maps, unsigned reserve)
1213{
1214 unsigned old_num = *reserved_maps;
1215 unsigned new_num = *num_maps + reserve;
1216 struct pinctrl_map *new_map;
1217
1218 if (old_num >= new_num)
1219 return 0;
1220
1221 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
1222 if (!new_map)
1223 return -ENOMEM;
1224
1225 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
1226
1227 *map = new_map;
1228 *reserved_maps = new_num;
1229
1230 return 0;
1231}
1232
1233static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
1234 unsigned *num_maps, const char *group,
1235 const char *function)
1236{
1237 if (*num_maps == *reserved_maps)
1238 return -ENOSPC;
1239
1240 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
1241 (*map)[*num_maps].data.mux.group = group;
1242 (*map)[*num_maps].data.mux.function = function;
1243 (*num_maps)++;
1244
1245 return 0;
1246}
1247
1248static int nmk_dt_add_map_configs(struct pinctrl_map **map,
1249 unsigned *reserved_maps,
1250 unsigned *num_maps, const char *group,
1251 unsigned long *configs, unsigned num_configs)
1252{
1253 unsigned long *dup_configs;
1254
1255 if (*num_maps == *reserved_maps)
1256 return -ENOSPC;
1257
1258 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
1259 GFP_KERNEL);
1260 if (!dup_configs)
1261 return -ENOMEM;
1262
1263 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
1264
1265 (*map)[*num_maps].data.configs.group_or_pin = group;
1266 (*map)[*num_maps].data.configs.configs = dup_configs;
1267 (*map)[*num_maps].data.configs.num_configs = num_configs;
1268 (*num_maps)++;
1269
1270 return 0;
1271}
1272
Sachin Kamat87ff9342013-03-14 17:24:44 +05301273#define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
1274#define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001275 .size = ARRAY_SIZE(y), }
1276
1277static const unsigned long nmk_pin_input_modes[] = {
1278 PIN_INPUT_NOPULL,
1279 PIN_INPUT_PULLUP,
1280 PIN_INPUT_PULLDOWN,
1281};
1282
1283static const unsigned long nmk_pin_output_modes[] = {
1284 PIN_OUTPUT_LOW,
1285 PIN_OUTPUT_HIGH,
1286 PIN_DIR_OUTPUT,
1287};
1288
1289static const unsigned long nmk_pin_sleep_modes[] = {
1290 PIN_SLEEPMODE_DISABLED,
1291 PIN_SLEEPMODE_ENABLED,
1292};
1293
1294static const unsigned long nmk_pin_sleep_input_modes[] = {
1295 PIN_SLPM_INPUT_NOPULL,
1296 PIN_SLPM_INPUT_PULLUP,
1297 PIN_SLPM_INPUT_PULLDOWN,
1298 PIN_SLPM_DIR_INPUT,
1299};
1300
1301static const unsigned long nmk_pin_sleep_output_modes[] = {
1302 PIN_SLPM_OUTPUT_LOW,
1303 PIN_SLPM_OUTPUT_HIGH,
1304 PIN_SLPM_DIR_OUTPUT,
1305};
1306
1307static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
1308 PIN_SLPM_WAKEUP_DISABLE,
1309 PIN_SLPM_WAKEUP_ENABLE,
1310};
1311
1312static const unsigned long nmk_pin_gpio_modes[] = {
1313 PIN_GPIOMODE_DISABLED,
1314 PIN_GPIOMODE_ENABLED,
1315};
1316
1317static const unsigned long nmk_pin_sleep_pdis_modes[] = {
1318 PIN_SLPM_PDIS_DISABLED,
1319 PIN_SLPM_PDIS_ENABLED,
1320};
1321
1322struct nmk_cfg_param {
1323 const char *property;
1324 unsigned long config;
1325 const unsigned long *choice;
1326 int size;
1327};
1328
1329static const struct nmk_cfg_param nmk_cfg_params[] = {
1330 NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes),
1331 NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes),
1332 NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes),
1333 NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes),
1334 NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes),
1335 NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes),
1336 NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes),
1337 NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes),
1338};
1339
1340static int nmk_dt_pin_config(int index, int val, unsigned long *config)
1341{
1342 int ret = 0;
1343
1344 if (nmk_cfg_params[index].choice == NULL)
1345 *config = nmk_cfg_params[index].config;
1346 else {
1347 /* test if out of range */
1348 if (val < nmk_cfg_params[index].size) {
1349 *config = nmk_cfg_params[index].config |
1350 nmk_cfg_params[index].choice[val];
1351 }
1352 }
1353 return ret;
1354}
1355
1356static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
1357{
1358 int i, pin_number;
1359 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1360
1361 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
1362 for (i = 0; i < npct->soc->npins; i++)
1363 if (npct->soc->pins[i].number == pin_number)
1364 return npct->soc->pins[i].name;
1365 return NULL;
1366}
1367
1368static bool nmk_pinctrl_dt_get_config(struct device_node *np,
1369 unsigned long *configs)
1370{
1371 bool has_config = 0;
1372 unsigned long cfg = 0;
1373 int i, val, ret;
1374
1375 for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
1376 ret = of_property_read_u32(np,
1377 nmk_cfg_params[i].property, &val);
1378 if (ret != -EINVAL) {
1379 if (nmk_dt_pin_config(i, val, &cfg) == 0) {
1380 *configs |= cfg;
1381 has_config = 1;
1382 }
1383 }
1384 }
1385
1386 return has_config;
1387}
1388
Sachin Kamat2230a36e2013-06-18 14:34:25 +05301389static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001390 struct device_node *np,
1391 struct pinctrl_map **map,
1392 unsigned *reserved_maps,
1393 unsigned *num_maps)
1394{
1395 int ret;
1396 const char *function = NULL;
1397 unsigned long configs = 0;
1398 bool has_config = 0;
1399 unsigned reserve = 0;
1400 struct property *prop;
1401 const char *group, *gpio_name;
1402 struct device_node *np_config;
1403
1404 ret = of_property_read_string(np, "ste,function", &function);
1405 if (ret >= 0)
1406 reserve = 1;
1407
1408 has_config = nmk_pinctrl_dt_get_config(np, &configs);
1409
1410 np_config = of_parse_phandle(np, "ste,config", 0);
1411 if (np_config)
1412 has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
1413
1414 ret = of_property_count_strings(np, "ste,pins");
1415 if (ret < 0)
1416 goto exit;
1417
1418 if (has_config)
1419 reserve++;
1420
1421 reserve *= ret;
1422
1423 ret = nmk_dt_reserve_map(map, reserved_maps, num_maps, reserve);
1424 if (ret < 0)
1425 goto exit;
1426
1427 of_property_for_each_string(np, "ste,pins", prop, group) {
1428 if (function) {
1429 ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
1430 group, function);
1431 if (ret < 0)
1432 goto exit;
1433 }
1434 if (has_config) {
1435 gpio_name = nmk_find_pin_name(pctldev, group);
1436
1437 ret = nmk_dt_add_map_configs(map, reserved_maps, num_maps,
1438 gpio_name, &configs, 1);
1439 if (ret < 0)
1440 goto exit;
1441 }
1442
1443 }
1444exit:
1445 return ret;
1446}
1447
Sachin Kamat2230a36e2013-06-18 14:34:25 +05301448static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001449 struct device_node *np_config,
1450 struct pinctrl_map **map, unsigned *num_maps)
1451{
1452 unsigned reserved_maps;
1453 struct device_node *np;
1454 int ret;
1455
1456 reserved_maps = 0;
1457 *map = NULL;
1458 *num_maps = 0;
1459
1460 for_each_child_of_node(np_config, np) {
1461 ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
1462 &reserved_maps, num_maps);
1463 if (ret < 0) {
1464 nmk_pinctrl_dt_free_map(pctldev, *map, *num_maps);
1465 return ret;
1466 }
1467 }
1468
1469 return 0;
1470}
1471
Laurent Pinchart022ab142013-02-16 10:25:07 +01001472static const struct pinctrl_ops nmk_pinctrl_ops = {
Linus Walleije98ea772012-04-26 23:57:25 +02001473 .get_groups_count = nmk_get_groups_cnt,
1474 .get_group_name = nmk_get_group_name,
1475 .get_group_pins = nmk_get_group_pins,
1476 .pin_dbg_show = nmk_pin_dbg_show,
Gabriel Fernandeze32af882012-12-17 15:53:24 +01001477 .dt_node_to_map = nmk_pinctrl_dt_node_to_map,
1478 .dt_free_map = nmk_pinctrl_dt_free_map,
Linus Walleije98ea772012-04-26 23:57:25 +02001479};
1480
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001481static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
1482{
1483 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1484
1485 return npct->soc->nfunctions;
1486}
1487
1488static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
1489 unsigned function)
1490{
1491 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1492
1493 return npct->soc->functions[function].name;
1494}
1495
1496static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
1497 unsigned function,
1498 const char * const **groups,
1499 unsigned * const num_groups)
1500{
1501 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1502
1503 *groups = npct->soc->functions[function].groups;
1504 *num_groups = npct->soc->functions[function].ngroups;
1505
1506 return 0;
1507}
1508
1509static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
1510 unsigned group)
1511{
1512 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1513 const struct nmk_pingroup *g;
1514 static unsigned int slpm[NUM_BANKS];
Linus Walleijf84b4172013-08-15 21:26:26 +02001515 unsigned long flags = 0;
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001516 bool glitch;
1517 int ret = -EINVAL;
1518 int i;
1519
1520 g = &npct->soc->groups[group];
1521
1522 if (g->altsetting < 0)
1523 return -EINVAL;
1524
1525 dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
1526
Linus Walleijdaf73172012-05-22 11:46:45 +02001527 /*
1528 * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
1529 * we may pass through an undesired state. In this case we take
1530 * some extra care.
1531 *
1532 * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
1533 * - Save SLPM registers (since we have a shadow register in the
1534 * nmk_chip we're using that as backup)
1535 * - Set SLPM=0 for the IOs you want to switch and others to 1
1536 * - Configure the GPIO registers for the IOs that are being switched
1537 * - Set IOFORCE=1
1538 * - Modify the AFLSA/B registers for the IOs that are being switched
1539 * - Set IOFORCE=0
1540 * - Restore SLPM registers
1541 * - Any spurious wake up event during switch sequence to be ignored
1542 * and cleared
1543 *
1544 * We REALLY need to save ALL slpm registers, because the external
1545 * IOFORCE will switch *all* ports to their sleepmode setting to as
1546 * to avoid glitches. (Not just one port!)
1547 */
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001548 glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001549
1550 if (glitch) {
1551 spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
1552
1553 /* Initially don't put any pins to sleep when switching */
1554 memset(slpm, 0xff, sizeof(slpm));
1555
1556 /*
1557 * Then mask the pins that need to be sleeping now when we're
1558 * switching to the ALT C function.
1559 */
1560 for (i = 0; i < g->npins; i++)
1561 slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
1562 nmk_gpio_glitch_slpm_init(slpm);
1563 }
1564
1565 for (i = 0; i < g->npins; i++) {
1566 struct pinctrl_gpio_range *range;
1567 struct nmk_gpio_chip *nmk_chip;
1568 struct gpio_chip *chip;
1569 unsigned bit;
1570
1571 range = nmk_match_gpio_range(pctldev, g->pins[i]);
1572 if (!range) {
1573 dev_err(npct->dev,
1574 "invalid pin offset %d in group %s at index %d\n",
1575 g->pins[i], g->name, i);
1576 goto out_glitch;
1577 }
1578 if (!range->gc) {
1579 dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1580 g->pins[i], g->name, i);
1581 goto out_glitch;
1582 }
1583 chip = range->gc;
1584 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1585 dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
1586
1587 clk_enable(nmk_chip->clk);
1588 bit = g->pins[i] % NMK_GPIO_PER_CHIP;
1589 /*
1590 * If the pin is switching to altfunc, and there was an
1591 * interrupt installed on it which has been lazy disabled,
1592 * actually mask the interrupt to prevent spurious interrupts
1593 * that would occur while the pin is under control of the
1594 * peripheral. Only SKE does this.
1595 */
1596 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1597
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001598 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1599 (g->altsetting & NMK_GPIO_ALT_C), glitch);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001600 clk_disable(nmk_chip->clk);
Jean-Nicolas Grauxc22df082012-09-27 15:38:50 +02001601
1602 /*
1603 * Call PRCM GPIOCR config function in case ALTC
1604 * has been selected:
1605 * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
1606 * must be set.
1607 * - If selection is pure ALTC and previous selection was ALTCx,
1608 * then some bits in PRCM GPIOCR registers must be cleared.
1609 */
1610 if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
1611 nmk_prcm_altcx_set_mode(npct, g->pins[i],
1612 g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001613 }
1614
1615 /* When all pins are successfully reconfigured we get here */
1616 ret = 0;
1617
1618out_glitch:
1619 if (glitch) {
1620 nmk_gpio_glitch_slpm_restore(slpm);
1621 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1622 }
1623
1624 return ret;
1625}
1626
1627static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
1628 unsigned function, unsigned group)
1629{
1630 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1631 const struct nmk_pingroup *g;
1632
1633 g = &npct->soc->groups[group];
1634
1635 if (g->altsetting < 0)
1636 return;
1637
1638 /* Poke out the mux, set the pin to some default state? */
1639 dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
1640}
1641
Axel Lin5212d092012-11-16 00:01:35 +08001642static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
1643 struct pinctrl_gpio_range *range,
1644 unsigned offset)
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001645{
1646 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1647 struct nmk_gpio_chip *nmk_chip;
1648 struct gpio_chip *chip;
1649 unsigned bit;
1650
1651 if (!range) {
1652 dev_err(npct->dev, "invalid range\n");
1653 return -EINVAL;
1654 }
1655 if (!range->gc) {
1656 dev_err(npct->dev, "missing GPIO chip in range\n");
1657 return -EINVAL;
1658 }
1659 chip = range->gc;
1660 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1661
1662 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
1663
1664 clk_enable(nmk_chip->clk);
1665 bit = offset % NMK_GPIO_PER_CHIP;
1666 /* There is no glitch when converting any pin to GPIO */
1667 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1668 clk_disable(nmk_chip->clk);
1669
1670 return 0;
1671}
1672
Axel Lin5212d092012-11-16 00:01:35 +08001673static void nmk_gpio_disable_free(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
1679 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
1680 /* Set the pin to some default state, GPIO is usually default */
1681}
1682
Laurent Pinchart022ab142013-02-16 10:25:07 +01001683static const struct pinmux_ops nmk_pinmux_ops = {
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001684 .get_functions_count = nmk_pmx_get_funcs_cnt,
1685 .get_function_name = nmk_pmx_get_func_name,
1686 .get_function_groups = nmk_pmx_get_func_groups,
1687 .enable = nmk_pmx_enable,
1688 .disable = nmk_pmx_disable,
1689 .gpio_request_enable = nmk_gpio_request_enable,
1690 .gpio_disable_free = nmk_gpio_disable_free,
1691};
1692
Axel Lin5212d092012-11-16 00:01:35 +08001693static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
1694 unsigned long *config)
Linus Walleijd41af622012-05-03 15:58:12 +02001695{
1696 /* Not implemented */
1697 return -EINVAL;
1698}
1699
Axel Lin5212d092012-11-16 00:01:35 +08001700static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1701 unsigned long config)
Linus Walleijd41af622012-05-03 15:58:12 +02001702{
1703 static const char *pullnames[] = {
1704 [NMK_GPIO_PULL_NONE] = "none",
1705 [NMK_GPIO_PULL_UP] = "up",
1706 [NMK_GPIO_PULL_DOWN] = "down",
1707 [3] /* illegal */ = "??"
1708 };
1709 static const char *slpmnames[] = {
1710 [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
1711 [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
1712 };
1713 struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
1714 struct nmk_gpio_chip *nmk_chip;
1715 struct pinctrl_gpio_range *range;
1716 struct gpio_chip *chip;
1717 unsigned bit;
1718
1719 /*
1720 * The pin config contains pin number and altfunction fields, here
1721 * we just ignore that part. It's being handled by the framework and
1722 * pinmux callback respectively.
1723 */
1724 pin_cfg_t cfg = (pin_cfg_t) config;
1725 int pull = PIN_PULL(cfg);
1726 int slpm = PIN_SLPM(cfg);
1727 int output = PIN_DIR(cfg);
1728 int val = PIN_VAL(cfg);
1729 bool lowemi = PIN_LOWEMI(cfg);
1730 bool gpiomode = PIN_GPIOMODE(cfg);
1731 bool sleep = PIN_SLEEPMODE(cfg);
1732
1733 range = nmk_match_gpio_range(pctldev, pin);
1734 if (!range) {
1735 dev_err(npct->dev, "invalid pin offset %d\n", pin);
1736 return -EINVAL;
1737 }
1738 if (!range->gc) {
1739 dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
1740 pin);
1741 return -EINVAL;
1742 }
1743 chip = range->gc;
1744 nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
1745
1746 if (sleep) {
1747 int slpm_pull = PIN_SLPM_PULL(cfg);
1748 int slpm_output = PIN_SLPM_DIR(cfg);
1749 int slpm_val = PIN_SLPM_VAL(cfg);
1750
1751 /* All pins go into GPIO mode at sleep */
1752 gpiomode = true;
1753
1754 /*
1755 * The SLPM_* values are normal values + 1 to allow zero to
1756 * mean "same as normal".
1757 */
1758 if (slpm_pull)
1759 pull = slpm_pull - 1;
1760 if (slpm_output)
1761 output = slpm_output - 1;
1762 if (slpm_val)
1763 val = slpm_val - 1;
1764
1765 dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
1766 pin,
1767 slpm_pull ? pullnames[pull] : "same",
1768 slpm_output ? (output ? "output" : "input") : "same",
1769 slpm_val ? (val ? "high" : "low") : "same");
1770 }
1771
1772 dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1773 pin, cfg, pullnames[pull], slpmnames[slpm],
1774 output ? "output " : "input",
1775 output ? (val ? "high" : "low") : "",
Sachin Kamat87ff9342013-03-14 17:24:44 +05301776 lowemi ? "on" : "off");
Linus Walleijd41af622012-05-03 15:58:12 +02001777
1778 clk_enable(nmk_chip->clk);
1779 bit = pin % NMK_GPIO_PER_CHIP;
1780 if (gpiomode)
1781 /* No glitch when going to GPIO mode */
1782 __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
1783 if (output)
1784 __nmk_gpio_make_output(nmk_chip, bit, val);
1785 else {
1786 __nmk_gpio_make_input(nmk_chip, bit);
1787 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1788 }
1789 /* TODO: isn't this only applicable on output pins? */
1790 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1791
1792 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1793 clk_disable(nmk_chip->clk);
1794 return 0;
1795}
1796
Laurent Pinchart022ab142013-02-16 10:25:07 +01001797static const struct pinconf_ops nmk_pinconf_ops = {
Linus Walleijd41af622012-05-03 15:58:12 +02001798 .pin_config_get = nmk_pin_config_get,
1799 .pin_config_set = nmk_pin_config_set,
1800};
1801
Linus Walleije98ea772012-04-26 23:57:25 +02001802static struct pinctrl_desc nmk_pinctrl_desc = {
1803 .name = "pinctrl-nomadik",
1804 .pctlops = &nmk_pinctrl_ops,
Linus Walleijdbfe8ca2012-05-02 22:56:47 +02001805 .pmxops = &nmk_pinmux_ops,
Linus Walleijd41af622012-05-03 15:58:12 +02001806 .confops = &nmk_pinconf_ops,
Linus Walleije98ea772012-04-26 23:57:25 +02001807 .owner = THIS_MODULE,
1808};
1809
Lee Jones855f80c2012-05-26 06:09:29 +01001810static const struct of_device_id nmk_pinctrl_match[] = {
1811 {
Lee Jones3fd765a2013-05-22 15:22:59 +01001812 .compatible = "stericsson,stn8815-pinctrl",
Linus Walleij6010d402013-01-05 23:10:09 +01001813 .data = (void *)PINCTRL_NMK_STN8815,
1814 },
1815 {
Lee Jones6b09a832013-05-22 15:23:00 +01001816 .compatible = "stericsson,db8500-pinctrl",
Lee Jones855f80c2012-05-26 06:09:29 +01001817 .data = (void *)PINCTRL_NMK_DB8500,
1818 },
Gabriel Fernandez356d3e42013-01-25 16:39:14 +01001819 {
Lee Jones6b09a832013-05-22 15:23:00 +01001820 .compatible = "stericsson,db8540-pinctrl",
Gabriel Fernandez356d3e42013-01-25 16:39:14 +01001821 .data = (void *)PINCTRL_NMK_DB8540,
1822 },
Lee Jones855f80c2012-05-26 06:09:29 +01001823 {},
1824};
1825
Julien Delacou8d99b322012-12-11 09:17:47 +01001826static int nmk_pinctrl_suspend(struct platform_device *pdev, pm_message_t state)
1827{
1828 struct nmk_pinctrl *npct;
1829
1830 npct = platform_get_drvdata(pdev);
1831 if (!npct)
1832 return -EINVAL;
1833
1834 return pinctrl_force_sleep(npct->pctl);
1835}
1836
1837static int nmk_pinctrl_resume(struct platform_device *pdev)
1838{
1839 struct nmk_pinctrl *npct;
1840
1841 npct = platform_get_drvdata(pdev);
1842 if (!npct)
1843 return -EINVAL;
1844
1845 return pinctrl_force_default(npct->pctl);
1846}
1847
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001848static int nmk_pinctrl_probe(struct platform_device *pdev)
Linus Walleije98ea772012-04-26 23:57:25 +02001849{
1850 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jones855f80c2012-05-26 06:09:29 +01001851 struct device_node *np = pdev->dev.of_node;
Lee Jones32e67ee2013-01-11 15:45:29 +00001852 struct device_node *prcm_np;
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
Lee Jones32e67ee2013-01-11 15:45:29 +00001881 if (np) {
1882 prcm_np = of_parse_phandle(np, "prcm", 0);
1883 if (prcm_np)
1884 npct->prcm_base = of_iomap(prcm_np, 0);
1885 }
1886
1887 /* Allow platform passed information to over-write DT. */
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Lee Jones32e67ee2013-01-11 15:45:29 +00001889 if (res)
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001890 npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
1891 resource_size(res));
Lee Jones32e67ee2013-01-11 15:45:29 +00001892 if (!npct->prcm_base) {
1893 if (version == PINCTRL_NMK_STN8815) {
1894 dev_info(&pdev->dev,
1895 "No PRCM base, "
1896 "assuming no ALT-Cx control is available\n");
1897 } else {
1898 dev_err(&pdev->dev, "missing PRCM base address\n");
1899 return -EINVAL;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001900 }
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001901 }
1902
Linus Walleije98ea772012-04-26 23:57:25 +02001903 /*
1904 * We need all the GPIO drivers to probe FIRST, or we will not be able
1905 * to obtain references to the struct gpio_chip * for them, and we
1906 * need this to proceed.
1907 */
1908 for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001909 if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
Linus Walleije98ea772012-04-26 23:57:25 +02001910 dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
Linus Walleije98ea772012-04-26 23:57:25 +02001911 return -EPROBE_DEFER;
1912 }
Patrice Chotard1d853ca2012-10-08 16:50:24 +02001913 npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
Linus Walleije98ea772012-04-26 23:57:25 +02001914 }
1915
1916 nmk_pinctrl_desc.pins = npct->soc->pins;
1917 nmk_pinctrl_desc.npins = npct->soc->npins;
1918 npct->dev = &pdev->dev;
Jonas Aabergf1671bf2012-10-25 08:40:42 +02001919
Linus Walleije98ea772012-04-26 23:57:25 +02001920 npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
1921 if (!npct->pctl) {
1922 dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
1923 return -EINVAL;
1924 }
1925
1926 /* We will handle a range of GPIO pins */
1927 for (i = 0; i < npct->soc->gpio_num_ranges; i++)
1928 pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
1929
1930 platform_set_drvdata(pdev, npct);
1931 dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
1932
1933 return 0;
1934}
1935
Lee Jones513c27f2012-04-13 15:05:05 +01001936static const struct of_device_id nmk_gpio_match[] = {
1937 { .compatible = "st,nomadik-gpio", },
1938 {}
1939};
1940
Rabin Vincent3e3c62c2010-03-03 04:52:34 +01001941static struct platform_driver nmk_gpio_driver = {
1942 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001943 .owner = THIS_MODULE,
1944 .name = "gpio",
Lee Jones513c27f2012-04-13 15:05:05 +01001945 .of_match_table = nmk_gpio_match,
Rabin Vincent5317e4d12011-02-10 09:29:53 +05301946 },
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001947 .probe = nmk_gpio_probe,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001948};
1949
Linus Walleije98ea772012-04-26 23:57:25 +02001950static const struct platform_device_id nmk_pinctrl_id[] = {
1951 { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
1952 { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
Patrice Chotard45a1b532012-07-20 15:45:22 +02001953 { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
Axel Lin8c995d62012-11-04 23:30:42 +08001954 { }
Linus Walleije98ea772012-04-26 23:57:25 +02001955};
1956
1957static struct platform_driver nmk_pinctrl_driver = {
1958 .driver = {
1959 .owner = THIS_MODULE,
1960 .name = "pinctrl-nomadik",
Lee Jones855f80c2012-05-26 06:09:29 +01001961 .of_match_table = nmk_pinctrl_match,
Linus Walleije98ea772012-04-26 23:57:25 +02001962 },
1963 .probe = nmk_pinctrl_probe,
1964 .id_table = nmk_pinctrl_id,
Julien Delacou8d99b322012-12-11 09:17:47 +01001965#ifdef CONFIG_PM
1966 .suspend = nmk_pinctrl_suspend,
1967 .resume = nmk_pinctrl_resume,
1968#endif
Linus Walleije98ea772012-04-26 23:57:25 +02001969};
1970
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001971static int __init nmk_gpio_init(void)
1972{
Linus Walleije98ea772012-04-26 23:57:25 +02001973 int ret;
1974
1975 ret = platform_driver_register(&nmk_gpio_driver);
1976 if (ret)
1977 return ret;
1978 return platform_driver_register(&nmk_pinctrl_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001979}
1980
Rabin Vincent33f45ea2010-06-02 06:09:52 +01001981core_initcall(nmk_gpio_init);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001982
1983MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
1984MODULE_DESCRIPTION("Nomadik GPIO Driver");
1985MODULE_LICENSE("GPL");