blob: caa7ab6811d1895c444fe5683b2a2ca8acc10558 [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 Chotard61ce1352013-06-20 16:05:00 +020036#include "core.h"
Patrice Chotard64a45c92013-06-20 16:04:59 +020037#include "pinconf.h"
Patrice Chotard0493e642013-01-08 10:41:02 +010038
39/*
40 * The AB9540 and AB8540 GPIO support are extended versions
41 * of the AB8500 GPIO support.
42 * The AB9540 supports an additional (7th) register so that
43 * more GPIO may be configured and used.
44 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
45 * internal pull-up and pull-down capabilities.
46 */
47
48/*
49 * GPIO registers offset
50 * Bank: 0x10
51 */
52#define AB8500_GPIO_SEL1_REG 0x00
53#define AB8500_GPIO_SEL2_REG 0x01
54#define AB8500_GPIO_SEL3_REG 0x02
55#define AB8500_GPIO_SEL4_REG 0x03
56#define AB8500_GPIO_SEL5_REG 0x04
57#define AB8500_GPIO_SEL6_REG 0x05
58#define AB9540_GPIO_SEL7_REG 0x06
59
60#define AB8500_GPIO_DIR1_REG 0x10
61#define AB8500_GPIO_DIR2_REG 0x11
62#define AB8500_GPIO_DIR3_REG 0x12
63#define AB8500_GPIO_DIR4_REG 0x13
64#define AB8500_GPIO_DIR5_REG 0x14
65#define AB8500_GPIO_DIR6_REG 0x15
66#define AB9540_GPIO_DIR7_REG 0x16
67
68#define AB8500_GPIO_OUT1_REG 0x20
69#define AB8500_GPIO_OUT2_REG 0x21
70#define AB8500_GPIO_OUT3_REG 0x22
71#define AB8500_GPIO_OUT4_REG 0x23
72#define AB8500_GPIO_OUT5_REG 0x24
73#define AB8500_GPIO_OUT6_REG 0x25
74#define AB9540_GPIO_OUT7_REG 0x26
75
76#define AB8500_GPIO_PUD1_REG 0x30
77#define AB8500_GPIO_PUD2_REG 0x31
78#define AB8500_GPIO_PUD3_REG 0x32
79#define AB8500_GPIO_PUD4_REG 0x33
80#define AB8500_GPIO_PUD5_REG 0x34
81#define AB8500_GPIO_PUD6_REG 0x35
82#define AB9540_GPIO_PUD7_REG 0x36
83
84#define AB8500_GPIO_IN1_REG 0x40
85#define AB8500_GPIO_IN2_REG 0x41
86#define AB8500_GPIO_IN3_REG 0x42
87#define AB8500_GPIO_IN4_REG 0x43
88#define AB8500_GPIO_IN5_REG 0x44
89#define AB8500_GPIO_IN6_REG 0x45
90#define AB9540_GPIO_IN7_REG 0x46
91#define AB8540_GPIO_VINSEL_REG 0x47
92#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
93#define AB8500_GPIO_ALTFUN_REG 0x50
Patrice Chotard0493e642013-01-08 10:41:02 +010094#define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
95#define AB8540_GPIO_VINSEL_MASK 0x03
96#define AB8540_GPIOX_VBAT_START 51
97#define AB8540_GPIOX_VBAT_END 54
98
Patrice Chotard0493e642013-01-08 10:41:02 +010099struct abx500_pinctrl {
100 struct device *dev;
101 struct pinctrl_dev *pctldev;
102 struct abx500_pinctrl_soc_data *soc;
103 struct gpio_chip chip;
104 struct ab8500 *parent;
Patrice Chotard0493e642013-01-08 10:41:02 +0100105 struct abx500_gpio_irq_cluster *irq_cluster;
106 int irq_cluster_size;
Patrice Chotard0493e642013-01-08 10:41:02 +0100107};
108
109/**
110 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
111 * @chip: Member of the structure abx500_pinctrl
112 */
113static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
114{
115 return container_of(chip, struct abx500_pinctrl, chip);
116}
117
118static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000119 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100120{
121 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
122 u8 pos = offset % 8;
123 u8 val;
124 int ret;
125
126 reg += offset / 8;
127 ret = abx500_get_register_interruptible(pct->dev,
128 AB8500_MISC, reg, &val);
129
130 *bit = !!(val & BIT(pos));
131
132 if (ret < 0)
133 dev_err(pct->dev,
134 "%s read reg =%x, offset=%x failed\n",
135 __func__, reg, offset);
136
137 return ret;
138}
139
140static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000141 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100142{
143 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
144 u8 pos = offset % 8;
145 int ret;
146
147 reg += offset / 8;
148 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000149 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100150 if (ret < 0)
151 dev_err(pct->dev, "%s write failed\n", __func__);
Lee Jones83b423c2013-01-23 13:24:08 +0000152
Patrice Chotard0493e642013-01-08 10:41:02 +0100153 return ret;
154}
Lee Jones83b423c2013-01-23 13:24:08 +0000155
Patrice Chotard0493e642013-01-08 10:41:02 +0100156/**
157 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000158 * @chip: Gpio device
159 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100160 */
161static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
162{
163 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
164 bool bit;
165 int ret;
166
167 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
168 offset, &bit);
169 if (ret < 0) {
170 dev_err(pct->dev, "%s failed\n", __func__);
171 return ret;
172 }
Lee Jones83b423c2013-01-23 13:24:08 +0000173
Patrice Chotard0493e642013-01-08 10:41:02 +0100174 return bit;
175}
176
177static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
178{
179 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
180 int ret;
181
182 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
183 if (ret < 0)
184 dev_err(pct->dev, "%s write failed\n", __func__);
185}
186
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200187static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
188 enum abx500_gpio_pull_updown *pull_updown)
189{
190 u8 pos;
191 u8 val;
192 int ret;
193 struct pullud *pullud;
194
195 if (!pct->soc->pullud) {
196 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
197 __func__);
198 ret = -EPERM;
199 goto out;
200 }
201
202 pullud = pct->soc->pullud;
203
204 if ((offset < pullud->first_pin)
205 || (offset > pullud->last_pin)) {
206 ret = -EINVAL;
207 goto out;
208 }
209
210 ret = abx500_get_register_interruptible(pct->dev,
211 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
212
213 pos = (offset - pullud->first_pin) << 1;
214 *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
215
216out:
217 if (ret < 0)
218 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
219
220 return ret;
221}
222
223static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
224 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100225{
226 u8 pos;
227 int ret;
228 struct pullud *pullud;
229
230 if (!pct->soc->pullud) {
231 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
232 __func__);
233 ret = -EPERM;
234 goto out;
235 }
236
237 pullud = pct->soc->pullud;
238
239 if ((offset < pullud->first_pin)
240 || (offset > pullud->last_pin)) {
241 ret = -EINVAL;
242 goto out;
243 }
Patrice Chotard10a8be52013-05-24 14:06:29 +0200244 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100245
246 ret = abx500_mask_and_set_register_interruptible(pct->dev,
247 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
248 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
249
250out:
251 if (ret < 0)
252 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000253
Patrice Chotard0493e642013-01-08 10:41:02 +0100254 return ret;
255}
256
257static int abx500_gpio_direction_output(struct gpio_chip *chip,
258 unsigned offset,
259 int val)
260{
261 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
262 struct pullud *pullud = pct->soc->pullud;
263 unsigned gpio;
264 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000265
Patrice Chotard0493e642013-01-08 10:41:02 +0100266 /* set direction as output */
267 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
268 if (ret < 0)
269 return ret;
270
271 /* disable pull down */
272 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
273 if (ret < 0)
274 return ret;
275
276 /* if supported, disable both pull down and pull up */
277 gpio = offset + 1;
278 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200279 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100280 gpio,
281 ABX500_GPIO_PULL_NONE);
282 if (ret < 0)
283 return ret;
284 }
Lee Jones83b423c2013-01-23 13:24:08 +0000285
Patrice Chotard0493e642013-01-08 10:41:02 +0100286 /* set the output as 1 or 0 */
287 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
288}
289
290static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
291{
292 /* set the register as input */
293 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
294}
295
296static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
297{
298 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000299 /* The AB8500 GPIO numbers are off by one */
300 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000301 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100302 int i;
303
304 for (i = 0; i < pct->irq_cluster_size; i++) {
305 struct abx500_gpio_irq_cluster *cluster =
306 &pct->irq_cluster[i];
307
Lee Jonesa6a16d22013-01-31 09:57:52 +0000308 if (gpio >= cluster->start && gpio <= cluster->end) {
309 /*
310 * The ABx500 GPIO's associated IRQs are clustered together
311 * throughout the interrupt numbers at irregular intervals.
312 * To solve this quandry, we have placed the read-in values
313 * into the cluster information table.
314 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100315 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000316 return irq_create_mapping(pct->parent->domain, hwirq);
317 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100318 }
319
320 return -EINVAL;
321}
322
323static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000324 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100325{
326 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
327 struct alternate_functions af = pct->soc->alternate_functions[gpio];
328 int ret;
329 int val;
330 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000331
Patrice Chotard0493e642013-01-08 10:41:02 +0100332 const char *modes[] = {
333 [ABX500_DEFAULT] = "default",
334 [ABX500_ALT_A] = "altA",
335 [ABX500_ALT_B] = "altB",
336 [ABX500_ALT_C] = "altC",
337 };
338
339 /* sanity check */
340 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
341 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
342 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
343 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
344 modes[alt_setting]);
345 return -EINVAL;
346 }
347
348 /* on ABx5xx, there is no GPIO0, so adjust the offset */
349 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000350
Patrice Chotard0493e642013-01-08 10:41:02 +0100351 switch (alt_setting) {
352 case ABX500_DEFAULT:
353 /*
354 * for ABx5xx family, default mode is always selected by
355 * writing 0 to GPIOSELx register, except for pins which
356 * support at least ALT_B mode, default mode is selected
357 * by writing 1 to GPIOSELx register
358 */
359 val = 0;
360 if (af.alt_bit1 != UNUSED)
361 val++;
362
363 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
364 offset, val);
365 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000366
Patrice Chotard0493e642013-01-08 10:41:02 +0100367 case ABX500_ALT_A:
368 /*
369 * for ABx5xx family, alt_a mode is always selected by
370 * writing 1 to GPIOSELx register, except for pins which
371 * support at least ALT_B mode, alt_a mode is selected
372 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
373 * register
374 */
375 if (af.alt_bit1 != UNUSED) {
376 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
377 offset, 0);
378 ret = abx500_gpio_set_bits(chip,
379 AB8500_GPIO_ALTFUN_REG,
380 af.alt_bit1,
381 !!(af.alta_val && BIT(0)));
382 if (af.alt_bit2 != UNUSED)
383 ret = abx500_gpio_set_bits(chip,
384 AB8500_GPIO_ALTFUN_REG,
385 af.alt_bit2,
386 !!(af.alta_val && BIT(1)));
387 } else
388 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
389 offset, 1);
390 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000391
Patrice Chotard0493e642013-01-08 10:41:02 +0100392 case ABX500_ALT_B:
393 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
394 offset, 0);
395 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
396 af.alt_bit1, !!(af.altb_val && BIT(0)));
397 if (af.alt_bit2 != UNUSED)
398 ret = abx500_gpio_set_bits(chip,
399 AB8500_GPIO_ALTFUN_REG,
400 af.alt_bit2,
401 !!(af.altb_val && BIT(1)));
402 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000403
Patrice Chotard0493e642013-01-08 10:41:02 +0100404 case ABX500_ALT_C:
405 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
406 offset, 0);
407 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
408 af.alt_bit2, !!(af.altc_val && BIT(0)));
409 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
410 af.alt_bit2, !!(af.altc_val && BIT(1)));
411 break;
412
413 default:
414 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000415
Patrice Chotard0493e642013-01-08 10:41:02 +0100416 return -EINVAL;
417 }
Lee Jones83b423c2013-01-23 13:24:08 +0000418
Patrice Chotard0493e642013-01-08 10:41:02 +0100419 return ret;
420}
421
422static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000423 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100424{
425 u8 mode;
426 bool bit_mode;
427 bool alt_bit1;
428 bool alt_bit2;
429 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
430 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100431 /* on ABx5xx, there is no GPIO0, so adjust the offset */
432 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100433
434 /*
435 * if gpiosel_bit is set to unused,
436 * it means no GPIO or special case
437 */
438 if (af.gpiosel_bit == UNUSED)
439 return ABX500_DEFAULT;
440
441 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100442 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100443 af.gpiosel_bit, &bit_mode);
444 mode = bit_mode;
445
446 /* sanity check */
447 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
448 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
449 dev_err(pct->dev,
450 "alt_bitX value not in correct range (-1 to 7)\n");
451 return -EINVAL;
452 }
Lee Jones83b423c2013-01-23 13:24:08 +0000453
Patrice Chotard0493e642013-01-08 10:41:02 +0100454 /* if alt_bit2 is used, alt_bit1 must be used too */
455 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
456 dev_err(pct->dev,
457 "if alt_bit2 is used, alt_bit1 can't be unused\n");
458 return -EINVAL;
459 }
460
461 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800462 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100463 return mode;
464 /*
465 * if pin GPIOSEL bit is set and pin supports alternate function,
466 * it means DEFAULT mode
467 */
468 if (mode)
469 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000470
Patrice Chotard0493e642013-01-08 10:41:02 +0100471 /*
472 * pin use the AlternatFunction register
473 * read alt_bit1 value
474 */
475 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
476 af.alt_bit1, &alt_bit1);
477
478 if (af.alt_bit2 != UNUSED)
479 /* read alt_bit2 value */
480 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
481 &alt_bit2);
482 else
483 alt_bit2 = 0;
484
485 mode = (alt_bit2 << 1) + alt_bit1;
486 if (mode == af.alta_val)
487 return ABX500_ALT_A;
488 else if (mode == af.altb_val)
489 return ABX500_ALT_B;
490 else
491 return ABX500_ALT_C;
492}
493
494#ifdef CONFIG_DEBUG_FS
495
496#include <linux/seq_file.h>
497
498static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000499 struct pinctrl_dev *pctldev,
500 struct gpio_chip *chip,
501 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100502{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200503 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
504 struct pullud *pullud = pct->soc->pullud;
Patrice Chotard0493e642013-01-08 10:41:02 +0100505 const char *label = gpiochip_is_requested(chip, offset - 1);
506 u8 gpio_offset = offset - 1;
507 int mode = -1;
508 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200509 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200510 enum abx500_gpio_pull_updown pud = 0;
Lee Jones83b423c2013-01-23 13:24:08 +0000511
Patrice Chotard0493e642013-01-08 10:41:02 +0100512 const char *modes[] = {
513 [ABX500_DEFAULT] = "default",
514 [ABX500_ALT_A] = "altA",
515 [ABX500_ALT_B] = "altB",
516 [ABX500_ALT_C] = "altC",
517 };
518
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200519 const char *pull_up_down[] = {
520 [ABX500_GPIO_PULL_DOWN] = "pull down",
521 [ABX500_GPIO_PULL_NONE] = "pull none",
522 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
523 [ABX500_GPIO_PULL_UP] = "pull up",
524 };
525
Patrice Chotard0493e642013-01-08 10:41:02 +0100526 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200527
528 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
529 gpio, label ?: "(none)",
530 is_out ? "out" : "in ");
531
532 if (!is_out) {
533 if (pullud &&
534 (offset >= pullud->first_pin) &&
535 (offset <= pullud->last_pin)) {
536 abx500_get_pull_updown(pct, offset, &pud);
537 seq_printf(s, " %-9s", pull_up_down[pud]);
538 } else {
539 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
540 gpio_offset, &pd);
541 seq_printf(s, " %-9s", pull_up_down[pd]);
542 }
543 } else
544 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100545
546 if (pctldev)
547 mode = abx500_get_mode(pctldev, chip, offset);
548
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200549 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100550}
551
552static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
553{
554 unsigned i;
555 unsigned gpio = chip->base;
556 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
557 struct pinctrl_dev *pctldev = pct->pctldev;
558
559 for (i = 0; i < chip->ngpio; i++, gpio++) {
560 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
561 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
562 seq_printf(s, "\n");
563 }
564}
565
566#else
567static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000568 struct pinctrl_dev *pctldev,
569 struct gpio_chip *chip,
570 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100571{
572}
573#define abx500_gpio_dbg_show NULL
574#endif
575
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530576static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100577{
578 int gpio = chip->base + offset;
579
580 return pinctrl_request_gpio(gpio);
581}
582
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530583static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100584{
585 int gpio = chip->base + offset;
586
587 pinctrl_free_gpio(gpio);
588}
589
590static struct gpio_chip abx500gpio_chip = {
591 .label = "abx500-gpio",
592 .owner = THIS_MODULE,
593 .request = abx500_gpio_request,
594 .free = abx500_gpio_free,
595 .direction_input = abx500_gpio_direction_input,
596 .get = abx500_gpio_get,
597 .direction_output = abx500_gpio_direction_output,
598 .set = abx500_gpio_set,
599 .to_irq = abx500_gpio_to_irq,
600 .dbg_show = abx500_gpio_dbg_show,
601};
602
Patrice Chotard0493e642013-01-08 10:41:02 +0100603static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
604{
605 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
606
607 return pct->soc->nfunctions;
608}
609
610static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
611 unsigned function)
612{
613 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
614
615 return pct->soc->functions[function].name;
616}
617
618static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000619 unsigned function,
620 const char * const **groups,
621 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100622{
623 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
624
625 *groups = pct->soc->functions[function].groups;
626 *num_groups = pct->soc->functions[function].ngroups;
627
628 return 0;
629}
630
Patrice Chotard0493e642013-01-08 10:41:02 +0100631static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000632 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100633{
634 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
635 struct gpio_chip *chip = &pct->chip;
636 const struct abx500_pingroup *g;
637 int i;
638 int ret = 0;
639
640 g = &pct->soc->groups[group];
641 if (g->altsetting < 0)
642 return -EINVAL;
643
644 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
645
646 for (i = 0; i < g->npins; i++) {
647 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
648 g->pins[i], g->altsetting);
649
Patrice Chotard0493e642013-01-08 10:41:02 +0100650 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
651 }
Lee Jones83b423c2013-01-23 13:24:08 +0000652
Patrice Chotard0493e642013-01-08 10:41:02 +0100653 return ret;
654}
655
656static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000657 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100658{
659 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
660 const struct abx500_pingroup *g;
661
662 g = &pct->soc->groups[group];
663 if (g->altsetting < 0)
664 return;
665
666 /* FIXME: poke out the mux, set the pin to some default state? */
667 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
668}
669
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530670static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000671 struct pinctrl_gpio_range *range,
672 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100673{
674 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
675 const struct abx500_pinrange *p;
676 int ret;
677 int i;
678
679 /*
680 * Different ranges have different ways to enable GPIO function on a
681 * pin, so refer back to our local range type, where we handily define
682 * what altfunc enables GPIO for a certain pin.
683 */
684 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
685 p = &pct->soc->gpio_ranges[i];
686 if ((offset >= p->offset) &&
687 (offset < (p->offset + p->npins)))
688 break;
689 }
690
691 if (i == pct->soc->gpio_num_ranges) {
692 dev_err(pct->dev, "%s failed to locate range\n", __func__);
693 return -ENODEV;
694 }
695
696 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
697 p->altfunc, offset);
698
699 ret = abx500_set_mode(pct->pctldev, &pct->chip,
700 offset, p->altfunc);
701 if (ret < 0) {
702 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
703 return ret;
704 }
705
706 return ret;
707}
708
709static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000710 struct pinctrl_gpio_range *range,
711 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100712{
713}
714
Laurent Pinchart022ab142013-02-16 10:25:07 +0100715static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100716 .get_functions_count = abx500_pmx_get_funcs_cnt,
717 .get_function_name = abx500_pmx_get_func_name,
718 .get_function_groups = abx500_pmx_get_func_groups,
719 .enable = abx500_pmx_enable,
720 .disable = abx500_pmx_disable,
721 .gpio_request_enable = abx500_gpio_request_enable,
722 .gpio_disable_free = abx500_gpio_disable_free,
723};
724
725static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
726{
727 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
728
729 return pct->soc->ngroups;
730}
731
732static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000733 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100734{
735 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
736
737 return pct->soc->groups[selector].name;
738}
739
740static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000741 unsigned selector,
742 const unsigned **pins,
743 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100744{
745 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
746
747 *pins = pct->soc->groups[selector].pins;
748 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000749
Patrice Chotard0493e642013-01-08 10:41:02 +0100750 return 0;
751}
752
753static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000754 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100755{
756 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
757 struct gpio_chip *chip = &pct->chip;
758
759 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
760 chip->base + offset - 1);
761}
762
Patrice Chotard64a45c92013-06-20 16:04:59 +0200763static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
764 struct pinctrl_map *map, unsigned num_maps)
765{
766 int i;
767
768 for (i = 0; i < num_maps; i++)
769 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
770 kfree(map[i].data.configs.configs);
771 kfree(map);
772}
773
774static int abx500_dt_reserve_map(struct pinctrl_map **map,
775 unsigned *reserved_maps,
776 unsigned *num_maps,
777 unsigned reserve)
778{
779 unsigned old_num = *reserved_maps;
780 unsigned new_num = *num_maps + reserve;
781 struct pinctrl_map *new_map;
782
783 if (old_num >= new_num)
784 return 0;
785
786 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
787 if (!new_map)
788 return -ENOMEM;
789
790 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
791
792 *map = new_map;
793 *reserved_maps = new_num;
794
795 return 0;
796}
797
798static int abx500_dt_add_map_mux(struct pinctrl_map **map,
799 unsigned *reserved_maps,
800 unsigned *num_maps, const char *group,
801 const char *function)
802{
803 if (*num_maps == *reserved_maps)
804 return -ENOSPC;
805
806 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
807 (*map)[*num_maps].data.mux.group = group;
808 (*map)[*num_maps].data.mux.function = function;
809 (*num_maps)++;
810
811 return 0;
812}
813
814static int abx500_dt_add_map_configs(struct pinctrl_map **map,
815 unsigned *reserved_maps,
816 unsigned *num_maps, const char *group,
817 unsigned long *configs, unsigned num_configs)
818{
819 unsigned long *dup_configs;
820
821 if (*num_maps == *reserved_maps)
822 return -ENOSPC;
823
824 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
825 GFP_KERNEL);
826 if (!dup_configs)
827 return -ENOMEM;
828
829 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
830
831 (*map)[*num_maps].data.configs.group_or_pin = group;
832 (*map)[*num_maps].data.configs.configs = dup_configs;
833 (*map)[*num_maps].data.configs.num_configs = num_configs;
834 (*num_maps)++;
835
836 return 0;
837}
838
839static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
840 const char *pin_name)
841{
842 int i, pin_number;
843 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
844
845 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
846 for (i = 0; i < npct->soc->npins; i++)
847 if (npct->soc->pins[i].number == pin_number)
848 return npct->soc->pins[i].name;
849 return NULL;
850}
851
852static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
853 struct device_node *np,
854 struct pinctrl_map **map,
855 unsigned *reserved_maps,
856 unsigned *num_maps)
857{
858 int ret;
859 const char *function = NULL;
860 unsigned long *configs;
861 unsigned int nconfigs = 0;
862 bool has_config = 0;
863 unsigned reserve = 0;
864 struct property *prop;
865 const char *group, *gpio_name;
866 struct device_node *np_config;
867
868 ret = of_property_read_string(np, "ste,function", &function);
869 if (ret >= 0)
870 reserve = 1;
871
872 ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
873 if (nconfigs)
874 has_config = 1;
875
876 np_config = of_parse_phandle(np, "ste,config", 0);
877 if (np_config) {
878 ret = pinconf_generic_parse_dt_config(np_config, &configs,
879 &nconfigs);
880 if (ret)
881 goto exit;
882 has_config |= nconfigs;
883 }
884
885 ret = of_property_count_strings(np, "ste,pins");
886 if (ret < 0)
887 goto exit;
888
889 if (has_config)
890 reserve++;
891
892 reserve *= ret;
893
894 ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
895 if (ret < 0)
896 goto exit;
897
898 of_property_for_each_string(np, "ste,pins", prop, group) {
899 if (function) {
900 ret = abx500_dt_add_map_mux(map, reserved_maps,
901 num_maps, group, function);
902 if (ret < 0)
903 goto exit;
904 }
905 if (has_config) {
906 gpio_name = abx500_find_pin_name(pctldev, group);
907
908 ret = abx500_dt_add_map_configs(map, reserved_maps,
909 num_maps, gpio_name, configs, 1);
910 if (ret < 0)
911 goto exit;
912 }
913
914 }
915exit:
916 return ret;
917}
918
919static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
920 struct device_node *np_config,
921 struct pinctrl_map **map, unsigned *num_maps)
922{
923 unsigned reserved_maps;
924 struct device_node *np;
925 int ret;
926
927 reserved_maps = 0;
928 *map = NULL;
929 *num_maps = 0;
930
931 for_each_child_of_node(np_config, np) {
932 ret = abx500_dt_subnode_to_map(pctldev, np, map,
933 &reserved_maps, num_maps);
934 if (ret < 0) {
935 abx500_dt_free_map(pctldev, *map, *num_maps);
936 return ret;
937 }
938 }
939
940 return 0;
941}
942
Laurent Pinchart022ab142013-02-16 10:25:07 +0100943static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100944 .get_groups_count = abx500_get_groups_cnt,
945 .get_group_name = abx500_get_group_name,
946 .get_group_pins = abx500_get_group_pins,
947 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200948 .dt_node_to_map = abx500_dt_node_to_map,
949 .dt_free_map = abx500_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100950};
951
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530952static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000953 unsigned pin,
954 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100955{
Lee Jones1abeebe2012-12-20 11:11:19 +0000956 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100957}
958
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530959static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000960 unsigned pin,
961 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100962{
963 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
964 struct pullud *pullud = pct->soc->pullud;
965 struct gpio_chip *chip = &pct->chip;
966 unsigned offset;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200967 int ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100968 enum pin_config_param param = pinconf_to_config_param(config);
969 enum pin_config_param argument = pinconf_to_config_argument(config);
970
971 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
972 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
973 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
974 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000975
Patrice Chotard0493e642013-01-08 10:41:02 +0100976 /* on ABx500, there is no GPIO0, so adjust the offset */
977 offset = pin - 1;
978
979 switch (param) {
Patrice Chotard61ce1352013-06-20 16:05:00 +0200980 case PIN_CONFIG_BIAS_DISABLE:
981 ret = abx500_gpio_direction_input(chip, offset);
982 /*
983 * Some chips only support pull down, while some actually
984 * support both pull up and pull down. Such chips have
985 * a "pullud" range specified for the pins that support
986 * both features. If the pin is not within that range, we
987 * fall back to the old bit set that only support pull down.
988 */
989 if (pullud &&
990 pin >= pullud->first_pin &&
991 pin <= pullud->last_pin)
992 ret = abx500_set_pull_updown(pct,
993 pin,
994 ABX500_GPIO_PULL_NONE);
995 else
996 /* Chip only supports pull down */
997 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
998 offset, ABX500_GPIO_PULL_NONE);
999 break;
1000
Patrice Chotard0493e642013-01-08 10:41:02 +01001001 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001002 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard0493e642013-01-08 10:41:02 +01001003 /*
1004 * if argument = 1 set the pull down
1005 * else clear the pull down
Patrice Chotard0493e642013-01-08 10:41:02 +01001006 * Some chips only support pull down, while some actually
1007 * support both pull up and pull down. Such chips have
1008 * a "pullud" range specified for the pins that support
1009 * both features. If the pin is not within that range, we
1010 * fall back to the old bit set that only support pull down.
1011 */
1012 if (pullud &&
1013 pin >= pullud->first_pin &&
1014 pin <= pullud->last_pin)
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001015 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +01001016 pin,
1017 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
1018 else
1019 /* Chip only supports pull down */
1020 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1021 offset, argument ? 0 : 1);
1022 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001023
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001024 case PIN_CONFIG_BIAS_PULL_UP:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001025 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001026 /*
1027 * if argument = 1 set the pull up
1028 * else clear the pull up
1029 */
1030 ret = abx500_gpio_direction_input(chip, offset);
1031 /*
1032 * Some chips only support pull down, while some actually
1033 * support both pull up and pull down. Such chips have
1034 * a "pullud" range specified for the pins that support
1035 * both features. If the pin is not within that range, do
1036 * nothing
1037 */
1038 if (pullud &&
1039 pin >= pullud->first_pin &&
1040 pin <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001041 ret = abx500_set_pull_updown(pct,
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001042 pin,
1043 argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
1044 }
1045 break;
1046
Patrice Chotard0493e642013-01-08 10:41:02 +01001047 case PIN_CONFIG_OUTPUT:
1048 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +00001049
Patrice Chotard0493e642013-01-08 10:41:02 +01001050 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001051
Patrice Chotard0493e642013-01-08 10:41:02 +01001052 default:
1053 dev_err(chip->dev, "illegal configuration requested\n");
Patrice Chotard0493e642013-01-08 10:41:02 +01001054 }
Lee Jones83b423c2013-01-23 13:24:08 +00001055
Patrice Chotard0493e642013-01-08 10:41:02 +01001056 return ret;
1057}
1058
Laurent Pinchart022ab142013-02-16 10:25:07 +01001059static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001060 .pin_config_get = abx500_pin_config_get,
1061 .pin_config_set = abx500_pin_config_set,
1062};
1063
1064static struct pinctrl_desc abx500_pinctrl_desc = {
1065 .name = "pinctrl-abx500",
1066 .pctlops = &abx500_pinctrl_ops,
1067 .pmxops = &abx500_pinmux_ops,
1068 .confops = &abx500_pinconf_ops,
1069 .owner = THIS_MODULE,
1070};
1071
1072static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1073{
1074 unsigned int lowest = 0;
1075 unsigned int highest = 0;
1076 unsigned int npins = 0;
1077 int i;
1078
1079 /*
1080 * Compute number of GPIOs from the last SoC gpio range descriptors
1081 * These ranges may include "holes" but the GPIO number space shall
1082 * still be homogeneous, so we need to detect and account for any
1083 * such holes so that these are included in the number of GPIO pins.
1084 */
1085 for (i = 0; i < soc->gpio_num_ranges; i++) {
1086 unsigned gstart;
1087 unsigned gend;
1088 const struct abx500_pinrange *p;
1089
1090 p = &soc->gpio_ranges[i];
1091 gstart = p->offset;
1092 gend = p->offset + p->npins - 1;
1093
1094 if (i == 0) {
1095 /* First iteration, set start values */
1096 lowest = gstart;
1097 highest = gend;
1098 } else {
1099 if (gstart < lowest)
1100 lowest = gstart;
1101 if (gend > highest)
1102 highest = gend;
1103 }
1104 }
1105 /* this gives the absolute number of pins */
1106 npins = highest - lowest + 1;
1107 return npins;
1108}
1109
Lee Jonesf30a3832013-01-31 11:07:40 +00001110static const struct of_device_id abx500_gpio_match[] = {
1111 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1112 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1113 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1114 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001115 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001116};
1117
Patrice Chotard0493e642013-01-08 10:41:02 +01001118static int abx500_gpio_probe(struct platform_device *pdev)
1119{
1120 struct ab8500_platform_data *abx500_pdata =
1121 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +00001122 struct abx500_gpio_platform_data *pdata = NULL;
1123 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +01001124 struct abx500_pinctrl *pct;
1125 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +00001126 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +00001127 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +01001128 int i;
1129
Lee Jonesf30a3832013-01-31 11:07:40 +00001130 if (abx500_pdata)
1131 pdata = abx500_pdata->gpio;
Lee Jonesf30a3832013-01-31 11:07:40 +00001132
Lee Jones86c976e2013-05-08 14:29:08 +01001133 if (!(pdata || np)) {
1134 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
1135 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001136 }
1137
1138 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1139 GFP_KERNEL);
1140 if (pct == NULL) {
1141 dev_err(&pdev->dev,
1142 "failed to allocate memory for pct\n");
1143 return -ENOMEM;
1144 }
1145
1146 pct->dev = &pdev->dev;
1147 pct->parent = dev_get_drvdata(pdev->dev.parent);
1148 pct->chip = abx500gpio_chip;
1149 pct->chip.dev = &pdev->dev;
Lee Jonesf30a3832013-01-31 11:07:40 +00001150 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +01001151
Lee Jones86c976e2013-05-08 14:29:08 +01001152 if (platid)
1153 id = platid->driver_data;
1154 else if (np) {
1155 const struct of_device_id *match;
1156
1157 match = of_match_device(abx500_gpio_match, &pdev->dev);
1158 if (match)
1159 id = (unsigned long)match->data;
1160 }
1161
Patrice Chotard0493e642013-01-08 10:41:02 +01001162 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001163 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001164 case PINCTRL_AB8500:
1165 abx500_pinctrl_ab8500_init(&pct->soc);
1166 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001167 case PINCTRL_AB8540:
1168 abx500_pinctrl_ab8540_init(&pct->soc);
1169 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001170 case PINCTRL_AB9540:
1171 abx500_pinctrl_ab9540_init(&pct->soc);
1172 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001173 case PINCTRL_AB8505:
1174 abx500_pinctrl_ab8505_init(&pct->soc);
1175 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001176 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001177 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001178 return -EINVAL;
1179 }
1180
1181 if (!pct->soc) {
1182 dev_err(&pdev->dev, "Invalid SOC data\n");
1183 return -EINVAL;
1184 }
1185
1186 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1187 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1188 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001189
Patrice Chotard0493e642013-01-08 10:41:02 +01001190 ret = gpiochip_add(&pct->chip);
1191 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001192 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001193 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001194 }
1195 dev_info(&pdev->dev, "added gpiochip\n");
1196
1197 abx500_pinctrl_desc.pins = pct->soc->pins;
1198 abx500_pinctrl_desc.npins = pct->soc->npins;
1199 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
1200 if (!pct->pctldev) {
1201 dev_err(&pdev->dev,
1202 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +00001203 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +01001204 goto out_rem_chip;
1205 }
1206 dev_info(&pdev->dev, "registered pin controller\n");
1207
1208 /* We will handle a range of GPIO pins */
1209 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1210 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1211
1212 ret = gpiochip_add_pin_range(&pct->chip,
1213 dev_name(&pdev->dev),
1214 p->offset - 1, p->offset, p->npins);
1215 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001216 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001217 }
1218
1219 platform_set_drvdata(pdev, pct);
1220 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1221
1222 return 0;
1223
1224out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +00001225 err = gpiochip_remove(&pct->chip);
1226 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +01001227 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +00001228
Patrice Chotard0493e642013-01-08 10:41:02 +01001229 return ret;
1230}
1231
Lee Jones83b423c2013-01-23 13:24:08 +00001232/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001233 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001234 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001235 */
1236static int abx500_gpio_remove(struct platform_device *pdev)
1237{
1238 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1239 int ret;
1240
1241 ret = gpiochip_remove(&pct->chip);
1242 if (ret < 0) {
1243 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
1244 ret);
1245 return ret;
1246 }
1247
Patrice Chotard0493e642013-01-08 10:41:02 +01001248 return 0;
1249}
1250
1251static const struct platform_device_id abx500_pinctrl_id[] = {
1252 { "pinctrl-ab8500", PINCTRL_AB8500 },
1253 { "pinctrl-ab8540", PINCTRL_AB8540 },
1254 { "pinctrl-ab9540", PINCTRL_AB9540 },
1255 { "pinctrl-ab8505", PINCTRL_AB8505 },
1256 { },
1257};
1258
1259static struct platform_driver abx500_gpio_driver = {
1260 .driver = {
1261 .name = "abx500-gpio",
1262 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001263 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001264 },
1265 .probe = abx500_gpio_probe,
1266 .remove = abx500_gpio_remove,
1267 .id_table = abx500_pinctrl_id,
1268};
1269
1270static int __init abx500_gpio_init(void)
1271{
1272 return platform_driver_register(&abx500_gpio_driver);
1273}
1274core_initcall(abx500_gpio_init);
1275
1276MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1277MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1278MODULE_ALIAS("platform:abx500-gpio");
1279MODULE_LICENSE("GPL v2");