blob: 17508b5820f571b8ff7c0d3f70aeb28d3c9ff18e [file] [log] [blame]
Patrice Chotard0493e642013-01-08 10:41:02 +01001/*
2 * Copyright (C) ST-Ericsson SA 2013
3 *
4 * Author: Patrice Chotard <patrice.chotard@st.com>
5 * License terms: GNU General Public License (GPL) version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/err.h>
Lee Jonesf30a3832013-01-31 11:07:40 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010019#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/irq.h>
22#include <linux/interrupt.h>
23#include <linux/bitops.h>
24#include <linux/mfd/abx500.h>
25#include <linux/mfd/abx500/ab8500.h>
26#include <linux/mfd/abx500/ab8500-gpio.h>
27#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/consumer.h>
29#include <linux/pinctrl/pinmux.h>
30#include <linux/pinctrl/pinconf.h>
31#include <linux/pinctrl/pinconf-generic.h>
32
33#include "pinctrl-abx500.h"
34
35/*
36 * The AB9540 and AB8540 GPIO support are extended versions
37 * of the AB8500 GPIO support.
38 * The AB9540 supports an additional (7th) register so that
39 * more GPIO may be configured and used.
40 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
41 * internal pull-up and pull-down capabilities.
42 */
43
44/*
45 * GPIO registers offset
46 * Bank: 0x10
47 */
48#define AB8500_GPIO_SEL1_REG 0x00
49#define AB8500_GPIO_SEL2_REG 0x01
50#define AB8500_GPIO_SEL3_REG 0x02
51#define AB8500_GPIO_SEL4_REG 0x03
52#define AB8500_GPIO_SEL5_REG 0x04
53#define AB8500_GPIO_SEL6_REG 0x05
54#define AB9540_GPIO_SEL7_REG 0x06
55
56#define AB8500_GPIO_DIR1_REG 0x10
57#define AB8500_GPIO_DIR2_REG 0x11
58#define AB8500_GPIO_DIR3_REG 0x12
59#define AB8500_GPIO_DIR4_REG 0x13
60#define AB8500_GPIO_DIR5_REG 0x14
61#define AB8500_GPIO_DIR6_REG 0x15
62#define AB9540_GPIO_DIR7_REG 0x16
63
64#define AB8500_GPIO_OUT1_REG 0x20
65#define AB8500_GPIO_OUT2_REG 0x21
66#define AB8500_GPIO_OUT3_REG 0x22
67#define AB8500_GPIO_OUT4_REG 0x23
68#define AB8500_GPIO_OUT5_REG 0x24
69#define AB8500_GPIO_OUT6_REG 0x25
70#define AB9540_GPIO_OUT7_REG 0x26
71
72#define AB8500_GPIO_PUD1_REG 0x30
73#define AB8500_GPIO_PUD2_REG 0x31
74#define AB8500_GPIO_PUD3_REG 0x32
75#define AB8500_GPIO_PUD4_REG 0x33
76#define AB8500_GPIO_PUD5_REG 0x34
77#define AB8500_GPIO_PUD6_REG 0x35
78#define AB9540_GPIO_PUD7_REG 0x36
79
80#define AB8500_GPIO_IN1_REG 0x40
81#define AB8500_GPIO_IN2_REG 0x41
82#define AB8500_GPIO_IN3_REG 0x42
83#define AB8500_GPIO_IN4_REG 0x43
84#define AB8500_GPIO_IN5_REG 0x44
85#define AB8500_GPIO_IN6_REG 0x45
86#define AB9540_GPIO_IN7_REG 0x46
87#define AB8540_GPIO_VINSEL_REG 0x47
88#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
89#define AB8500_GPIO_ALTFUN_REG 0x50
90#define AB8500_NUM_VIR_GPIO_IRQ 16
91#define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
92#define AB8540_GPIO_VINSEL_MASK 0x03
93#define AB8540_GPIOX_VBAT_START 51
94#define AB8540_GPIOX_VBAT_END 54
95
96enum abx500_gpio_action {
97 NONE,
98 STARTUP,
99 SHUTDOWN,
100 MASK,
101 UNMASK
102};
103
104struct abx500_pinctrl {
105 struct device *dev;
106 struct pinctrl_dev *pctldev;
107 struct abx500_pinctrl_soc_data *soc;
108 struct gpio_chip chip;
109 struct ab8500 *parent;
110 struct mutex lock;
111 u32 irq_base;
112 enum abx500_gpio_action irq_action;
113 u16 rising;
114 u16 falling;
115 struct abx500_gpio_irq_cluster *irq_cluster;
116 int irq_cluster_size;
117 int irq_gpio_rising_offset;
118 int irq_gpio_falling_offset;
119 int irq_gpio_factor;
120};
121
122/**
123 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
124 * @chip: Member of the structure abx500_pinctrl
125 */
126static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
127{
128 return container_of(chip, struct abx500_pinctrl, chip);
129}
130
131static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000132 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100133{
134 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
135 u8 pos = offset % 8;
136 u8 val;
137 int ret;
138
139 reg += offset / 8;
140 ret = abx500_get_register_interruptible(pct->dev,
141 AB8500_MISC, reg, &val);
142
143 *bit = !!(val & BIT(pos));
144
145 if (ret < 0)
146 dev_err(pct->dev,
147 "%s read reg =%x, offset=%x failed\n",
148 __func__, reg, offset);
149
150 return ret;
151}
152
153static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000154 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100155{
156 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
157 u8 pos = offset % 8;
158 int ret;
159
160 reg += offset / 8;
161 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000162 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100163 if (ret < 0)
164 dev_err(pct->dev, "%s write failed\n", __func__);
Lee Jones83b423c2013-01-23 13:24:08 +0000165
Patrice Chotard0493e642013-01-08 10:41:02 +0100166 return ret;
167}
Lee Jones83b423c2013-01-23 13:24:08 +0000168
Patrice Chotard0493e642013-01-08 10:41:02 +0100169/**
170 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000171 * @chip: Gpio device
172 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100173 */
174static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
175{
176 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
177 bool bit;
178 int ret;
179
180 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
181 offset, &bit);
182 if (ret < 0) {
183 dev_err(pct->dev, "%s failed\n", __func__);
184 return ret;
185 }
Lee Jones83b423c2013-01-23 13:24:08 +0000186
Patrice Chotard0493e642013-01-08 10:41:02 +0100187 return bit;
188}
189
190static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
191{
192 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
193 int ret;
194
195 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
196 if (ret < 0)
197 dev_err(pct->dev, "%s write failed\n", __func__);
198}
199
200static int abx500_config_pull_updown(struct abx500_pinctrl *pct,
Lee Jones83b423c2013-01-23 13:24:08 +0000201 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100202{
203 u8 pos;
204 int ret;
205 struct pullud *pullud;
206
207 if (!pct->soc->pullud) {
208 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
209 __func__);
210 ret = -EPERM;
211 goto out;
212 }
213
214 pullud = pct->soc->pullud;
215
216 if ((offset < pullud->first_pin)
217 || (offset > pullud->last_pin)) {
218 ret = -EINVAL;
219 goto out;
220 }
221
222 pos = offset << 1;
223
224 ret = abx500_mask_and_set_register_interruptible(pct->dev,
225 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
226 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
227
228out:
229 if (ret < 0)
230 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000231
Patrice Chotard0493e642013-01-08 10:41:02 +0100232 return ret;
233}
234
235static int abx500_gpio_direction_output(struct gpio_chip *chip,
236 unsigned offset,
237 int val)
238{
239 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
240 struct pullud *pullud = pct->soc->pullud;
241 unsigned gpio;
242 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000243
Patrice Chotard0493e642013-01-08 10:41:02 +0100244 /* set direction as output */
245 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
246 if (ret < 0)
247 return ret;
248
249 /* disable pull down */
250 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
251 if (ret < 0)
252 return ret;
253
254 /* if supported, disable both pull down and pull up */
255 gpio = offset + 1;
256 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
257 ret = abx500_config_pull_updown(pct,
258 gpio,
259 ABX500_GPIO_PULL_NONE);
260 if (ret < 0)
261 return ret;
262 }
Lee Jones83b423c2013-01-23 13:24:08 +0000263
Patrice Chotard0493e642013-01-08 10:41:02 +0100264 /* set the output as 1 or 0 */
265 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
266}
267
268static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
269{
270 /* set the register as input */
271 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
272}
273
274static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
275{
276 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000277 /* The AB8500 GPIO numbers are off by one */
278 int gpio = offset + 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100279 int base = pct->irq_base;
280 int i;
281
282 for (i = 0; i < pct->irq_cluster_size; i++) {
283 struct abx500_gpio_irq_cluster *cluster =
284 &pct->irq_cluster[i];
285
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000286 if (gpio >= cluster->start && gpio <= cluster->end)
287 return base + gpio - cluster->start;
Patrice Chotard0493e642013-01-08 10:41:02 +0100288
289 /* Advance by the number of gpios in this cluster */
290 base += cluster->end + cluster->offset - cluster->start + 1;
291 }
292
293 return -EINVAL;
294}
295
296static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000297 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100298{
299 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
300 struct alternate_functions af = pct->soc->alternate_functions[gpio];
301 int ret;
302 int val;
303 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000304
Patrice Chotard0493e642013-01-08 10:41:02 +0100305 const char *modes[] = {
306 [ABX500_DEFAULT] = "default",
307 [ABX500_ALT_A] = "altA",
308 [ABX500_ALT_B] = "altB",
309 [ABX500_ALT_C] = "altC",
310 };
311
312 /* sanity check */
313 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
314 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
315 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
316 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
317 modes[alt_setting]);
318 return -EINVAL;
319 }
320
321 /* on ABx5xx, there is no GPIO0, so adjust the offset */
322 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000323
Patrice Chotard0493e642013-01-08 10:41:02 +0100324 switch (alt_setting) {
325 case ABX500_DEFAULT:
326 /*
327 * for ABx5xx family, default mode is always selected by
328 * writing 0 to GPIOSELx register, except for pins which
329 * support at least ALT_B mode, default mode is selected
330 * by writing 1 to GPIOSELx register
331 */
332 val = 0;
333 if (af.alt_bit1 != UNUSED)
334 val++;
335
336 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
337 offset, val);
338 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000339
Patrice Chotard0493e642013-01-08 10:41:02 +0100340 case ABX500_ALT_A:
341 /*
342 * for ABx5xx family, alt_a mode is always selected by
343 * writing 1 to GPIOSELx register, except for pins which
344 * support at least ALT_B mode, alt_a mode is selected
345 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
346 * register
347 */
348 if (af.alt_bit1 != UNUSED) {
349 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
350 offset, 0);
351 ret = abx500_gpio_set_bits(chip,
352 AB8500_GPIO_ALTFUN_REG,
353 af.alt_bit1,
354 !!(af.alta_val && BIT(0)));
355 if (af.alt_bit2 != UNUSED)
356 ret = abx500_gpio_set_bits(chip,
357 AB8500_GPIO_ALTFUN_REG,
358 af.alt_bit2,
359 !!(af.alta_val && BIT(1)));
360 } else
361 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
362 offset, 1);
363 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000364
Patrice Chotard0493e642013-01-08 10:41:02 +0100365 case ABX500_ALT_B:
366 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
367 offset, 0);
368 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
369 af.alt_bit1, !!(af.altb_val && BIT(0)));
370 if (af.alt_bit2 != UNUSED)
371 ret = abx500_gpio_set_bits(chip,
372 AB8500_GPIO_ALTFUN_REG,
373 af.alt_bit2,
374 !!(af.altb_val && BIT(1)));
375 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000376
Patrice Chotard0493e642013-01-08 10:41:02 +0100377 case ABX500_ALT_C:
378 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
379 offset, 0);
380 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
381 af.alt_bit2, !!(af.altc_val && BIT(0)));
382 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
383 af.alt_bit2, !!(af.altc_val && BIT(1)));
384 break;
385
386 default:
387 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000388
Patrice Chotard0493e642013-01-08 10:41:02 +0100389 return -EINVAL;
390 }
Lee Jones83b423c2013-01-23 13:24:08 +0000391
Patrice Chotard0493e642013-01-08 10:41:02 +0100392 return ret;
393}
394
395static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000396 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100397{
398 u8 mode;
399 bool bit_mode;
400 bool alt_bit1;
401 bool alt_bit2;
402 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
403 struct alternate_functions af = pct->soc->alternate_functions[gpio];
404
405 /*
406 * if gpiosel_bit is set to unused,
407 * it means no GPIO or special case
408 */
409 if (af.gpiosel_bit == UNUSED)
410 return ABX500_DEFAULT;
411
412 /* read GpioSelx register */
413 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (gpio / 8),
414 af.gpiosel_bit, &bit_mode);
415 mode = bit_mode;
416
417 /* sanity check */
418 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
419 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
420 dev_err(pct->dev,
421 "alt_bitX value not in correct range (-1 to 7)\n");
422 return -EINVAL;
423 }
Lee Jones83b423c2013-01-23 13:24:08 +0000424
Patrice Chotard0493e642013-01-08 10:41:02 +0100425 /* if alt_bit2 is used, alt_bit1 must be used too */
426 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
427 dev_err(pct->dev,
428 "if alt_bit2 is used, alt_bit1 can't be unused\n");
429 return -EINVAL;
430 }
431
432 /* check if pin use AlternateFunction register */
433 if ((af.alt_bit1 == UNUSED) && (af.alt_bit1 == UNUSED))
434 return mode;
435 /*
436 * if pin GPIOSEL bit is set and pin supports alternate function,
437 * it means DEFAULT mode
438 */
439 if (mode)
440 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000441
Patrice Chotard0493e642013-01-08 10:41:02 +0100442 /*
443 * pin use the AlternatFunction register
444 * read alt_bit1 value
445 */
446 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
447 af.alt_bit1, &alt_bit1);
448
449 if (af.alt_bit2 != UNUSED)
450 /* read alt_bit2 value */
451 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
452 &alt_bit2);
453 else
454 alt_bit2 = 0;
455
456 mode = (alt_bit2 << 1) + alt_bit1;
457 if (mode == af.alta_val)
458 return ABX500_ALT_A;
459 else if (mode == af.altb_val)
460 return ABX500_ALT_B;
461 else
462 return ABX500_ALT_C;
463}
464
465#ifdef CONFIG_DEBUG_FS
466
467#include <linux/seq_file.h>
468
469static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000470 struct pinctrl_dev *pctldev,
471 struct gpio_chip *chip,
472 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100473{
474 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
475 const char *label = gpiochip_is_requested(chip, offset - 1);
476 u8 gpio_offset = offset - 1;
477 int mode = -1;
478 bool is_out;
479 bool pull;
Lee Jones83b423c2013-01-23 13:24:08 +0000480
Patrice Chotard0493e642013-01-08 10:41:02 +0100481 const char *modes[] = {
482 [ABX500_DEFAULT] = "default",
483 [ABX500_ALT_A] = "altA",
484 [ABX500_ALT_B] = "altB",
485 [ABX500_ALT_C] = "altC",
486 };
487
488 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
489 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, gpio_offset, &pull);
490
491 if (pctldev)
492 mode = abx500_get_mode(pctldev, chip, offset);
493
494 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s %-9s %s",
495 gpio, label ?: "(none)",
496 is_out ? "out" : "in ",
497 is_out ?
498 (chip->get
499 ? (chip->get(chip, offset) ? "hi" : "lo")
500 : "? ")
501 : (pull ? "pull up" : "pull down"),
502 (mode < 0) ? "unknown" : modes[mode]);
503
504 if (label && !is_out) {
505 int irq = gpio_to_irq(gpio);
506 struct irq_desc *desc = irq_to_desc(irq);
507
508 if (irq >= 0 && desc->action) {
509 char *trigger;
510 int irq_offset = irq - pct->irq_base;
511
512 if (pct->rising & BIT(irq_offset))
513 trigger = "edge-rising";
514 else if (pct->falling & BIT(irq_offset))
515 trigger = "edge-falling";
516 else
517 trigger = "edge-undefined";
518
519 seq_printf(s, " irq-%d %s", irq, trigger);
520 }
521 }
522}
523
524static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
525{
526 unsigned i;
527 unsigned gpio = chip->base;
528 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
529 struct pinctrl_dev *pctldev = pct->pctldev;
530
531 for (i = 0; i < chip->ngpio; i++, gpio++) {
532 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
533 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
534 seq_printf(s, "\n");
535 }
536}
537
538#else
539static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000540 struct pinctrl_dev *pctldev,
541 struct gpio_chip *chip,
542 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100543{
544}
545#define abx500_gpio_dbg_show NULL
546#endif
547
548int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
549{
550 int gpio = chip->base + offset;
551
552 return pinctrl_request_gpio(gpio);
553}
554
555void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
556{
557 int gpio = chip->base + offset;
558
559 pinctrl_free_gpio(gpio);
560}
561
562static struct gpio_chip abx500gpio_chip = {
563 .label = "abx500-gpio",
564 .owner = THIS_MODULE,
565 .request = abx500_gpio_request,
566 .free = abx500_gpio_free,
567 .direction_input = abx500_gpio_direction_input,
568 .get = abx500_gpio_get,
569 .direction_output = abx500_gpio_direction_output,
570 .set = abx500_gpio_set,
571 .to_irq = abx500_gpio_to_irq,
572 .dbg_show = abx500_gpio_dbg_show,
573};
574
575static unsigned int irq_to_rising(unsigned int irq)
576{
577 struct abx500_pinctrl *pct = irq_get_chip_data(irq);
578 int offset = irq - pct->irq_base;
579 int new_irq;
580
581 new_irq = offset * pct->irq_gpio_factor
582 + pct->irq_gpio_rising_offset
583 + pct->parent->irq_base;
584
585 return new_irq;
586}
587
588static unsigned int irq_to_falling(unsigned int irq)
589{
590 struct abx500_pinctrl *pct = irq_get_chip_data(irq);
591 int offset = irq - pct->irq_base;
592 int new_irq;
593
594 new_irq = offset * pct->irq_gpio_factor
595 + pct->irq_gpio_falling_offset
596 + pct->parent->irq_base;
597 return new_irq;
598
599}
600
601static unsigned int rising_to_irq(unsigned int irq, void *dev)
602{
603 struct abx500_pinctrl *pct = dev;
604 int offset, new_irq;
605
606 offset = irq - pct->irq_gpio_rising_offset
607 - pct->parent->irq_base;
608 new_irq = (offset / pct->irq_gpio_factor)
609 + pct->irq_base;
610
611 return new_irq;
612}
613
614static unsigned int falling_to_irq(unsigned int irq, void *dev)
615{
616 struct abx500_pinctrl *pct = dev;
617 int offset, new_irq;
618
619 offset = irq - pct->irq_gpio_falling_offset
620 - pct->parent->irq_base;
621 new_irq = (offset / pct->irq_gpio_factor)
622 + pct->irq_base;
623
624 return new_irq;
625}
626
627/*
628 * IRQ handler
629 */
630
631static irqreturn_t handle_rising(int irq, void *dev)
632{
633
634 handle_nested_irq(rising_to_irq(irq , dev));
635 return IRQ_HANDLED;
636}
637
638static irqreturn_t handle_falling(int irq, void *dev)
639{
640
641 handle_nested_irq(falling_to_irq(irq, dev));
642 return IRQ_HANDLED;
643}
644
645static void abx500_gpio_irq_lock(struct irq_data *data)
646{
647 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
648 mutex_lock(&pct->lock);
649}
650
651static void abx500_gpio_irq_sync_unlock(struct irq_data *data)
652{
653 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
654 unsigned int irq = data->irq;
655 int offset = irq - pct->irq_base;
656 bool rising = pct->rising & BIT(offset);
657 bool falling = pct->falling & BIT(offset);
658 int ret;
659
660 switch (pct->irq_action) {
661 case STARTUP:
662 if (rising)
663 ret = request_threaded_irq(irq_to_rising(irq),
664 NULL, handle_rising,
665 IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND,
666 "abx500-gpio-r", pct);
667 if (falling)
668 ret = request_threaded_irq(irq_to_falling(irq),
669 NULL, handle_falling,
670 IRQF_TRIGGER_FALLING | IRQF_NO_SUSPEND,
671 "abx500-gpio-f", pct);
672 break;
673 case SHUTDOWN:
674 if (rising)
675 free_irq(irq_to_rising(irq), pct);
676 if (falling)
677 free_irq(irq_to_falling(irq), pct);
678 break;
679 case MASK:
680 if (rising)
681 disable_irq(irq_to_rising(irq));
682 if (falling)
683 disable_irq(irq_to_falling(irq));
684 break;
685 case UNMASK:
686 if (rising)
687 enable_irq(irq_to_rising(irq));
688 if (falling)
689 enable_irq(irq_to_falling(irq));
690 break;
691 case NONE:
692 break;
693 }
694 pct->irq_action = NONE;
695 pct->rising &= ~(BIT(offset));
696 pct->falling &= ~(BIT(offset));
697 mutex_unlock(&pct->lock);
698}
699
700
701static void abx500_gpio_irq_mask(struct irq_data *data)
702{
703 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
704 pct->irq_action = MASK;
705}
706
707static void abx500_gpio_irq_unmask(struct irq_data *data)
708{
709 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
710 pct->irq_action = UNMASK;
711}
712
713static int abx500_gpio_irq_set_type(struct irq_data *data, unsigned int type)
714{
715 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
716 unsigned int irq = data->irq;
717 int offset = irq - pct->irq_base;
718
719 if (type == IRQ_TYPE_EDGE_BOTH) {
720 pct->rising = BIT(offset);
721 pct->falling = BIT(offset);
722 } else if (type == IRQ_TYPE_EDGE_RISING) {
723 pct->rising = BIT(offset);
724 } else {
725 pct->falling = BIT(offset);
726 }
727 return 0;
728}
729
730static unsigned int abx500_gpio_irq_startup(struct irq_data *data)
731{
732 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
733 pct->irq_action = STARTUP;
734 return 0;
735}
736
737static void abx500_gpio_irq_shutdown(struct irq_data *data)
738{
739 struct abx500_pinctrl *pct = irq_data_get_irq_chip_data(data);
740 pct->irq_action = SHUTDOWN;
741}
742
743static struct irq_chip abx500_gpio_irq_chip = {
744 .name = "abx500-gpio",
745 .irq_startup = abx500_gpio_irq_startup,
746 .irq_shutdown = abx500_gpio_irq_shutdown,
747 .irq_bus_lock = abx500_gpio_irq_lock,
748 .irq_bus_sync_unlock = abx500_gpio_irq_sync_unlock,
749 .irq_mask = abx500_gpio_irq_mask,
750 .irq_unmask = abx500_gpio_irq_unmask,
751 .irq_set_type = abx500_gpio_irq_set_type,
752};
753
754static int abx500_gpio_irq_init(struct abx500_pinctrl *pct)
755{
756 u32 base = pct->irq_base;
757 int irq;
758
759 for (irq = base; irq < base + AB8500_NUM_VIR_GPIO_IRQ ; irq++) {
760 irq_set_chip_data(irq, pct);
761 irq_set_chip_and_handler(irq, &abx500_gpio_irq_chip,
762 handle_simple_irq);
763 irq_set_nested_thread(irq, 1);
764#ifdef CONFIG_ARM
765 set_irq_flags(irq, IRQF_VALID);
766#else
767 irq_set_noprobe(irq);
768#endif
769 }
770
771 return 0;
772}
773
774static void abx500_gpio_irq_remove(struct abx500_pinctrl *pct)
775{
776 int base = pct->irq_base;
777 int irq;
778
779 for (irq = base; irq < base + AB8500_NUM_VIR_GPIO_IRQ; irq++) {
780#ifdef CONFIG_ARM
781 set_irq_flags(irq, 0);
782#endif
783 irq_set_chip_and_handler(irq, NULL, NULL);
784 irq_set_chip_data(irq, NULL);
785 }
786}
787
788static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
789{
790 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
791
792 return pct->soc->nfunctions;
793}
794
795static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
796 unsigned function)
797{
798 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
799
800 return pct->soc->functions[function].name;
801}
802
803static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000804 unsigned function,
805 const char * const **groups,
806 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100807{
808 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
809
810 *groups = pct->soc->functions[function].groups;
811 *num_groups = pct->soc->functions[function].ngroups;
812
813 return 0;
814}
815
816static void abx500_disable_lazy_irq(struct gpio_chip *chip, unsigned gpio)
817{
818 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
819 int irq;
820 int offset;
821 bool rising;
822 bool falling;
823
824 /*
825 * check if gpio has interrupt capability and convert
826 * gpio number to irq
827 * On ABx5xx, there is no GPIO0, GPIO1 is the
828 * first one, so adjust gpio number
829 */
830 gpio--;
831 irq = gpio_to_irq(gpio + chip->base);
832 if (irq < 0)
833 return;
834
835 offset = irq - pct->irq_base;
836 rising = pct->rising & BIT(offset);
837 falling = pct->falling & BIT(offset);
838
839 /* nothing to do ?*/
840 if (!rising && !falling)
841 return;
842
843 if (rising) {
844 disable_irq(irq_to_rising(irq));
845 free_irq(irq_to_rising(irq), pct);
846 }
847 if (falling) {
848 disable_irq(irq_to_falling(irq));
849 free_irq(irq_to_falling(irq), pct);
850 }
851}
852
853static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000854 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100855{
856 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
857 struct gpio_chip *chip = &pct->chip;
858 const struct abx500_pingroup *g;
859 int i;
860 int ret = 0;
861
862 g = &pct->soc->groups[group];
863 if (g->altsetting < 0)
864 return -EINVAL;
865
866 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
867
868 for (i = 0; i < g->npins; i++) {
869 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
870 g->pins[i], g->altsetting);
871
872 abx500_disable_lazy_irq(chip, g->pins[i]);
873 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
874 }
Lee Jones83b423c2013-01-23 13:24:08 +0000875
Patrice Chotard0493e642013-01-08 10:41:02 +0100876 return ret;
877}
878
879static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000880 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100881{
882 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
883 const struct abx500_pingroup *g;
884
885 g = &pct->soc->groups[group];
886 if (g->altsetting < 0)
887 return;
888
889 /* FIXME: poke out the mux, set the pin to some default state? */
890 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
891}
892
893int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000894 struct pinctrl_gpio_range *range,
895 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100896{
897 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
898 const struct abx500_pinrange *p;
899 int ret;
900 int i;
901
902 /*
903 * Different ranges have different ways to enable GPIO function on a
904 * pin, so refer back to our local range type, where we handily define
905 * what altfunc enables GPIO for a certain pin.
906 */
907 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
908 p = &pct->soc->gpio_ranges[i];
909 if ((offset >= p->offset) &&
910 (offset < (p->offset + p->npins)))
911 break;
912 }
913
914 if (i == pct->soc->gpio_num_ranges) {
915 dev_err(pct->dev, "%s failed to locate range\n", __func__);
916 return -ENODEV;
917 }
918
919 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
920 p->altfunc, offset);
921
922 ret = abx500_set_mode(pct->pctldev, &pct->chip,
923 offset, p->altfunc);
924 if (ret < 0) {
925 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
926 return ret;
927 }
928
929 return ret;
930}
931
932static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000933 struct pinctrl_gpio_range *range,
934 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100935{
936}
937
938static struct pinmux_ops abx500_pinmux_ops = {
939 .get_functions_count = abx500_pmx_get_funcs_cnt,
940 .get_function_name = abx500_pmx_get_func_name,
941 .get_function_groups = abx500_pmx_get_func_groups,
942 .enable = abx500_pmx_enable,
943 .disable = abx500_pmx_disable,
944 .gpio_request_enable = abx500_gpio_request_enable,
945 .gpio_disable_free = abx500_gpio_disable_free,
946};
947
948static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
949{
950 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
951
952 return pct->soc->ngroups;
953}
954
955static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000956 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100957{
958 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
959
960 return pct->soc->groups[selector].name;
961}
962
963static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000964 unsigned selector,
965 const unsigned **pins,
966 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100967{
968 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
969
970 *pins = pct->soc->groups[selector].pins;
971 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000972
Patrice Chotard0493e642013-01-08 10:41:02 +0100973 return 0;
974}
975
976static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000977 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100978{
979 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
980 struct gpio_chip *chip = &pct->chip;
981
982 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
983 chip->base + offset - 1);
984}
985
986static struct pinctrl_ops abx500_pinctrl_ops = {
987 .get_groups_count = abx500_get_groups_cnt,
988 .get_group_name = abx500_get_group_name,
989 .get_group_pins = abx500_get_group_pins,
990 .pin_dbg_show = abx500_pin_dbg_show,
991};
992
993int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000994 unsigned pin,
995 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100996{
Lee Jones1abeebe2012-12-20 11:11:19 +0000997 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100998}
999
1000int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +00001001 unsigned pin,
1002 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +01001003{
1004 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
1005 struct pullud *pullud = pct->soc->pullud;
1006 struct gpio_chip *chip = &pct->chip;
1007 unsigned offset;
1008 int ret;
1009 enum pin_config_param param = pinconf_to_config_param(config);
1010 enum pin_config_param argument = pinconf_to_config_argument(config);
1011
1012 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
1013 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
1014 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
1015 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +00001016
Patrice Chotard0493e642013-01-08 10:41:02 +01001017 /* on ABx500, there is no GPIO0, so adjust the offset */
1018 offset = pin - 1;
1019
1020 switch (param) {
1021 case PIN_CONFIG_BIAS_PULL_DOWN:
1022 /*
1023 * if argument = 1 set the pull down
1024 * else clear the pull down
1025 */
1026 ret = abx500_gpio_direction_input(chip, offset);
1027 /*
1028 * Some chips only support pull down, while some actually
1029 * support both pull up and pull down. Such chips have
1030 * a "pullud" range specified for the pins that support
1031 * both features. If the pin is not within that range, we
1032 * fall back to the old bit set that only support pull down.
1033 */
1034 if (pullud &&
1035 pin >= pullud->first_pin &&
1036 pin <= pullud->last_pin)
1037 ret = abx500_config_pull_updown(pct,
1038 pin,
1039 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
1040 else
1041 /* Chip only supports pull down */
1042 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1043 offset, argument ? 0 : 1);
1044 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001045
Patrice Chotard0493e642013-01-08 10:41:02 +01001046 case PIN_CONFIG_OUTPUT:
1047 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +00001048
Patrice Chotard0493e642013-01-08 10:41:02 +01001049 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001050
Patrice Chotard0493e642013-01-08 10:41:02 +01001051 default:
1052 dev_err(chip->dev, "illegal configuration requested\n");
Lee Jones83b423c2013-01-23 13:24:08 +00001053
Patrice Chotard0493e642013-01-08 10:41:02 +01001054 return -EINVAL;
1055 }
Lee Jones83b423c2013-01-23 13:24:08 +00001056
Patrice Chotard0493e642013-01-08 10:41:02 +01001057 return ret;
1058}
1059
1060static struct pinconf_ops abx500_pinconf_ops = {
1061 .pin_config_get = abx500_pin_config_get,
1062 .pin_config_set = abx500_pin_config_set,
1063};
1064
1065static struct pinctrl_desc abx500_pinctrl_desc = {
1066 .name = "pinctrl-abx500",
1067 .pctlops = &abx500_pinctrl_ops,
1068 .pmxops = &abx500_pinmux_ops,
1069 .confops = &abx500_pinconf_ops,
1070 .owner = THIS_MODULE,
1071};
1072
1073static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1074{
1075 unsigned int lowest = 0;
1076 unsigned int highest = 0;
1077 unsigned int npins = 0;
1078 int i;
1079
1080 /*
1081 * Compute number of GPIOs from the last SoC gpio range descriptors
1082 * These ranges may include "holes" but the GPIO number space shall
1083 * still be homogeneous, so we need to detect and account for any
1084 * such holes so that these are included in the number of GPIO pins.
1085 */
1086 for (i = 0; i < soc->gpio_num_ranges; i++) {
1087 unsigned gstart;
1088 unsigned gend;
1089 const struct abx500_pinrange *p;
1090
1091 p = &soc->gpio_ranges[i];
1092 gstart = p->offset;
1093 gend = p->offset + p->npins - 1;
1094
1095 if (i == 0) {
1096 /* First iteration, set start values */
1097 lowest = gstart;
1098 highest = gend;
1099 } else {
1100 if (gstart < lowest)
1101 lowest = gstart;
1102 if (gend > highest)
1103 highest = gend;
1104 }
1105 }
1106 /* this gives the absolute number of pins */
1107 npins = highest - lowest + 1;
1108 return npins;
1109}
1110
Lee Jonesf30a3832013-01-31 11:07:40 +00001111static const struct of_device_id abx500_gpio_match[] = {
1112 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1113 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1114 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1115 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
1116};
1117
Patrice Chotard0493e642013-01-08 10:41:02 +01001118static int abx500_gpio_probe(struct platform_device *pdev)
1119{
1120 struct ab8500_platform_data *abx500_pdata =
1121 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +00001122 struct abx500_gpio_platform_data *pdata = NULL;
1123 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +01001124 struct abx500_pinctrl *pct;
1125 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +00001126 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +00001127 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +01001128 int i;
1129
Lee Jonesf30a3832013-01-31 11:07:40 +00001130 if (abx500_pdata)
1131 pdata = abx500_pdata->gpio;
Lee Jones83b423c2013-01-23 13:24:08 +00001132 if (!pdata) {
Lee Jonesf30a3832013-01-31 11:07:40 +00001133 if (np) {
1134 const struct of_device_id *match;
1135
1136 match = of_match_device(abx500_gpio_match, &pdev->dev);
1137 if (!match)
1138 return -ENODEV;
1139 id = (unsigned long)match->data;
1140 } else {
1141 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
1142 return -ENODEV;
1143 }
Patrice Chotard0493e642013-01-08 10:41:02 +01001144 }
1145
Lee Jonesf30a3832013-01-31 11:07:40 +00001146 if (platid)
1147 id = platid->driver_data;
1148
Patrice Chotard0493e642013-01-08 10:41:02 +01001149 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1150 GFP_KERNEL);
1151 if (pct == NULL) {
1152 dev_err(&pdev->dev,
1153 "failed to allocate memory for pct\n");
1154 return -ENOMEM;
1155 }
1156
1157 pct->dev = &pdev->dev;
1158 pct->parent = dev_get_drvdata(pdev->dev.parent);
1159 pct->chip = abx500gpio_chip;
1160 pct->chip.dev = &pdev->dev;
1161 pct->chip.base = pdata->gpio_base;
1162 pct->irq_base = pdata->irq_base;
Lee Jonesf30a3832013-01-31 11:07:40 +00001163 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +01001164
1165 /* initialize the lock */
1166 mutex_init(&pct->lock);
1167
1168 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001169 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001170 case PINCTRL_AB8500:
1171 abx500_pinctrl_ab8500_init(&pct->soc);
1172 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001173 case PINCTRL_AB8540:
1174 abx500_pinctrl_ab8540_init(&pct->soc);
1175 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001176 case PINCTRL_AB9540:
1177 abx500_pinctrl_ab9540_init(&pct->soc);
1178 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001179 case PINCTRL_AB8505:
1180 abx500_pinctrl_ab8505_init(&pct->soc);
1181 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001182 default:
1183 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n",
1184 (int) platid->driver_data);
Lee Jonesd41e35c2013-01-16 09:17:13 +00001185 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +01001186 return -EINVAL;
1187 }
1188
1189 if (!pct->soc) {
1190 dev_err(&pdev->dev, "Invalid SOC data\n");
Lee Jonesd41e35c2013-01-16 09:17:13 +00001191 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +01001192 return -EINVAL;
1193 }
1194
1195 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1196 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1197 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
1198 pct->irq_gpio_rising_offset = pct->soc->irq_gpio_rising_offset;
1199 pct->irq_gpio_falling_offset = pct->soc->irq_gpio_falling_offset;
1200 pct->irq_gpio_factor = pct->soc->irq_gpio_factor;
1201
1202 ret = abx500_gpio_irq_init(pct);
1203 if (ret)
1204 goto out_free;
1205 ret = gpiochip_add(&pct->chip);
1206 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001207 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesd41e35c2013-01-16 09:17:13 +00001208 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +01001209 goto out_rem_irq;
1210 }
1211 dev_info(&pdev->dev, "added gpiochip\n");
1212
1213 abx500_pinctrl_desc.pins = pct->soc->pins;
1214 abx500_pinctrl_desc.npins = pct->soc->npins;
1215 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
1216 if (!pct->pctldev) {
1217 dev_err(&pdev->dev,
1218 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +00001219 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +01001220 goto out_rem_chip;
1221 }
1222 dev_info(&pdev->dev, "registered pin controller\n");
1223
1224 /* We will handle a range of GPIO pins */
1225 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1226 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1227
1228 ret = gpiochip_add_pin_range(&pct->chip,
1229 dev_name(&pdev->dev),
1230 p->offset - 1, p->offset, p->npins);
1231 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001232 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001233 }
1234
1235 platform_set_drvdata(pdev, pct);
1236 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1237
1238 return 0;
1239
1240out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +00001241 err = gpiochip_remove(&pct->chip);
1242 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +01001243 dev_info(&pdev->dev, "failed to remove gpiochip\n");
1244out_rem_irq:
1245 abx500_gpio_irq_remove(pct);
1246out_free:
1247 mutex_destroy(&pct->lock);
1248 return ret;
1249}
1250
Lee Jones83b423c2013-01-23 13:24:08 +00001251/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001252 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001253 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001254 */
1255static int abx500_gpio_remove(struct platform_device *pdev)
1256{
1257 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1258 int ret;
1259
1260 ret = gpiochip_remove(&pct->chip);
1261 if (ret < 0) {
1262 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
1263 ret);
1264 return ret;
1265 }
1266
1267 mutex_destroy(&pct->lock);
1268
1269 return 0;
1270}
1271
1272static const struct platform_device_id abx500_pinctrl_id[] = {
1273 { "pinctrl-ab8500", PINCTRL_AB8500 },
1274 { "pinctrl-ab8540", PINCTRL_AB8540 },
1275 { "pinctrl-ab9540", PINCTRL_AB9540 },
1276 { "pinctrl-ab8505", PINCTRL_AB8505 },
1277 { },
1278};
1279
1280static struct platform_driver abx500_gpio_driver = {
1281 .driver = {
1282 .name = "abx500-gpio",
1283 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001284 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001285 },
1286 .probe = abx500_gpio_probe,
1287 .remove = abx500_gpio_remove,
1288 .id_table = abx500_pinctrl_id,
1289};
1290
1291static int __init abx500_gpio_init(void)
1292{
1293 return platform_driver_register(&abx500_gpio_driver);
1294}
1295core_initcall(abx500_gpio_init);
1296
1297MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1298MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1299MODULE_ALIAS("platform:abx500-gpio");
1300MODULE_LICENSE("GPL v2");