blob: b8f6c7597d53b7abe6378bf901f9097300b00988 [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>
Lee Jonesac652d72013-01-31 10:43:00 +000022#include <linux/irqdomain.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010023#include <linux/interrupt.h>
24#include <linux/bitops.h>
25#include <linux/mfd/abx500.h>
26#include <linux/mfd/abx500/ab8500.h>
27#include <linux/mfd/abx500/ab8500-gpio.h>
28#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/consumer.h>
30#include <linux/pinctrl/pinmux.h>
31#include <linux/pinctrl/pinconf.h>
32#include <linux/pinctrl/pinconf-generic.h>
33
34#include "pinctrl-abx500.h"
35
36/*
37 * The AB9540 and AB8540 GPIO support are extended versions
38 * of the AB8500 GPIO support.
39 * The AB9540 supports an additional (7th) register so that
40 * more GPIO may be configured and used.
41 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
42 * internal pull-up and pull-down capabilities.
43 */
44
45/*
46 * GPIO registers offset
47 * Bank: 0x10
48 */
49#define AB8500_GPIO_SEL1_REG 0x00
50#define AB8500_GPIO_SEL2_REG 0x01
51#define AB8500_GPIO_SEL3_REG 0x02
52#define AB8500_GPIO_SEL4_REG 0x03
53#define AB8500_GPIO_SEL5_REG 0x04
54#define AB8500_GPIO_SEL6_REG 0x05
55#define AB9540_GPIO_SEL7_REG 0x06
56
57#define AB8500_GPIO_DIR1_REG 0x10
58#define AB8500_GPIO_DIR2_REG 0x11
59#define AB8500_GPIO_DIR3_REG 0x12
60#define AB8500_GPIO_DIR4_REG 0x13
61#define AB8500_GPIO_DIR5_REG 0x14
62#define AB8500_GPIO_DIR6_REG 0x15
63#define AB9540_GPIO_DIR7_REG 0x16
64
65#define AB8500_GPIO_OUT1_REG 0x20
66#define AB8500_GPIO_OUT2_REG 0x21
67#define AB8500_GPIO_OUT3_REG 0x22
68#define AB8500_GPIO_OUT4_REG 0x23
69#define AB8500_GPIO_OUT5_REG 0x24
70#define AB8500_GPIO_OUT6_REG 0x25
71#define AB9540_GPIO_OUT7_REG 0x26
72
73#define AB8500_GPIO_PUD1_REG 0x30
74#define AB8500_GPIO_PUD2_REG 0x31
75#define AB8500_GPIO_PUD3_REG 0x32
76#define AB8500_GPIO_PUD4_REG 0x33
77#define AB8500_GPIO_PUD5_REG 0x34
78#define AB8500_GPIO_PUD6_REG 0x35
79#define AB9540_GPIO_PUD7_REG 0x36
80
81#define AB8500_GPIO_IN1_REG 0x40
82#define AB8500_GPIO_IN2_REG 0x41
83#define AB8500_GPIO_IN3_REG 0x42
84#define AB8500_GPIO_IN4_REG 0x43
85#define AB8500_GPIO_IN5_REG 0x44
86#define AB8500_GPIO_IN6_REG 0x45
87#define AB9540_GPIO_IN7_REG 0x46
88#define AB8540_GPIO_VINSEL_REG 0x47
89#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
90#define AB8500_GPIO_ALTFUN_REG 0x50
Patrice Chotard0493e642013-01-08 10:41:02 +010091#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
Patrice Chotard0493e642013-01-08 10:41:02 +010096struct abx500_pinctrl {
97 struct device *dev;
98 struct pinctrl_dev *pctldev;
99 struct abx500_pinctrl_soc_data *soc;
100 struct gpio_chip chip;
101 struct ab8500 *parent;
102 struct mutex lock;
103 u32 irq_base;
Patrice Chotard0493e642013-01-08 10:41:02 +0100104 struct abx500_gpio_irq_cluster *irq_cluster;
105 int irq_cluster_size;
Patrice Chotard0493e642013-01-08 10:41:02 +0100106};
107
108/**
109 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
110 * @chip: Member of the structure abx500_pinctrl
111 */
112static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
113{
114 return container_of(chip, struct abx500_pinctrl, chip);
115}
116
117static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000118 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100119{
120 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
121 u8 pos = offset % 8;
122 u8 val;
123 int ret;
124
125 reg += offset / 8;
126 ret = abx500_get_register_interruptible(pct->dev,
127 AB8500_MISC, reg, &val);
128
129 *bit = !!(val & BIT(pos));
130
131 if (ret < 0)
132 dev_err(pct->dev,
133 "%s read reg =%x, offset=%x failed\n",
134 __func__, reg, offset);
135
136 return ret;
137}
138
139static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000140 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100141{
142 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
143 u8 pos = offset % 8;
144 int ret;
145
146 reg += offset / 8;
147 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000148 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100149 if (ret < 0)
150 dev_err(pct->dev, "%s write failed\n", __func__);
Lee Jones83b423c2013-01-23 13:24:08 +0000151
Patrice Chotard0493e642013-01-08 10:41:02 +0100152 return ret;
153}
Lee Jones83b423c2013-01-23 13:24:08 +0000154
Patrice Chotard0493e642013-01-08 10:41:02 +0100155/**
156 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000157 * @chip: Gpio device
158 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100159 */
160static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
161{
162 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
163 bool bit;
164 int ret;
165
166 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
167 offset, &bit);
168 if (ret < 0) {
169 dev_err(pct->dev, "%s failed\n", __func__);
170 return ret;
171 }
Lee Jones83b423c2013-01-23 13:24:08 +0000172
Patrice Chotard0493e642013-01-08 10:41:02 +0100173 return bit;
174}
175
176static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
177{
178 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
179 int ret;
180
181 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
182 if (ret < 0)
183 dev_err(pct->dev, "%s write failed\n", __func__);
184}
185
186static int abx500_config_pull_updown(struct abx500_pinctrl *pct,
Lee Jones83b423c2013-01-23 13:24:08 +0000187 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100188{
189 u8 pos;
190 int ret;
191 struct pullud *pullud;
192
193 if (!pct->soc->pullud) {
194 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
195 __func__);
196 ret = -EPERM;
197 goto out;
198 }
199
200 pullud = pct->soc->pullud;
201
202 if ((offset < pullud->first_pin)
203 || (offset > pullud->last_pin)) {
204 ret = -EINVAL;
205 goto out;
206 }
207
208 pos = offset << 1;
209
210 ret = abx500_mask_and_set_register_interruptible(pct->dev,
211 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
212 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
213
214out:
215 if (ret < 0)
216 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000217
Patrice Chotard0493e642013-01-08 10:41:02 +0100218 return ret;
219}
220
221static int abx500_gpio_direction_output(struct gpio_chip *chip,
222 unsigned offset,
223 int val)
224{
225 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
226 struct pullud *pullud = pct->soc->pullud;
227 unsigned gpio;
228 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000229
Patrice Chotard0493e642013-01-08 10:41:02 +0100230 /* set direction as output */
231 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
232 if (ret < 0)
233 return ret;
234
235 /* disable pull down */
236 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
237 if (ret < 0)
238 return ret;
239
240 /* if supported, disable both pull down and pull up */
241 gpio = offset + 1;
242 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
243 ret = abx500_config_pull_updown(pct,
244 gpio,
245 ABX500_GPIO_PULL_NONE);
246 if (ret < 0)
247 return ret;
248 }
Lee Jones83b423c2013-01-23 13:24:08 +0000249
Patrice Chotard0493e642013-01-08 10:41:02 +0100250 /* set the output as 1 or 0 */
251 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
252}
253
254static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
255{
256 /* set the register as input */
257 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
258}
259
260static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
261{
262 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000263 /* The AB8500 GPIO numbers are off by one */
264 int gpio = offset + 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100265 int base = pct->irq_base;
266 int i;
267
268 for (i = 0; i < pct->irq_cluster_size; i++) {
269 struct abx500_gpio_irq_cluster *cluster =
270 &pct->irq_cluster[i];
271
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000272 if (gpio >= cluster->start && gpio <= cluster->end)
273 return base + gpio - cluster->start;
Patrice Chotard0493e642013-01-08 10:41:02 +0100274
275 /* Advance by the number of gpios in this cluster */
276 base += cluster->end + cluster->offset - cluster->start + 1;
277 }
278
279 return -EINVAL;
280}
281
282static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000283 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100284{
285 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
286 struct alternate_functions af = pct->soc->alternate_functions[gpio];
287 int ret;
288 int val;
289 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000290
Patrice Chotard0493e642013-01-08 10:41:02 +0100291 const char *modes[] = {
292 [ABX500_DEFAULT] = "default",
293 [ABX500_ALT_A] = "altA",
294 [ABX500_ALT_B] = "altB",
295 [ABX500_ALT_C] = "altC",
296 };
297
298 /* sanity check */
299 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
300 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
301 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
302 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
303 modes[alt_setting]);
304 return -EINVAL;
305 }
306
307 /* on ABx5xx, there is no GPIO0, so adjust the offset */
308 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000309
Patrice Chotard0493e642013-01-08 10:41:02 +0100310 switch (alt_setting) {
311 case ABX500_DEFAULT:
312 /*
313 * for ABx5xx family, default mode is always selected by
314 * writing 0 to GPIOSELx register, except for pins which
315 * support at least ALT_B mode, default mode is selected
316 * by writing 1 to GPIOSELx register
317 */
318 val = 0;
319 if (af.alt_bit1 != UNUSED)
320 val++;
321
322 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
323 offset, val);
324 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000325
Patrice Chotard0493e642013-01-08 10:41:02 +0100326 case ABX500_ALT_A:
327 /*
328 * for ABx5xx family, alt_a mode is always selected by
329 * writing 1 to GPIOSELx register, except for pins which
330 * support at least ALT_B mode, alt_a mode is selected
331 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
332 * register
333 */
334 if (af.alt_bit1 != UNUSED) {
335 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
336 offset, 0);
337 ret = abx500_gpio_set_bits(chip,
338 AB8500_GPIO_ALTFUN_REG,
339 af.alt_bit1,
340 !!(af.alta_val && BIT(0)));
341 if (af.alt_bit2 != UNUSED)
342 ret = abx500_gpio_set_bits(chip,
343 AB8500_GPIO_ALTFUN_REG,
344 af.alt_bit2,
345 !!(af.alta_val && BIT(1)));
346 } else
347 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
348 offset, 1);
349 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000350
Patrice Chotard0493e642013-01-08 10:41:02 +0100351 case ABX500_ALT_B:
352 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
353 offset, 0);
354 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
355 af.alt_bit1, !!(af.altb_val && BIT(0)));
356 if (af.alt_bit2 != UNUSED)
357 ret = abx500_gpio_set_bits(chip,
358 AB8500_GPIO_ALTFUN_REG,
359 af.alt_bit2,
360 !!(af.altb_val && BIT(1)));
361 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000362
Patrice Chotard0493e642013-01-08 10:41:02 +0100363 case ABX500_ALT_C:
364 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
365 offset, 0);
366 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
367 af.alt_bit2, !!(af.altc_val && BIT(0)));
368 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
369 af.alt_bit2, !!(af.altc_val && BIT(1)));
370 break;
371
372 default:
373 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000374
Patrice Chotard0493e642013-01-08 10:41:02 +0100375 return -EINVAL;
376 }
Lee Jones83b423c2013-01-23 13:24:08 +0000377
Patrice Chotard0493e642013-01-08 10:41:02 +0100378 return ret;
379}
380
381static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000382 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100383{
384 u8 mode;
385 bool bit_mode;
386 bool alt_bit1;
387 bool alt_bit2;
388 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
389 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100390 /* on ABx5xx, there is no GPIO0, so adjust the offset */
391 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100392
393 /*
394 * if gpiosel_bit is set to unused,
395 * it means no GPIO or special case
396 */
397 if (af.gpiosel_bit == UNUSED)
398 return ABX500_DEFAULT;
399
400 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100401 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100402 af.gpiosel_bit, &bit_mode);
403 mode = bit_mode;
404
405 /* sanity check */
406 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
407 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
408 dev_err(pct->dev,
409 "alt_bitX value not in correct range (-1 to 7)\n");
410 return -EINVAL;
411 }
Lee Jones83b423c2013-01-23 13:24:08 +0000412
Patrice Chotard0493e642013-01-08 10:41:02 +0100413 /* if alt_bit2 is used, alt_bit1 must be used too */
414 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
415 dev_err(pct->dev,
416 "if alt_bit2 is used, alt_bit1 can't be unused\n");
417 return -EINVAL;
418 }
419
420 /* check if pin use AlternateFunction register */
421 if ((af.alt_bit1 == UNUSED) && (af.alt_bit1 == UNUSED))
422 return mode;
423 /*
424 * if pin GPIOSEL bit is set and pin supports alternate function,
425 * it means DEFAULT mode
426 */
427 if (mode)
428 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000429
Patrice Chotard0493e642013-01-08 10:41:02 +0100430 /*
431 * pin use the AlternatFunction register
432 * read alt_bit1 value
433 */
434 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
435 af.alt_bit1, &alt_bit1);
436
437 if (af.alt_bit2 != UNUSED)
438 /* read alt_bit2 value */
439 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
440 &alt_bit2);
441 else
442 alt_bit2 = 0;
443
444 mode = (alt_bit2 << 1) + alt_bit1;
445 if (mode == af.alta_val)
446 return ABX500_ALT_A;
447 else if (mode == af.altb_val)
448 return ABX500_ALT_B;
449 else
450 return ABX500_ALT_C;
451}
452
453#ifdef CONFIG_DEBUG_FS
454
455#include <linux/seq_file.h>
456
457static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000458 struct pinctrl_dev *pctldev,
459 struct gpio_chip *chip,
460 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100461{
Patrice Chotard0493e642013-01-08 10:41:02 +0100462 const char *label = gpiochip_is_requested(chip, offset - 1);
463 u8 gpio_offset = offset - 1;
464 int mode = -1;
465 bool is_out;
466 bool pull;
Lee Jones83b423c2013-01-23 13:24:08 +0000467
Patrice Chotard0493e642013-01-08 10:41:02 +0100468 const char *modes[] = {
469 [ABX500_DEFAULT] = "default",
470 [ABX500_ALT_A] = "altA",
471 [ABX500_ALT_B] = "altB",
472 [ABX500_ALT_C] = "altC",
473 };
474
475 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
476 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, gpio_offset, &pull);
477
478 if (pctldev)
479 mode = abx500_get_mode(pctldev, chip, offset);
480
481 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s %-9s %s",
482 gpio, label ?: "(none)",
483 is_out ? "out" : "in ",
484 is_out ?
485 (chip->get
486 ? (chip->get(chip, offset) ? "hi" : "lo")
487 : "? ")
488 : (pull ? "pull up" : "pull down"),
489 (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100490}
491
492static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
493{
494 unsigned i;
495 unsigned gpio = chip->base;
496 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
497 struct pinctrl_dev *pctldev = pct->pctldev;
498
499 for (i = 0; i < chip->ngpio; i++, gpio++) {
500 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
501 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
502 seq_printf(s, "\n");
503 }
504}
505
506#else
507static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000508 struct pinctrl_dev *pctldev,
509 struct gpio_chip *chip,
510 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100511{
512}
513#define abx500_gpio_dbg_show NULL
514#endif
515
516int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
517{
518 int gpio = chip->base + offset;
519
520 return pinctrl_request_gpio(gpio);
521}
522
523void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
524{
525 int gpio = chip->base + offset;
526
527 pinctrl_free_gpio(gpio);
528}
529
530static struct gpio_chip abx500gpio_chip = {
531 .label = "abx500-gpio",
532 .owner = THIS_MODULE,
533 .request = abx500_gpio_request,
534 .free = abx500_gpio_free,
535 .direction_input = abx500_gpio_direction_input,
536 .get = abx500_gpio_get,
537 .direction_output = abx500_gpio_direction_output,
538 .set = abx500_gpio_set,
539 .to_irq = abx500_gpio_to_irq,
540 .dbg_show = abx500_gpio_dbg_show,
541};
542
Patrice Chotard0493e642013-01-08 10:41:02 +0100543static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
544{
545 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
546
547 return pct->soc->nfunctions;
548}
549
550static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
551 unsigned function)
552{
553 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
554
555 return pct->soc->functions[function].name;
556}
557
558static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000559 unsigned function,
560 const char * const **groups,
561 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100562{
563 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
564
565 *groups = pct->soc->functions[function].groups;
566 *num_groups = pct->soc->functions[function].ngroups;
567
568 return 0;
569}
570
Patrice Chotard0493e642013-01-08 10:41:02 +0100571static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000572 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100573{
574 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
575 struct gpio_chip *chip = &pct->chip;
576 const struct abx500_pingroup *g;
577 int i;
578 int ret = 0;
579
580 g = &pct->soc->groups[group];
581 if (g->altsetting < 0)
582 return -EINVAL;
583
584 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
585
586 for (i = 0; i < g->npins; i++) {
587 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
588 g->pins[i], g->altsetting);
589
Patrice Chotard0493e642013-01-08 10:41:02 +0100590 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
591 }
Lee Jones83b423c2013-01-23 13:24:08 +0000592
Patrice Chotard0493e642013-01-08 10:41:02 +0100593 return ret;
594}
595
596static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000597 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100598{
599 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
600 const struct abx500_pingroup *g;
601
602 g = &pct->soc->groups[group];
603 if (g->altsetting < 0)
604 return;
605
606 /* FIXME: poke out the mux, set the pin to some default state? */
607 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
608}
609
610int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000611 struct pinctrl_gpio_range *range,
612 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100613{
614 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
615 const struct abx500_pinrange *p;
616 int ret;
617 int i;
618
619 /*
620 * Different ranges have different ways to enable GPIO function on a
621 * pin, so refer back to our local range type, where we handily define
622 * what altfunc enables GPIO for a certain pin.
623 */
624 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
625 p = &pct->soc->gpio_ranges[i];
626 if ((offset >= p->offset) &&
627 (offset < (p->offset + p->npins)))
628 break;
629 }
630
631 if (i == pct->soc->gpio_num_ranges) {
632 dev_err(pct->dev, "%s failed to locate range\n", __func__);
633 return -ENODEV;
634 }
635
636 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
637 p->altfunc, offset);
638
639 ret = abx500_set_mode(pct->pctldev, &pct->chip,
640 offset, p->altfunc);
641 if (ret < 0) {
642 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
643 return ret;
644 }
645
646 return ret;
647}
648
649static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000650 struct pinctrl_gpio_range *range,
651 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100652{
653}
654
655static struct pinmux_ops abx500_pinmux_ops = {
656 .get_functions_count = abx500_pmx_get_funcs_cnt,
657 .get_function_name = abx500_pmx_get_func_name,
658 .get_function_groups = abx500_pmx_get_func_groups,
659 .enable = abx500_pmx_enable,
660 .disable = abx500_pmx_disable,
661 .gpio_request_enable = abx500_gpio_request_enable,
662 .gpio_disable_free = abx500_gpio_disable_free,
663};
664
665static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
666{
667 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
668
669 return pct->soc->ngroups;
670}
671
672static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000673 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100674{
675 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
676
677 return pct->soc->groups[selector].name;
678}
679
680static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000681 unsigned selector,
682 const unsigned **pins,
683 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100684{
685 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
686
687 *pins = pct->soc->groups[selector].pins;
688 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000689
Patrice Chotard0493e642013-01-08 10:41:02 +0100690 return 0;
691}
692
693static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000694 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100695{
696 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
697 struct gpio_chip *chip = &pct->chip;
698
699 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
700 chip->base + offset - 1);
701}
702
703static struct pinctrl_ops abx500_pinctrl_ops = {
704 .get_groups_count = abx500_get_groups_cnt,
705 .get_group_name = abx500_get_group_name,
706 .get_group_pins = abx500_get_group_pins,
707 .pin_dbg_show = abx500_pin_dbg_show,
708};
709
710int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000711 unsigned pin,
712 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100713{
Lee Jones1abeebe2012-12-20 11:11:19 +0000714 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100715}
716
717int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000718 unsigned pin,
719 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100720{
721 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
722 struct pullud *pullud = pct->soc->pullud;
723 struct gpio_chip *chip = &pct->chip;
724 unsigned offset;
725 int ret;
726 enum pin_config_param param = pinconf_to_config_param(config);
727 enum pin_config_param argument = pinconf_to_config_argument(config);
728
729 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
730 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
731 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
732 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000733
Patrice Chotard0493e642013-01-08 10:41:02 +0100734 /* on ABx500, there is no GPIO0, so adjust the offset */
735 offset = pin - 1;
736
737 switch (param) {
738 case PIN_CONFIG_BIAS_PULL_DOWN:
739 /*
740 * if argument = 1 set the pull down
741 * else clear the pull down
742 */
743 ret = abx500_gpio_direction_input(chip, offset);
744 /*
745 * Some chips only support pull down, while some actually
746 * support both pull up and pull down. Such chips have
747 * a "pullud" range specified for the pins that support
748 * both features. If the pin is not within that range, we
749 * fall back to the old bit set that only support pull down.
750 */
751 if (pullud &&
752 pin >= pullud->first_pin &&
753 pin <= pullud->last_pin)
754 ret = abx500_config_pull_updown(pct,
755 pin,
756 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
757 else
758 /* Chip only supports pull down */
759 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
760 offset, argument ? 0 : 1);
761 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000762
Patrice Chotard0493e642013-01-08 10:41:02 +0100763 case PIN_CONFIG_OUTPUT:
764 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +0000765
Patrice Chotard0493e642013-01-08 10:41:02 +0100766 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000767
Patrice Chotard0493e642013-01-08 10:41:02 +0100768 default:
769 dev_err(chip->dev, "illegal configuration requested\n");
Lee Jones83b423c2013-01-23 13:24:08 +0000770
Patrice Chotard0493e642013-01-08 10:41:02 +0100771 return -EINVAL;
772 }
Lee Jones83b423c2013-01-23 13:24:08 +0000773
Patrice Chotard0493e642013-01-08 10:41:02 +0100774 return ret;
775}
776
777static struct pinconf_ops abx500_pinconf_ops = {
778 .pin_config_get = abx500_pin_config_get,
779 .pin_config_set = abx500_pin_config_set,
780};
781
782static struct pinctrl_desc abx500_pinctrl_desc = {
783 .name = "pinctrl-abx500",
784 .pctlops = &abx500_pinctrl_ops,
785 .pmxops = &abx500_pinmux_ops,
786 .confops = &abx500_pinconf_ops,
787 .owner = THIS_MODULE,
788};
789
790static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
791{
792 unsigned int lowest = 0;
793 unsigned int highest = 0;
794 unsigned int npins = 0;
795 int i;
796
797 /*
798 * Compute number of GPIOs from the last SoC gpio range descriptors
799 * These ranges may include "holes" but the GPIO number space shall
800 * still be homogeneous, so we need to detect and account for any
801 * such holes so that these are included in the number of GPIO pins.
802 */
803 for (i = 0; i < soc->gpio_num_ranges; i++) {
804 unsigned gstart;
805 unsigned gend;
806 const struct abx500_pinrange *p;
807
808 p = &soc->gpio_ranges[i];
809 gstart = p->offset;
810 gend = p->offset + p->npins - 1;
811
812 if (i == 0) {
813 /* First iteration, set start values */
814 lowest = gstart;
815 highest = gend;
816 } else {
817 if (gstart < lowest)
818 lowest = gstart;
819 if (gend > highest)
820 highest = gend;
821 }
822 }
823 /* this gives the absolute number of pins */
824 npins = highest - lowest + 1;
825 return npins;
826}
827
Lee Jonesf30a3832013-01-31 11:07:40 +0000828static const struct of_device_id abx500_gpio_match[] = {
829 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
830 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
831 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
832 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
833};
834
Patrice Chotard0493e642013-01-08 10:41:02 +0100835static int abx500_gpio_probe(struct platform_device *pdev)
836{
837 struct ab8500_platform_data *abx500_pdata =
838 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +0000839 struct abx500_gpio_platform_data *pdata = NULL;
840 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +0100841 struct abx500_pinctrl *pct;
842 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +0000843 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +0000844 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +0100845 int i;
846
Lee Jonesf30a3832013-01-31 11:07:40 +0000847 if (abx500_pdata)
848 pdata = abx500_pdata->gpio;
Lee Jones83b423c2013-01-23 13:24:08 +0000849 if (!pdata) {
Lee Jonesf30a3832013-01-31 11:07:40 +0000850 if (np) {
851 const struct of_device_id *match;
852
853 match = of_match_device(abx500_gpio_match, &pdev->dev);
854 if (!match)
855 return -ENODEV;
856 id = (unsigned long)match->data;
857 } else {
858 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
859 return -ENODEV;
860 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100861 }
862
Lee Jonesf30a3832013-01-31 11:07:40 +0000863 if (platid)
864 id = platid->driver_data;
865
Patrice Chotard0493e642013-01-08 10:41:02 +0100866 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
867 GFP_KERNEL);
868 if (pct == NULL) {
869 dev_err(&pdev->dev,
870 "failed to allocate memory for pct\n");
871 return -ENOMEM;
872 }
873
874 pct->dev = &pdev->dev;
875 pct->parent = dev_get_drvdata(pdev->dev.parent);
876 pct->chip = abx500gpio_chip;
877 pct->chip.dev = &pdev->dev;
878 pct->chip.base = pdata->gpio_base;
879 pct->irq_base = pdata->irq_base;
Lee Jonesf30a3832013-01-31 11:07:40 +0000880 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +0100881
882 /* initialize the lock */
883 mutex_init(&pct->lock);
884
885 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +0000886 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +0100887 case PINCTRL_AB8500:
888 abx500_pinctrl_ab8500_init(&pct->soc);
889 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +0100890 case PINCTRL_AB8540:
891 abx500_pinctrl_ab8540_init(&pct->soc);
892 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +0100893 case PINCTRL_AB9540:
894 abx500_pinctrl_ab9540_init(&pct->soc);
895 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +0100896 case PINCTRL_AB8505:
897 abx500_pinctrl_ab8505_init(&pct->soc);
898 break;
Patrice Chotard0493e642013-01-08 10:41:02 +0100899 default:
900 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n",
901 (int) platid->driver_data);
Lee Jonesd41e35c2013-01-16 09:17:13 +0000902 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +0100903 return -EINVAL;
904 }
905
906 if (!pct->soc) {
907 dev_err(&pdev->dev, "Invalid SOC data\n");
Lee Jonesd41e35c2013-01-16 09:17:13 +0000908 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +0100909 return -EINVAL;
910 }
911
912 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
913 pct->irq_cluster = pct->soc->gpio_irq_cluster;
914 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +0100915
Patrice Chotard0493e642013-01-08 10:41:02 +0100916 ret = gpiochip_add(&pct->chip);
917 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +0000918 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesd41e35c2013-01-16 09:17:13 +0000919 mutex_destroy(&pct->lock);
Lee Jonesac652d72013-01-31 10:43:00 +0000920 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100921 }
922 dev_info(&pdev->dev, "added gpiochip\n");
923
924 abx500_pinctrl_desc.pins = pct->soc->pins;
925 abx500_pinctrl_desc.npins = pct->soc->npins;
926 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
927 if (!pct->pctldev) {
928 dev_err(&pdev->dev,
929 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +0000930 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100931 goto out_rem_chip;
932 }
933 dev_info(&pdev->dev, "registered pin controller\n");
934
935 /* We will handle a range of GPIO pins */
936 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
937 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
938
939 ret = gpiochip_add_pin_range(&pct->chip,
940 dev_name(&pdev->dev),
941 p->offset - 1, p->offset, p->npins);
942 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +0000943 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +0100944 }
945
946 platform_set_drvdata(pdev, pct);
947 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
948
949 return 0;
950
951out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +0000952 err = gpiochip_remove(&pct->chip);
953 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +0100954 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +0000955
Patrice Chotard0493e642013-01-08 10:41:02 +0100956 mutex_destroy(&pct->lock);
957 return ret;
958}
959
Lee Jones83b423c2013-01-23 13:24:08 +0000960/**
Patrice Chotard0493e642013-01-08 10:41:02 +0100961 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +0000962 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +0100963 */
964static int abx500_gpio_remove(struct platform_device *pdev)
965{
966 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
967 int ret;
968
969 ret = gpiochip_remove(&pct->chip);
970 if (ret < 0) {
971 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
972 ret);
973 return ret;
974 }
975
976 mutex_destroy(&pct->lock);
977
978 return 0;
979}
980
981static const struct platform_device_id abx500_pinctrl_id[] = {
982 { "pinctrl-ab8500", PINCTRL_AB8500 },
983 { "pinctrl-ab8540", PINCTRL_AB8540 },
984 { "pinctrl-ab9540", PINCTRL_AB9540 },
985 { "pinctrl-ab8505", PINCTRL_AB8505 },
986 { },
987};
988
989static struct platform_driver abx500_gpio_driver = {
990 .driver = {
991 .name = "abx500-gpio",
992 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +0000993 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +0100994 },
995 .probe = abx500_gpio_probe,
996 .remove = abx500_gpio_remove,
997 .id_table = abx500_pinctrl_id,
998};
999
1000static int __init abx500_gpio_init(void)
1001{
1002 return platform_driver_register(&abx500_gpio_driver);
1003}
1004core_initcall(abx500_gpio_init);
1005
1006MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1007MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1008MODULE_ALIAS("platform:abx500-gpio");
1009MODULE_LICENSE("GPL v2");