blob: f13a57b13b05d20e359f34b70d4769640f15c8e6 [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>
Patrice Chotard64a45c92013-06-20 16:04:59 +020033#include <linux/pinctrl/machine.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010034
35#include "pinctrl-abx500.h"
Patrice Chotard64a45c92013-06-20 16:04:59 +020036#include "pinconf.h"
Patrice Chotard0493e642013-01-08 10:41:02 +010037
38/*
39 * The AB9540 and AB8540 GPIO support are extended versions
40 * of the AB8500 GPIO support.
41 * The AB9540 supports an additional (7th) register so that
42 * more GPIO may be configured and used.
43 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
44 * internal pull-up and pull-down capabilities.
45 */
46
47/*
48 * GPIO registers offset
49 * Bank: 0x10
50 */
51#define AB8500_GPIO_SEL1_REG 0x00
52#define AB8500_GPIO_SEL2_REG 0x01
53#define AB8500_GPIO_SEL3_REG 0x02
54#define AB8500_GPIO_SEL4_REG 0x03
55#define AB8500_GPIO_SEL5_REG 0x04
56#define AB8500_GPIO_SEL6_REG 0x05
57#define AB9540_GPIO_SEL7_REG 0x06
58
59#define AB8500_GPIO_DIR1_REG 0x10
60#define AB8500_GPIO_DIR2_REG 0x11
61#define AB8500_GPIO_DIR3_REG 0x12
62#define AB8500_GPIO_DIR4_REG 0x13
63#define AB8500_GPIO_DIR5_REG 0x14
64#define AB8500_GPIO_DIR6_REG 0x15
65#define AB9540_GPIO_DIR7_REG 0x16
66
67#define AB8500_GPIO_OUT1_REG 0x20
68#define AB8500_GPIO_OUT2_REG 0x21
69#define AB8500_GPIO_OUT3_REG 0x22
70#define AB8500_GPIO_OUT4_REG 0x23
71#define AB8500_GPIO_OUT5_REG 0x24
72#define AB8500_GPIO_OUT6_REG 0x25
73#define AB9540_GPIO_OUT7_REG 0x26
74
75#define AB8500_GPIO_PUD1_REG 0x30
76#define AB8500_GPIO_PUD2_REG 0x31
77#define AB8500_GPIO_PUD3_REG 0x32
78#define AB8500_GPIO_PUD4_REG 0x33
79#define AB8500_GPIO_PUD5_REG 0x34
80#define AB8500_GPIO_PUD6_REG 0x35
81#define AB9540_GPIO_PUD7_REG 0x36
82
83#define AB8500_GPIO_IN1_REG 0x40
84#define AB8500_GPIO_IN2_REG 0x41
85#define AB8500_GPIO_IN3_REG 0x42
86#define AB8500_GPIO_IN4_REG 0x43
87#define AB8500_GPIO_IN5_REG 0x44
88#define AB8500_GPIO_IN6_REG 0x45
89#define AB9540_GPIO_IN7_REG 0x46
90#define AB8540_GPIO_VINSEL_REG 0x47
91#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
92#define AB8500_GPIO_ALTFUN_REG 0x50
Patrice Chotard0493e642013-01-08 10:41:02 +010093#define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
94#define AB8540_GPIO_VINSEL_MASK 0x03
95#define AB8540_GPIOX_VBAT_START 51
96#define AB8540_GPIOX_VBAT_END 54
97
Patrice Chotard0493e642013-01-08 10:41:02 +010098struct abx500_pinctrl {
99 struct device *dev;
100 struct pinctrl_dev *pctldev;
101 struct abx500_pinctrl_soc_data *soc;
102 struct gpio_chip chip;
103 struct ab8500 *parent;
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
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200186static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
187 enum abx500_gpio_pull_updown *pull_updown)
188{
189 u8 pos;
190 u8 val;
191 int ret;
192 struct pullud *pullud;
193
194 if (!pct->soc->pullud) {
195 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
196 __func__);
197 ret = -EPERM;
198 goto out;
199 }
200
201 pullud = pct->soc->pullud;
202
203 if ((offset < pullud->first_pin)
204 || (offset > pullud->last_pin)) {
205 ret = -EINVAL;
206 goto out;
207 }
208
209 ret = abx500_get_register_interruptible(pct->dev,
210 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
211
212 pos = (offset - pullud->first_pin) << 1;
213 *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
214
215out:
216 if (ret < 0)
217 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
218
219 return ret;
220}
221
222static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
223 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100224{
225 u8 pos;
226 int ret;
227 struct pullud *pullud;
228
229 if (!pct->soc->pullud) {
230 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
231 __func__);
232 ret = -EPERM;
233 goto out;
234 }
235
236 pullud = pct->soc->pullud;
237
238 if ((offset < pullud->first_pin)
239 || (offset > pullud->last_pin)) {
240 ret = -EINVAL;
241 goto out;
242 }
Patrice Chotard10a8be52013-05-24 14:06:29 +0200243 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100244
245 ret = abx500_mask_and_set_register_interruptible(pct->dev,
246 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
247 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
248
249out:
250 if (ret < 0)
251 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000252
Patrice Chotard0493e642013-01-08 10:41:02 +0100253 return ret;
254}
255
256static int abx500_gpio_direction_output(struct gpio_chip *chip,
257 unsigned offset,
258 int val)
259{
260 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
261 struct pullud *pullud = pct->soc->pullud;
262 unsigned gpio;
263 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000264
Patrice Chotard0493e642013-01-08 10:41:02 +0100265 /* set direction as output */
266 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
267 if (ret < 0)
268 return ret;
269
270 /* disable pull down */
271 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
272 if (ret < 0)
273 return ret;
274
275 /* if supported, disable both pull down and pull up */
276 gpio = offset + 1;
277 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200278 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100279 gpio,
280 ABX500_GPIO_PULL_NONE);
281 if (ret < 0)
282 return ret;
283 }
Lee Jones83b423c2013-01-23 13:24:08 +0000284
Patrice Chotard0493e642013-01-08 10:41:02 +0100285 /* set the output as 1 or 0 */
286 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
287}
288
289static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
290{
291 /* set the register as input */
292 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
293}
294
295static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
296{
297 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000298 /* The AB8500 GPIO numbers are off by one */
299 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000300 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100301 int i;
302
303 for (i = 0; i < pct->irq_cluster_size; i++) {
304 struct abx500_gpio_irq_cluster *cluster =
305 &pct->irq_cluster[i];
306
Lee Jonesa6a16d22013-01-31 09:57:52 +0000307 if (gpio >= cluster->start && gpio <= cluster->end) {
308 /*
309 * The ABx500 GPIO's associated IRQs are clustered together
310 * throughout the interrupt numbers at irregular intervals.
311 * To solve this quandry, we have placed the read-in values
312 * into the cluster information table.
313 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100314 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000315 return irq_create_mapping(pct->parent->domain, hwirq);
316 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100317 }
318
319 return -EINVAL;
320}
321
322static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000323 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100324{
325 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
326 struct alternate_functions af = pct->soc->alternate_functions[gpio];
327 int ret;
328 int val;
329 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000330
Patrice Chotard0493e642013-01-08 10:41:02 +0100331 const char *modes[] = {
332 [ABX500_DEFAULT] = "default",
333 [ABX500_ALT_A] = "altA",
334 [ABX500_ALT_B] = "altB",
335 [ABX500_ALT_C] = "altC",
336 };
337
338 /* sanity check */
339 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
340 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
341 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
342 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
343 modes[alt_setting]);
344 return -EINVAL;
345 }
346
347 /* on ABx5xx, there is no GPIO0, so adjust the offset */
348 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000349
Patrice Chotard0493e642013-01-08 10:41:02 +0100350 switch (alt_setting) {
351 case ABX500_DEFAULT:
352 /*
353 * for ABx5xx family, default mode is always selected by
354 * writing 0 to GPIOSELx register, except for pins which
355 * support at least ALT_B mode, default mode is selected
356 * by writing 1 to GPIOSELx register
357 */
358 val = 0;
359 if (af.alt_bit1 != UNUSED)
360 val++;
361
362 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
363 offset, val);
364 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000365
Patrice Chotard0493e642013-01-08 10:41:02 +0100366 case ABX500_ALT_A:
367 /*
368 * for ABx5xx family, alt_a mode is always selected by
369 * writing 1 to GPIOSELx register, except for pins which
370 * support at least ALT_B mode, alt_a mode is selected
371 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
372 * register
373 */
374 if (af.alt_bit1 != UNUSED) {
375 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
376 offset, 0);
377 ret = abx500_gpio_set_bits(chip,
378 AB8500_GPIO_ALTFUN_REG,
379 af.alt_bit1,
380 !!(af.alta_val && BIT(0)));
381 if (af.alt_bit2 != UNUSED)
382 ret = abx500_gpio_set_bits(chip,
383 AB8500_GPIO_ALTFUN_REG,
384 af.alt_bit2,
385 !!(af.alta_val && BIT(1)));
386 } else
387 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
388 offset, 1);
389 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000390
Patrice Chotard0493e642013-01-08 10:41:02 +0100391 case ABX500_ALT_B:
392 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
393 offset, 0);
394 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
395 af.alt_bit1, !!(af.altb_val && BIT(0)));
396 if (af.alt_bit2 != UNUSED)
397 ret = abx500_gpio_set_bits(chip,
398 AB8500_GPIO_ALTFUN_REG,
399 af.alt_bit2,
400 !!(af.altb_val && BIT(1)));
401 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000402
Patrice Chotard0493e642013-01-08 10:41:02 +0100403 case ABX500_ALT_C:
404 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
405 offset, 0);
406 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
407 af.alt_bit2, !!(af.altc_val && BIT(0)));
408 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
409 af.alt_bit2, !!(af.altc_val && BIT(1)));
410 break;
411
412 default:
413 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000414
Patrice Chotard0493e642013-01-08 10:41:02 +0100415 return -EINVAL;
416 }
Lee Jones83b423c2013-01-23 13:24:08 +0000417
Patrice Chotard0493e642013-01-08 10:41:02 +0100418 return ret;
419}
420
421static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000422 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100423{
424 u8 mode;
425 bool bit_mode;
426 bool alt_bit1;
427 bool alt_bit2;
428 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
429 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100430 /* on ABx5xx, there is no GPIO0, so adjust the offset */
431 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100432
433 /*
434 * if gpiosel_bit is set to unused,
435 * it means no GPIO or special case
436 */
437 if (af.gpiosel_bit == UNUSED)
438 return ABX500_DEFAULT;
439
440 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100441 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100442 af.gpiosel_bit, &bit_mode);
443 mode = bit_mode;
444
445 /* sanity check */
446 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
447 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
448 dev_err(pct->dev,
449 "alt_bitX value not in correct range (-1 to 7)\n");
450 return -EINVAL;
451 }
Lee Jones83b423c2013-01-23 13:24:08 +0000452
Patrice Chotard0493e642013-01-08 10:41:02 +0100453 /* if alt_bit2 is used, alt_bit1 must be used too */
454 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
455 dev_err(pct->dev,
456 "if alt_bit2 is used, alt_bit1 can't be unused\n");
457 return -EINVAL;
458 }
459
460 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800461 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100462 return mode;
463 /*
464 * if pin GPIOSEL bit is set and pin supports alternate function,
465 * it means DEFAULT mode
466 */
467 if (mode)
468 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000469
Patrice Chotard0493e642013-01-08 10:41:02 +0100470 /*
471 * pin use the AlternatFunction register
472 * read alt_bit1 value
473 */
474 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
475 af.alt_bit1, &alt_bit1);
476
477 if (af.alt_bit2 != UNUSED)
478 /* read alt_bit2 value */
479 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
480 &alt_bit2);
481 else
482 alt_bit2 = 0;
483
484 mode = (alt_bit2 << 1) + alt_bit1;
485 if (mode == af.alta_val)
486 return ABX500_ALT_A;
487 else if (mode == af.altb_val)
488 return ABX500_ALT_B;
489 else
490 return ABX500_ALT_C;
491}
492
493#ifdef CONFIG_DEBUG_FS
494
495#include <linux/seq_file.h>
496
497static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000498 struct pinctrl_dev *pctldev,
499 struct gpio_chip *chip,
500 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100501{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200502 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
503 struct pullud *pullud = pct->soc->pullud;
Patrice Chotard0493e642013-01-08 10:41:02 +0100504 const char *label = gpiochip_is_requested(chip, offset - 1);
505 u8 gpio_offset = offset - 1;
506 int mode = -1;
507 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200508 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200509 enum abx500_gpio_pull_updown pud = 0;
Lee Jones83b423c2013-01-23 13:24:08 +0000510
Patrice Chotard0493e642013-01-08 10:41:02 +0100511 const char *modes[] = {
512 [ABX500_DEFAULT] = "default",
513 [ABX500_ALT_A] = "altA",
514 [ABX500_ALT_B] = "altB",
515 [ABX500_ALT_C] = "altC",
516 };
517
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200518 const char *pull_up_down[] = {
519 [ABX500_GPIO_PULL_DOWN] = "pull down",
520 [ABX500_GPIO_PULL_NONE] = "pull none",
521 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
522 [ABX500_GPIO_PULL_UP] = "pull up",
523 };
524
Patrice Chotard0493e642013-01-08 10:41:02 +0100525 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200526
527 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
528 gpio, label ?: "(none)",
529 is_out ? "out" : "in ");
530
531 if (!is_out) {
532 if (pullud &&
533 (offset >= pullud->first_pin) &&
534 (offset <= pullud->last_pin)) {
535 abx500_get_pull_updown(pct, offset, &pud);
536 seq_printf(s, " %-9s", pull_up_down[pud]);
537 } else {
538 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
539 gpio_offset, &pd);
540 seq_printf(s, " %-9s", pull_up_down[pd]);
541 }
542 } else
543 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100544
545 if (pctldev)
546 mode = abx500_get_mode(pctldev, chip, offset);
547
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200548 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100549}
550
551static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
552{
553 unsigned i;
554 unsigned gpio = chip->base;
555 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
556 struct pinctrl_dev *pctldev = pct->pctldev;
557
558 for (i = 0; i < chip->ngpio; i++, gpio++) {
559 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
560 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
561 seq_printf(s, "\n");
562 }
563}
564
565#else
566static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000567 struct pinctrl_dev *pctldev,
568 struct gpio_chip *chip,
569 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100570{
571}
572#define abx500_gpio_dbg_show NULL
573#endif
574
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530575static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100576{
577 int gpio = chip->base + offset;
578
579 return pinctrl_request_gpio(gpio);
580}
581
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530582static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100583{
584 int gpio = chip->base + offset;
585
586 pinctrl_free_gpio(gpio);
587}
588
589static struct gpio_chip abx500gpio_chip = {
590 .label = "abx500-gpio",
591 .owner = THIS_MODULE,
592 .request = abx500_gpio_request,
593 .free = abx500_gpio_free,
594 .direction_input = abx500_gpio_direction_input,
595 .get = abx500_gpio_get,
596 .direction_output = abx500_gpio_direction_output,
597 .set = abx500_gpio_set,
598 .to_irq = abx500_gpio_to_irq,
599 .dbg_show = abx500_gpio_dbg_show,
600};
601
Patrice Chotard0493e642013-01-08 10:41:02 +0100602static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
603{
604 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
605
606 return pct->soc->nfunctions;
607}
608
609static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
610 unsigned function)
611{
612 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
613
614 return pct->soc->functions[function].name;
615}
616
617static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000618 unsigned function,
619 const char * const **groups,
620 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100621{
622 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
623
624 *groups = pct->soc->functions[function].groups;
625 *num_groups = pct->soc->functions[function].ngroups;
626
627 return 0;
628}
629
Patrice Chotard0493e642013-01-08 10:41:02 +0100630static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000631 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100632{
633 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
634 struct gpio_chip *chip = &pct->chip;
635 const struct abx500_pingroup *g;
636 int i;
637 int ret = 0;
638
639 g = &pct->soc->groups[group];
640 if (g->altsetting < 0)
641 return -EINVAL;
642
643 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
644
645 for (i = 0; i < g->npins; i++) {
646 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
647 g->pins[i], g->altsetting);
648
Patrice Chotard0493e642013-01-08 10:41:02 +0100649 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
650 }
Lee Jones83b423c2013-01-23 13:24:08 +0000651
Patrice Chotard0493e642013-01-08 10:41:02 +0100652 return ret;
653}
654
655static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000656 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100657{
658 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
659 const struct abx500_pingroup *g;
660
661 g = &pct->soc->groups[group];
662 if (g->altsetting < 0)
663 return;
664
665 /* FIXME: poke out the mux, set the pin to some default state? */
666 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
667}
668
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530669static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000670 struct pinctrl_gpio_range *range,
671 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100672{
673 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
674 const struct abx500_pinrange *p;
675 int ret;
676 int i;
677
678 /*
679 * Different ranges have different ways to enable GPIO function on a
680 * pin, so refer back to our local range type, where we handily define
681 * what altfunc enables GPIO for a certain pin.
682 */
683 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
684 p = &pct->soc->gpio_ranges[i];
685 if ((offset >= p->offset) &&
686 (offset < (p->offset + p->npins)))
687 break;
688 }
689
690 if (i == pct->soc->gpio_num_ranges) {
691 dev_err(pct->dev, "%s failed to locate range\n", __func__);
692 return -ENODEV;
693 }
694
695 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
696 p->altfunc, offset);
697
698 ret = abx500_set_mode(pct->pctldev, &pct->chip,
699 offset, p->altfunc);
700 if (ret < 0) {
701 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
702 return ret;
703 }
704
705 return ret;
706}
707
708static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000709 struct pinctrl_gpio_range *range,
710 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100711{
712}
713
Laurent Pinchart022ab142013-02-16 10:25:07 +0100714static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100715 .get_functions_count = abx500_pmx_get_funcs_cnt,
716 .get_function_name = abx500_pmx_get_func_name,
717 .get_function_groups = abx500_pmx_get_func_groups,
718 .enable = abx500_pmx_enable,
719 .disable = abx500_pmx_disable,
720 .gpio_request_enable = abx500_gpio_request_enable,
721 .gpio_disable_free = abx500_gpio_disable_free,
722};
723
724static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
725{
726 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
727
728 return pct->soc->ngroups;
729}
730
731static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000732 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100733{
734 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
735
736 return pct->soc->groups[selector].name;
737}
738
739static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000740 unsigned selector,
741 const unsigned **pins,
742 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100743{
744 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
745
746 *pins = pct->soc->groups[selector].pins;
747 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000748
Patrice Chotard0493e642013-01-08 10:41:02 +0100749 return 0;
750}
751
752static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000753 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100754{
755 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
756 struct gpio_chip *chip = &pct->chip;
757
758 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
759 chip->base + offset - 1);
760}
761
Patrice Chotard64a45c92013-06-20 16:04:59 +0200762static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
763 struct pinctrl_map *map, unsigned num_maps)
764{
765 int i;
766
767 for (i = 0; i < num_maps; i++)
768 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
769 kfree(map[i].data.configs.configs);
770 kfree(map);
771}
772
773static int abx500_dt_reserve_map(struct pinctrl_map **map,
774 unsigned *reserved_maps,
775 unsigned *num_maps,
776 unsigned reserve)
777{
778 unsigned old_num = *reserved_maps;
779 unsigned new_num = *num_maps + reserve;
780 struct pinctrl_map *new_map;
781
782 if (old_num >= new_num)
783 return 0;
784
785 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
786 if (!new_map)
787 return -ENOMEM;
788
789 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
790
791 *map = new_map;
792 *reserved_maps = new_num;
793
794 return 0;
795}
796
797static int abx500_dt_add_map_mux(struct pinctrl_map **map,
798 unsigned *reserved_maps,
799 unsigned *num_maps, const char *group,
800 const char *function)
801{
802 if (*num_maps == *reserved_maps)
803 return -ENOSPC;
804
805 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
806 (*map)[*num_maps].data.mux.group = group;
807 (*map)[*num_maps].data.mux.function = function;
808 (*num_maps)++;
809
810 return 0;
811}
812
813static int abx500_dt_add_map_configs(struct pinctrl_map **map,
814 unsigned *reserved_maps,
815 unsigned *num_maps, const char *group,
816 unsigned long *configs, unsigned num_configs)
817{
818 unsigned long *dup_configs;
819
820 if (*num_maps == *reserved_maps)
821 return -ENOSPC;
822
823 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
824 GFP_KERNEL);
825 if (!dup_configs)
826 return -ENOMEM;
827
828 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
829
830 (*map)[*num_maps].data.configs.group_or_pin = group;
831 (*map)[*num_maps].data.configs.configs = dup_configs;
832 (*map)[*num_maps].data.configs.num_configs = num_configs;
833 (*num_maps)++;
834
835 return 0;
836}
837
838static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
839 const char *pin_name)
840{
841 int i, pin_number;
842 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
843
844 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
845 for (i = 0; i < npct->soc->npins; i++)
846 if (npct->soc->pins[i].number == pin_number)
847 return npct->soc->pins[i].name;
848 return NULL;
849}
850
851static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
852 struct device_node *np,
853 struct pinctrl_map **map,
854 unsigned *reserved_maps,
855 unsigned *num_maps)
856{
857 int ret;
858 const char *function = NULL;
859 unsigned long *configs;
860 unsigned int nconfigs = 0;
861 bool has_config = 0;
862 unsigned reserve = 0;
863 struct property *prop;
864 const char *group, *gpio_name;
865 struct device_node *np_config;
866
867 ret = of_property_read_string(np, "ste,function", &function);
868 if (ret >= 0)
869 reserve = 1;
870
871 ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
872 if (nconfigs)
873 has_config = 1;
874
875 np_config = of_parse_phandle(np, "ste,config", 0);
876 if (np_config) {
877 ret = pinconf_generic_parse_dt_config(np_config, &configs,
878 &nconfigs);
879 if (ret)
880 goto exit;
881 has_config |= nconfigs;
882 }
883
884 ret = of_property_count_strings(np, "ste,pins");
885 if (ret < 0)
886 goto exit;
887
888 if (has_config)
889 reserve++;
890
891 reserve *= ret;
892
893 ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
894 if (ret < 0)
895 goto exit;
896
897 of_property_for_each_string(np, "ste,pins", prop, group) {
898 if (function) {
899 ret = abx500_dt_add_map_mux(map, reserved_maps,
900 num_maps, group, function);
901 if (ret < 0)
902 goto exit;
903 }
904 if (has_config) {
905 gpio_name = abx500_find_pin_name(pctldev, group);
906
907 ret = abx500_dt_add_map_configs(map, reserved_maps,
908 num_maps, gpio_name, configs, 1);
909 if (ret < 0)
910 goto exit;
911 }
912
913 }
914exit:
915 return ret;
916}
917
918static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
919 struct device_node *np_config,
920 struct pinctrl_map **map, unsigned *num_maps)
921{
922 unsigned reserved_maps;
923 struct device_node *np;
924 int ret;
925
926 reserved_maps = 0;
927 *map = NULL;
928 *num_maps = 0;
929
930 for_each_child_of_node(np_config, np) {
931 ret = abx500_dt_subnode_to_map(pctldev, np, map,
932 &reserved_maps, num_maps);
933 if (ret < 0) {
934 abx500_dt_free_map(pctldev, *map, *num_maps);
935 return ret;
936 }
937 }
938
939 return 0;
940}
941
Laurent Pinchart022ab142013-02-16 10:25:07 +0100942static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100943 .get_groups_count = abx500_get_groups_cnt,
944 .get_group_name = abx500_get_group_name,
945 .get_group_pins = abx500_get_group_pins,
946 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200947 .dt_node_to_map = abx500_dt_node_to_map,
948 .dt_free_map = abx500_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100949};
950
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530951static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000952 unsigned pin,
953 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100954{
Lee Jones1abeebe2012-12-20 11:11:19 +0000955 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100956}
957
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530958static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000959 unsigned pin,
960 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100961{
962 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
963 struct pullud *pullud = pct->soc->pullud;
964 struct gpio_chip *chip = &pct->chip;
965 unsigned offset;
Patrice Chotard9ed3cd32013-05-24 14:06:30 +0200966 int ret = 0;
Patrice Chotard0493e642013-01-08 10:41:02 +0100967 enum pin_config_param param = pinconf_to_config_param(config);
968 enum pin_config_param argument = pinconf_to_config_argument(config);
969
970 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
971 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
972 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
973 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000974
Patrice Chotard0493e642013-01-08 10:41:02 +0100975 /* on ABx500, there is no GPIO0, so adjust the offset */
976 offset = pin - 1;
977
978 switch (param) {
979 case PIN_CONFIG_BIAS_PULL_DOWN:
980 /*
981 * if argument = 1 set the pull down
982 * else clear the pull down
983 */
984 ret = abx500_gpio_direction_input(chip, offset);
985 /*
986 * Some chips only support pull down, while some actually
987 * support both pull up and pull down. Such chips have
988 * a "pullud" range specified for the pins that support
989 * both features. If the pin is not within that range, we
990 * fall back to the old bit set that only support pull down.
991 */
992 if (pullud &&
993 pin >= pullud->first_pin &&
994 pin <= pullud->last_pin)
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200995 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100996 pin,
997 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
998 else
999 /* Chip only supports pull down */
1000 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1001 offset, argument ? 0 : 1);
1002 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001003
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001004 case PIN_CONFIG_BIAS_PULL_UP:
1005 /*
1006 * if argument = 1 set the pull up
1007 * else clear the pull up
1008 */
1009 ret = abx500_gpio_direction_input(chip, offset);
1010 /*
1011 * Some chips only support pull down, while some actually
1012 * support both pull up and pull down. Such chips have
1013 * a "pullud" range specified for the pins that support
1014 * both features. If the pin is not within that range, do
1015 * nothing
1016 */
1017 if (pullud &&
1018 pin >= pullud->first_pin &&
1019 pin <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001020 ret = abx500_set_pull_updown(pct,
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001021 pin,
1022 argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
1023 }
1024 break;
1025
Patrice Chotard0493e642013-01-08 10:41:02 +01001026 case PIN_CONFIG_OUTPUT:
1027 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +00001028
Patrice Chotard0493e642013-01-08 10:41:02 +01001029 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001030
Patrice Chotard0493e642013-01-08 10:41:02 +01001031 default:
1032 dev_err(chip->dev, "illegal configuration requested\n");
Lee Jones83b423c2013-01-23 13:24:08 +00001033
Patrice Chotard0493e642013-01-08 10:41:02 +01001034 return -EINVAL;
1035 }
Lee Jones83b423c2013-01-23 13:24:08 +00001036
Patrice Chotard0493e642013-01-08 10:41:02 +01001037 return ret;
1038}
1039
Laurent Pinchart022ab142013-02-16 10:25:07 +01001040static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001041 .pin_config_get = abx500_pin_config_get,
1042 .pin_config_set = abx500_pin_config_set,
1043};
1044
1045static struct pinctrl_desc abx500_pinctrl_desc = {
1046 .name = "pinctrl-abx500",
1047 .pctlops = &abx500_pinctrl_ops,
1048 .pmxops = &abx500_pinmux_ops,
1049 .confops = &abx500_pinconf_ops,
1050 .owner = THIS_MODULE,
1051};
1052
1053static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1054{
1055 unsigned int lowest = 0;
1056 unsigned int highest = 0;
1057 unsigned int npins = 0;
1058 int i;
1059
1060 /*
1061 * Compute number of GPIOs from the last SoC gpio range descriptors
1062 * These ranges may include "holes" but the GPIO number space shall
1063 * still be homogeneous, so we need to detect and account for any
1064 * such holes so that these are included in the number of GPIO pins.
1065 */
1066 for (i = 0; i < soc->gpio_num_ranges; i++) {
1067 unsigned gstart;
1068 unsigned gend;
1069 const struct abx500_pinrange *p;
1070
1071 p = &soc->gpio_ranges[i];
1072 gstart = p->offset;
1073 gend = p->offset + p->npins - 1;
1074
1075 if (i == 0) {
1076 /* First iteration, set start values */
1077 lowest = gstart;
1078 highest = gend;
1079 } else {
1080 if (gstart < lowest)
1081 lowest = gstart;
1082 if (gend > highest)
1083 highest = gend;
1084 }
1085 }
1086 /* this gives the absolute number of pins */
1087 npins = highest - lowest + 1;
1088 return npins;
1089}
1090
Lee Jonesf30a3832013-01-31 11:07:40 +00001091static const struct of_device_id abx500_gpio_match[] = {
1092 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1093 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1094 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1095 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001096 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001097};
1098
Patrice Chotard0493e642013-01-08 10:41:02 +01001099static int abx500_gpio_probe(struct platform_device *pdev)
1100{
1101 struct ab8500_platform_data *abx500_pdata =
1102 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +00001103 struct abx500_gpio_platform_data *pdata = NULL;
1104 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +01001105 struct abx500_pinctrl *pct;
1106 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +00001107 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +00001108 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +01001109 int i;
1110
Lee Jonesf30a3832013-01-31 11:07:40 +00001111 if (abx500_pdata)
1112 pdata = abx500_pdata->gpio;
Lee Jonesf30a3832013-01-31 11:07:40 +00001113
Lee Jones86c976e2013-05-08 14:29:08 +01001114 if (!(pdata || np)) {
1115 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
1116 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001117 }
1118
1119 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1120 GFP_KERNEL);
1121 if (pct == NULL) {
1122 dev_err(&pdev->dev,
1123 "failed to allocate memory for pct\n");
1124 return -ENOMEM;
1125 }
1126
1127 pct->dev = &pdev->dev;
1128 pct->parent = dev_get_drvdata(pdev->dev.parent);
1129 pct->chip = abx500gpio_chip;
1130 pct->chip.dev = &pdev->dev;
Lee Jonesf30a3832013-01-31 11:07:40 +00001131 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +01001132
Lee Jones86c976e2013-05-08 14:29:08 +01001133 if (platid)
1134 id = platid->driver_data;
1135 else if (np) {
1136 const struct of_device_id *match;
1137
1138 match = of_match_device(abx500_gpio_match, &pdev->dev);
1139 if (match)
1140 id = (unsigned long)match->data;
1141 }
1142
Patrice Chotard0493e642013-01-08 10:41:02 +01001143 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001144 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001145 case PINCTRL_AB8500:
1146 abx500_pinctrl_ab8500_init(&pct->soc);
1147 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001148 case PINCTRL_AB8540:
1149 abx500_pinctrl_ab8540_init(&pct->soc);
1150 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001151 case PINCTRL_AB9540:
1152 abx500_pinctrl_ab9540_init(&pct->soc);
1153 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001154 case PINCTRL_AB8505:
1155 abx500_pinctrl_ab8505_init(&pct->soc);
1156 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001157 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001158 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001159 return -EINVAL;
1160 }
1161
1162 if (!pct->soc) {
1163 dev_err(&pdev->dev, "Invalid SOC data\n");
1164 return -EINVAL;
1165 }
1166
1167 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1168 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1169 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001170
Patrice Chotard0493e642013-01-08 10:41:02 +01001171 ret = gpiochip_add(&pct->chip);
1172 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001173 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001174 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001175 }
1176 dev_info(&pdev->dev, "added gpiochip\n");
1177
1178 abx500_pinctrl_desc.pins = pct->soc->pins;
1179 abx500_pinctrl_desc.npins = pct->soc->npins;
1180 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
1181 if (!pct->pctldev) {
1182 dev_err(&pdev->dev,
1183 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +00001184 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +01001185 goto out_rem_chip;
1186 }
1187 dev_info(&pdev->dev, "registered pin controller\n");
1188
1189 /* We will handle a range of GPIO pins */
1190 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1191 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1192
1193 ret = gpiochip_add_pin_range(&pct->chip,
1194 dev_name(&pdev->dev),
1195 p->offset - 1, p->offset, p->npins);
1196 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001197 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001198 }
1199
1200 platform_set_drvdata(pdev, pct);
1201 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1202
1203 return 0;
1204
1205out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +00001206 err = gpiochip_remove(&pct->chip);
1207 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +01001208 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +00001209
Patrice Chotard0493e642013-01-08 10:41:02 +01001210 return ret;
1211}
1212
Lee Jones83b423c2013-01-23 13:24:08 +00001213/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001214 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001215 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001216 */
1217static int abx500_gpio_remove(struct platform_device *pdev)
1218{
1219 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1220 int ret;
1221
1222 ret = gpiochip_remove(&pct->chip);
1223 if (ret < 0) {
1224 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
1225 ret);
1226 return ret;
1227 }
1228
Patrice Chotard0493e642013-01-08 10:41:02 +01001229 return 0;
1230}
1231
1232static const struct platform_device_id abx500_pinctrl_id[] = {
1233 { "pinctrl-ab8500", PINCTRL_AB8500 },
1234 { "pinctrl-ab8540", PINCTRL_AB8540 },
1235 { "pinctrl-ab9540", PINCTRL_AB9540 },
1236 { "pinctrl-ab8505", PINCTRL_AB8505 },
1237 { },
1238};
1239
1240static struct platform_driver abx500_gpio_driver = {
1241 .driver = {
1242 .name = "abx500-gpio",
1243 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001244 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001245 },
1246 .probe = abx500_gpio_probe,
1247 .remove = abx500_gpio_remove,
1248 .id_table = abx500_pinctrl_id,
1249};
1250
1251static int __init abx500_gpio_init(void)
1252{
1253 return platform_driver_register(&abx500_gpio_driver);
1254}
1255core_initcall(abx500_gpio_init);
1256
1257MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1258MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1259MODULE_ALIAS("platform:abx500-gpio");
1260MODULE_LICENSE("GPL v2");