blob: 720e77998e64b15fd25a89a33bc668f8cd591a9c [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;
Patrice Chotard0493e642013-01-08 10:41:02 +0100103 struct abx500_gpio_irq_cluster *irq_cluster;
104 int irq_cluster_size;
Patrice Chotard0493e642013-01-08 10:41:02 +0100105};
106
107/**
108 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
109 * @chip: Member of the structure abx500_pinctrl
110 */
111static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
112{
113 return container_of(chip, struct abx500_pinctrl, chip);
114}
115
116static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000117 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100118{
119 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
120 u8 pos = offset % 8;
121 u8 val;
122 int ret;
123
124 reg += offset / 8;
125 ret = abx500_get_register_interruptible(pct->dev,
126 AB8500_MISC, reg, &val);
127
128 *bit = !!(val & BIT(pos));
129
130 if (ret < 0)
131 dev_err(pct->dev,
132 "%s read reg =%x, offset=%x failed\n",
133 __func__, reg, offset);
134
135 return ret;
136}
137
138static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000139 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100140{
141 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
142 u8 pos = offset % 8;
143 int ret;
144
145 reg += offset / 8;
146 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000147 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100148 if (ret < 0)
149 dev_err(pct->dev, "%s write failed\n", __func__);
Lee Jones83b423c2013-01-23 13:24:08 +0000150
Patrice Chotard0493e642013-01-08 10:41:02 +0100151 return ret;
152}
Lee Jones83b423c2013-01-23 13:24:08 +0000153
Patrice Chotard0493e642013-01-08 10:41:02 +0100154/**
155 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000156 * @chip: Gpio device
157 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100158 */
159static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
160{
161 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
162 bool bit;
163 int ret;
164
165 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
166 offset, &bit);
167 if (ret < 0) {
168 dev_err(pct->dev, "%s failed\n", __func__);
169 return ret;
170 }
Lee Jones83b423c2013-01-23 13:24:08 +0000171
Patrice Chotard0493e642013-01-08 10:41:02 +0100172 return bit;
173}
174
175static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
176{
177 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
178 int ret;
179
180 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
181 if (ret < 0)
182 dev_err(pct->dev, "%s write failed\n", __func__);
183}
184
185static int abx500_config_pull_updown(struct abx500_pinctrl *pct,
Lee Jones83b423c2013-01-23 13:24:08 +0000186 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100187{
188 u8 pos;
189 int ret;
190 struct pullud *pullud;
191
192 if (!pct->soc->pullud) {
193 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
194 __func__);
195 ret = -EPERM;
196 goto out;
197 }
198
199 pullud = pct->soc->pullud;
200
201 if ((offset < pullud->first_pin)
202 || (offset > pullud->last_pin)) {
203 ret = -EINVAL;
204 goto out;
205 }
206
207 pos = offset << 1;
208
209 ret = abx500_mask_and_set_register_interruptible(pct->dev,
210 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
211 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
212
213out:
214 if (ret < 0)
215 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000216
Patrice Chotard0493e642013-01-08 10:41:02 +0100217 return ret;
218}
219
220static int abx500_gpio_direction_output(struct gpio_chip *chip,
221 unsigned offset,
222 int val)
223{
224 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
225 struct pullud *pullud = pct->soc->pullud;
226 unsigned gpio;
227 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000228
Patrice Chotard0493e642013-01-08 10:41:02 +0100229 /* set direction as output */
230 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
231 if (ret < 0)
232 return ret;
233
234 /* disable pull down */
235 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
236 if (ret < 0)
237 return ret;
238
239 /* if supported, disable both pull down and pull up */
240 gpio = offset + 1;
241 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
242 ret = abx500_config_pull_updown(pct,
243 gpio,
244 ABX500_GPIO_PULL_NONE);
245 if (ret < 0)
246 return ret;
247 }
Lee Jones83b423c2013-01-23 13:24:08 +0000248
Patrice Chotard0493e642013-01-08 10:41:02 +0100249 /* set the output as 1 or 0 */
250 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
251}
252
253static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
254{
255 /* set the register as input */
256 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
257}
258
259static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
260{
261 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000262 /* The AB8500 GPIO numbers are off by one */
263 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000264 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100265 int i;
266
267 for (i = 0; i < pct->irq_cluster_size; i++) {
268 struct abx500_gpio_irq_cluster *cluster =
269 &pct->irq_cluster[i];
270
Lee Jonesa6a16d22013-01-31 09:57:52 +0000271 if (gpio >= cluster->start && gpio <= cluster->end) {
272 /*
273 * The ABx500 GPIO's associated IRQs are clustered together
274 * throughout the interrupt numbers at irregular intervals.
275 * To solve this quandry, we have placed the read-in values
276 * into the cluster information table.
277 */
278 hwirq = gpio + cluster->to_irq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100279
Lee Jonesa6a16d22013-01-31 09:57:52 +0000280 return irq_create_mapping(pct->parent->domain, hwirq);
281 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100282 }
283
284 return -EINVAL;
285}
286
287static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000288 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100289{
290 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
291 struct alternate_functions af = pct->soc->alternate_functions[gpio];
292 int ret;
293 int val;
294 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000295
Patrice Chotard0493e642013-01-08 10:41:02 +0100296 const char *modes[] = {
297 [ABX500_DEFAULT] = "default",
298 [ABX500_ALT_A] = "altA",
299 [ABX500_ALT_B] = "altB",
300 [ABX500_ALT_C] = "altC",
301 };
302
303 /* sanity check */
304 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
305 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
306 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
307 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
308 modes[alt_setting]);
309 return -EINVAL;
310 }
311
312 /* on ABx5xx, there is no GPIO0, so adjust the offset */
313 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000314
Patrice Chotard0493e642013-01-08 10:41:02 +0100315 switch (alt_setting) {
316 case ABX500_DEFAULT:
317 /*
318 * for ABx5xx family, default mode is always selected by
319 * writing 0 to GPIOSELx register, except for pins which
320 * support at least ALT_B mode, default mode is selected
321 * by writing 1 to GPIOSELx register
322 */
323 val = 0;
324 if (af.alt_bit1 != UNUSED)
325 val++;
326
327 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
328 offset, val);
329 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000330
Patrice Chotard0493e642013-01-08 10:41:02 +0100331 case ABX500_ALT_A:
332 /*
333 * for ABx5xx family, alt_a mode is always selected by
334 * writing 1 to GPIOSELx register, except for pins which
335 * support at least ALT_B mode, alt_a mode is selected
336 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
337 * register
338 */
339 if (af.alt_bit1 != UNUSED) {
340 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
341 offset, 0);
342 ret = abx500_gpio_set_bits(chip,
343 AB8500_GPIO_ALTFUN_REG,
344 af.alt_bit1,
345 !!(af.alta_val && BIT(0)));
346 if (af.alt_bit2 != UNUSED)
347 ret = abx500_gpio_set_bits(chip,
348 AB8500_GPIO_ALTFUN_REG,
349 af.alt_bit2,
350 !!(af.alta_val && BIT(1)));
351 } else
352 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
353 offset, 1);
354 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000355
Patrice Chotard0493e642013-01-08 10:41:02 +0100356 case ABX500_ALT_B:
357 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
358 offset, 0);
359 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
360 af.alt_bit1, !!(af.altb_val && BIT(0)));
361 if (af.alt_bit2 != UNUSED)
362 ret = abx500_gpio_set_bits(chip,
363 AB8500_GPIO_ALTFUN_REG,
364 af.alt_bit2,
365 !!(af.altb_val && BIT(1)));
366 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000367
Patrice Chotard0493e642013-01-08 10:41:02 +0100368 case ABX500_ALT_C:
369 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
370 offset, 0);
371 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
372 af.alt_bit2, !!(af.altc_val && BIT(0)));
373 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
374 af.alt_bit2, !!(af.altc_val && BIT(1)));
375 break;
376
377 default:
378 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000379
Patrice Chotard0493e642013-01-08 10:41:02 +0100380 return -EINVAL;
381 }
Lee Jones83b423c2013-01-23 13:24:08 +0000382
Patrice Chotard0493e642013-01-08 10:41:02 +0100383 return ret;
384}
385
386static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000387 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100388{
389 u8 mode;
390 bool bit_mode;
391 bool alt_bit1;
392 bool alt_bit2;
393 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
394 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100395 /* on ABx5xx, there is no GPIO0, so adjust the offset */
396 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100397
398 /*
399 * if gpiosel_bit is set to unused,
400 * it means no GPIO or special case
401 */
402 if (af.gpiosel_bit == UNUSED)
403 return ABX500_DEFAULT;
404
405 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100406 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100407 af.gpiosel_bit, &bit_mode);
408 mode = bit_mode;
409
410 /* sanity check */
411 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
412 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
413 dev_err(pct->dev,
414 "alt_bitX value not in correct range (-1 to 7)\n");
415 return -EINVAL;
416 }
Lee Jones83b423c2013-01-23 13:24:08 +0000417
Patrice Chotard0493e642013-01-08 10:41:02 +0100418 /* if alt_bit2 is used, alt_bit1 must be used too */
419 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
420 dev_err(pct->dev,
421 "if alt_bit2 is used, alt_bit1 can't be unused\n");
422 return -EINVAL;
423 }
424
425 /* check if pin use AlternateFunction register */
426 if ((af.alt_bit1 == UNUSED) && (af.alt_bit1 == UNUSED))
427 return mode;
428 /*
429 * if pin GPIOSEL bit is set and pin supports alternate function,
430 * it means DEFAULT mode
431 */
432 if (mode)
433 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000434
Patrice Chotard0493e642013-01-08 10:41:02 +0100435 /*
436 * pin use the AlternatFunction register
437 * read alt_bit1 value
438 */
439 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
440 af.alt_bit1, &alt_bit1);
441
442 if (af.alt_bit2 != UNUSED)
443 /* read alt_bit2 value */
444 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
445 &alt_bit2);
446 else
447 alt_bit2 = 0;
448
449 mode = (alt_bit2 << 1) + alt_bit1;
450 if (mode == af.alta_val)
451 return ABX500_ALT_A;
452 else if (mode == af.altb_val)
453 return ABX500_ALT_B;
454 else
455 return ABX500_ALT_C;
456}
457
458#ifdef CONFIG_DEBUG_FS
459
460#include <linux/seq_file.h>
461
462static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000463 struct pinctrl_dev *pctldev,
464 struct gpio_chip *chip,
465 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100466{
Patrice Chotard0493e642013-01-08 10:41:02 +0100467 const char *label = gpiochip_is_requested(chip, offset - 1);
468 u8 gpio_offset = offset - 1;
469 int mode = -1;
470 bool is_out;
471 bool pull;
Lee Jones83b423c2013-01-23 13:24:08 +0000472
Patrice Chotard0493e642013-01-08 10:41:02 +0100473 const char *modes[] = {
474 [ABX500_DEFAULT] = "default",
475 [ABX500_ALT_A] = "altA",
476 [ABX500_ALT_B] = "altB",
477 [ABX500_ALT_C] = "altC",
478 };
479
480 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
481 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, gpio_offset, &pull);
482
483 if (pctldev)
484 mode = abx500_get_mode(pctldev, chip, offset);
485
486 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s %-9s %s",
487 gpio, label ?: "(none)",
488 is_out ? "out" : "in ",
489 is_out ?
490 (chip->get
491 ? (chip->get(chip, offset) ? "hi" : "lo")
492 : "? ")
493 : (pull ? "pull up" : "pull down"),
494 (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100495}
496
497static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
498{
499 unsigned i;
500 unsigned gpio = chip->base;
501 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
502 struct pinctrl_dev *pctldev = pct->pctldev;
503
504 for (i = 0; i < chip->ngpio; i++, gpio++) {
505 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
506 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
507 seq_printf(s, "\n");
508 }
509}
510
511#else
512static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000513 struct pinctrl_dev *pctldev,
514 struct gpio_chip *chip,
515 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100516{
517}
518#define abx500_gpio_dbg_show NULL
519#endif
520
521int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
522{
523 int gpio = chip->base + offset;
524
525 return pinctrl_request_gpio(gpio);
526}
527
528void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
529{
530 int gpio = chip->base + offset;
531
532 pinctrl_free_gpio(gpio);
533}
534
535static struct gpio_chip abx500gpio_chip = {
536 .label = "abx500-gpio",
537 .owner = THIS_MODULE,
538 .request = abx500_gpio_request,
539 .free = abx500_gpio_free,
540 .direction_input = abx500_gpio_direction_input,
541 .get = abx500_gpio_get,
542 .direction_output = abx500_gpio_direction_output,
543 .set = abx500_gpio_set,
544 .to_irq = abx500_gpio_to_irq,
545 .dbg_show = abx500_gpio_dbg_show,
546};
547
Patrice Chotard0493e642013-01-08 10:41:02 +0100548static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
549{
550 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
551
552 return pct->soc->nfunctions;
553}
554
555static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
556 unsigned function)
557{
558 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
559
560 return pct->soc->functions[function].name;
561}
562
563static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000564 unsigned function,
565 const char * const **groups,
566 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100567{
568 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
569
570 *groups = pct->soc->functions[function].groups;
571 *num_groups = pct->soc->functions[function].ngroups;
572
573 return 0;
574}
575
Patrice Chotard0493e642013-01-08 10:41:02 +0100576static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000577 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100578{
579 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
580 struct gpio_chip *chip = &pct->chip;
581 const struct abx500_pingroup *g;
582 int i;
583 int ret = 0;
584
585 g = &pct->soc->groups[group];
586 if (g->altsetting < 0)
587 return -EINVAL;
588
589 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
590
591 for (i = 0; i < g->npins; i++) {
592 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
593 g->pins[i], g->altsetting);
594
Patrice Chotard0493e642013-01-08 10:41:02 +0100595 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
596 }
Lee Jones83b423c2013-01-23 13:24:08 +0000597
Patrice Chotard0493e642013-01-08 10:41:02 +0100598 return ret;
599}
600
601static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000602 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100603{
604 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
605 const struct abx500_pingroup *g;
606
607 g = &pct->soc->groups[group];
608 if (g->altsetting < 0)
609 return;
610
611 /* FIXME: poke out the mux, set the pin to some default state? */
612 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
613}
614
615int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000616 struct pinctrl_gpio_range *range,
617 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100618{
619 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
620 const struct abx500_pinrange *p;
621 int ret;
622 int i;
623
624 /*
625 * Different ranges have different ways to enable GPIO function on a
626 * pin, so refer back to our local range type, where we handily define
627 * what altfunc enables GPIO for a certain pin.
628 */
629 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
630 p = &pct->soc->gpio_ranges[i];
631 if ((offset >= p->offset) &&
632 (offset < (p->offset + p->npins)))
633 break;
634 }
635
636 if (i == pct->soc->gpio_num_ranges) {
637 dev_err(pct->dev, "%s failed to locate range\n", __func__);
638 return -ENODEV;
639 }
640
641 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
642 p->altfunc, offset);
643
644 ret = abx500_set_mode(pct->pctldev, &pct->chip,
645 offset, p->altfunc);
646 if (ret < 0) {
647 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
648 return ret;
649 }
650
651 return ret;
652}
653
654static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000655 struct pinctrl_gpio_range *range,
656 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100657{
658}
659
660static struct pinmux_ops abx500_pinmux_ops = {
661 .get_functions_count = abx500_pmx_get_funcs_cnt,
662 .get_function_name = abx500_pmx_get_func_name,
663 .get_function_groups = abx500_pmx_get_func_groups,
664 .enable = abx500_pmx_enable,
665 .disable = abx500_pmx_disable,
666 .gpio_request_enable = abx500_gpio_request_enable,
667 .gpio_disable_free = abx500_gpio_disable_free,
668};
669
670static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
671{
672 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
673
674 return pct->soc->ngroups;
675}
676
677static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000678 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100679{
680 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
681
682 return pct->soc->groups[selector].name;
683}
684
685static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000686 unsigned selector,
687 const unsigned **pins,
688 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100689{
690 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
691
692 *pins = pct->soc->groups[selector].pins;
693 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000694
Patrice Chotard0493e642013-01-08 10:41:02 +0100695 return 0;
696}
697
698static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000699 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100700{
701 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
702 struct gpio_chip *chip = &pct->chip;
703
704 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
705 chip->base + offset - 1);
706}
707
708static struct pinctrl_ops abx500_pinctrl_ops = {
709 .get_groups_count = abx500_get_groups_cnt,
710 .get_group_name = abx500_get_group_name,
711 .get_group_pins = abx500_get_group_pins,
712 .pin_dbg_show = abx500_pin_dbg_show,
713};
714
715int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000716 unsigned pin,
717 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100718{
Lee Jones1abeebe2012-12-20 11:11:19 +0000719 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100720}
721
722int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000723 unsigned pin,
724 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100725{
726 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
727 struct pullud *pullud = pct->soc->pullud;
728 struct gpio_chip *chip = &pct->chip;
729 unsigned offset;
730 int ret;
731 enum pin_config_param param = pinconf_to_config_param(config);
732 enum pin_config_param argument = pinconf_to_config_argument(config);
733
734 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
735 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
736 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
737 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000738
Patrice Chotard0493e642013-01-08 10:41:02 +0100739 /* on ABx500, there is no GPIO0, so adjust the offset */
740 offset = pin - 1;
741
742 switch (param) {
743 case PIN_CONFIG_BIAS_PULL_DOWN:
744 /*
745 * if argument = 1 set the pull down
746 * else clear the pull down
747 */
748 ret = abx500_gpio_direction_input(chip, offset);
749 /*
750 * Some chips only support pull down, while some actually
751 * support both pull up and pull down. Such chips have
752 * a "pullud" range specified for the pins that support
753 * both features. If the pin is not within that range, we
754 * fall back to the old bit set that only support pull down.
755 */
756 if (pullud &&
757 pin >= pullud->first_pin &&
758 pin <= pullud->last_pin)
759 ret = abx500_config_pull_updown(pct,
760 pin,
761 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
762 else
763 /* Chip only supports pull down */
764 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
765 offset, argument ? 0 : 1);
766 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000767
Patrice Chotard0493e642013-01-08 10:41:02 +0100768 case PIN_CONFIG_OUTPUT:
769 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +0000770
Patrice Chotard0493e642013-01-08 10:41:02 +0100771 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000772
Patrice Chotard0493e642013-01-08 10:41:02 +0100773 default:
774 dev_err(chip->dev, "illegal configuration requested\n");
Lee Jones83b423c2013-01-23 13:24:08 +0000775
Patrice Chotard0493e642013-01-08 10:41:02 +0100776 return -EINVAL;
777 }
Lee Jones83b423c2013-01-23 13:24:08 +0000778
Patrice Chotard0493e642013-01-08 10:41:02 +0100779 return ret;
780}
781
782static struct pinconf_ops abx500_pinconf_ops = {
783 .pin_config_get = abx500_pin_config_get,
784 .pin_config_set = abx500_pin_config_set,
785};
786
787static struct pinctrl_desc abx500_pinctrl_desc = {
788 .name = "pinctrl-abx500",
789 .pctlops = &abx500_pinctrl_ops,
790 .pmxops = &abx500_pinmux_ops,
791 .confops = &abx500_pinconf_ops,
792 .owner = THIS_MODULE,
793};
794
795static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
796{
797 unsigned int lowest = 0;
798 unsigned int highest = 0;
799 unsigned int npins = 0;
800 int i;
801
802 /*
803 * Compute number of GPIOs from the last SoC gpio range descriptors
804 * These ranges may include "holes" but the GPIO number space shall
805 * still be homogeneous, so we need to detect and account for any
806 * such holes so that these are included in the number of GPIO pins.
807 */
808 for (i = 0; i < soc->gpio_num_ranges; i++) {
809 unsigned gstart;
810 unsigned gend;
811 const struct abx500_pinrange *p;
812
813 p = &soc->gpio_ranges[i];
814 gstart = p->offset;
815 gend = p->offset + p->npins - 1;
816
817 if (i == 0) {
818 /* First iteration, set start values */
819 lowest = gstart;
820 highest = gend;
821 } else {
822 if (gstart < lowest)
823 lowest = gstart;
824 if (gend > highest)
825 highest = gend;
826 }
827 }
828 /* this gives the absolute number of pins */
829 npins = highest - lowest + 1;
830 return npins;
831}
832
Lee Jonesf30a3832013-01-31 11:07:40 +0000833static const struct of_device_id abx500_gpio_match[] = {
834 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
835 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
836 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
837 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
838};
839
Patrice Chotard0493e642013-01-08 10:41:02 +0100840static int abx500_gpio_probe(struct platform_device *pdev)
841{
842 struct ab8500_platform_data *abx500_pdata =
843 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +0000844 struct abx500_gpio_platform_data *pdata = NULL;
845 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +0100846 struct abx500_pinctrl *pct;
847 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +0000848 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +0000849 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +0100850 int i;
851
Lee Jonesf30a3832013-01-31 11:07:40 +0000852 if (abx500_pdata)
853 pdata = abx500_pdata->gpio;
Lee Jones83b423c2013-01-23 13:24:08 +0000854 if (!pdata) {
Lee Jonesf30a3832013-01-31 11:07:40 +0000855 if (np) {
856 const struct of_device_id *match;
857
858 match = of_match_device(abx500_gpio_match, &pdev->dev);
859 if (!match)
860 return -ENODEV;
861 id = (unsigned long)match->data;
862 } else {
863 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
864 return -ENODEV;
865 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100866 }
867
Lee Jonesf30a3832013-01-31 11:07:40 +0000868 if (platid)
869 id = platid->driver_data;
870
Patrice Chotard0493e642013-01-08 10:41:02 +0100871 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
872 GFP_KERNEL);
873 if (pct == NULL) {
874 dev_err(&pdev->dev,
875 "failed to allocate memory for pct\n");
876 return -ENOMEM;
877 }
878
879 pct->dev = &pdev->dev;
880 pct->parent = dev_get_drvdata(pdev->dev.parent);
881 pct->chip = abx500gpio_chip;
882 pct->chip.dev = &pdev->dev;
883 pct->chip.base = pdata->gpio_base;
Lee Jonesf30a3832013-01-31 11:07:40 +0000884 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +0100885
886 /* initialize the lock */
887 mutex_init(&pct->lock);
888
889 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +0000890 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +0100891 case PINCTRL_AB8500:
892 abx500_pinctrl_ab8500_init(&pct->soc);
893 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +0100894 case PINCTRL_AB8540:
895 abx500_pinctrl_ab8540_init(&pct->soc);
896 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +0100897 case PINCTRL_AB9540:
898 abx500_pinctrl_ab9540_init(&pct->soc);
899 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +0100900 case PINCTRL_AB8505:
901 abx500_pinctrl_ab8505_init(&pct->soc);
902 break;
Patrice Chotard0493e642013-01-08 10:41:02 +0100903 default:
904 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n",
905 (int) platid->driver_data);
Lee Jonesd41e35c2013-01-16 09:17:13 +0000906 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +0100907 return -EINVAL;
908 }
909
910 if (!pct->soc) {
911 dev_err(&pdev->dev, "Invalid SOC data\n");
Lee Jonesd41e35c2013-01-16 09:17:13 +0000912 mutex_destroy(&pct->lock);
Patrice Chotard0493e642013-01-08 10:41:02 +0100913 return -EINVAL;
914 }
915
916 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
917 pct->irq_cluster = pct->soc->gpio_irq_cluster;
918 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +0100919
Patrice Chotard0493e642013-01-08 10:41:02 +0100920 ret = gpiochip_add(&pct->chip);
921 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +0000922 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesd41e35c2013-01-16 09:17:13 +0000923 mutex_destroy(&pct->lock);
Lee Jonesac652d72013-01-31 10:43:00 +0000924 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100925 }
926 dev_info(&pdev->dev, "added gpiochip\n");
927
928 abx500_pinctrl_desc.pins = pct->soc->pins;
929 abx500_pinctrl_desc.npins = pct->soc->npins;
930 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
931 if (!pct->pctldev) {
932 dev_err(&pdev->dev,
933 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +0000934 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100935 goto out_rem_chip;
936 }
937 dev_info(&pdev->dev, "registered pin controller\n");
938
939 /* We will handle a range of GPIO pins */
940 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
941 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
942
943 ret = gpiochip_add_pin_range(&pct->chip,
944 dev_name(&pdev->dev),
945 p->offset - 1, p->offset, p->npins);
946 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +0000947 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +0100948 }
949
950 platform_set_drvdata(pdev, pct);
951 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
952
953 return 0;
954
955out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +0000956 err = gpiochip_remove(&pct->chip);
957 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +0100958 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +0000959
Patrice Chotard0493e642013-01-08 10:41:02 +0100960 mutex_destroy(&pct->lock);
961 return ret;
962}
963
Lee Jones83b423c2013-01-23 13:24:08 +0000964/**
Patrice Chotard0493e642013-01-08 10:41:02 +0100965 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +0000966 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +0100967 */
968static int abx500_gpio_remove(struct platform_device *pdev)
969{
970 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
971 int ret;
972
973 ret = gpiochip_remove(&pct->chip);
974 if (ret < 0) {
975 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
976 ret);
977 return ret;
978 }
979
980 mutex_destroy(&pct->lock);
981
982 return 0;
983}
984
985static const struct platform_device_id abx500_pinctrl_id[] = {
986 { "pinctrl-ab8500", PINCTRL_AB8500 },
987 { "pinctrl-ab8540", PINCTRL_AB8540 },
988 { "pinctrl-ab9540", PINCTRL_AB9540 },
989 { "pinctrl-ab8505", PINCTRL_AB8505 },
990 { },
991};
992
993static struct platform_driver abx500_gpio_driver = {
994 .driver = {
995 .name = "abx500-gpio",
996 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +0000997 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +0100998 },
999 .probe = abx500_gpio_probe,
1000 .remove = abx500_gpio_remove,
1001 .id_table = abx500_pinctrl_id,
1002};
1003
1004static int __init abx500_gpio_init(void)
1005{
1006 return platform_driver_register(&abx500_gpio_driver);
1007}
1008core_initcall(abx500_gpio_init);
1009
1010MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1011MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1012MODULE_ALIAS("platform:abx500-gpio");
1013MODULE_LICENSE("GPL v2");