blob: c3f2b4baeed2d3e11f1208b9f47ce3f1b2c1bf92 [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;
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200165 bool is_out;
166 u8 gpio_offset = offset - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100167 int ret;
168
Patrice Chotardd8d4f7f2013-06-20 16:05:43 +0200169 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
170 gpio_offset, &is_out);
171 if (ret < 0) {
172 dev_err(pct->dev, "%s failed\n", __func__);
173 return ret;
174 }
175
176 if (is_out)
177 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
178 gpio_offset, &bit);
179 else
180 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
181 gpio_offset, &bit);
Patrice Chotard0493e642013-01-08 10:41:02 +0100182 if (ret < 0) {
183 dev_err(pct->dev, "%s failed\n", __func__);
184 return ret;
185 }
Lee Jones83b423c2013-01-23 13:24:08 +0000186
Patrice Chotard0493e642013-01-08 10:41:02 +0100187 return bit;
188}
189
190static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
191{
192 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
193 int ret;
194
195 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
196 if (ret < 0)
197 dev_err(pct->dev, "%s write failed\n", __func__);
198}
199
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200200static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
201 enum abx500_gpio_pull_updown *pull_updown)
202{
203 u8 pos;
204 u8 val;
205 int ret;
206 struct pullud *pullud;
207
208 if (!pct->soc->pullud) {
209 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
210 __func__);
211 ret = -EPERM;
212 goto out;
213 }
214
215 pullud = pct->soc->pullud;
216
217 if ((offset < pullud->first_pin)
218 || (offset > pullud->last_pin)) {
219 ret = -EINVAL;
220 goto out;
221 }
222
223 ret = abx500_get_register_interruptible(pct->dev,
224 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
225
226 pos = (offset - pullud->first_pin) << 1;
227 *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
228
229out:
230 if (ret < 0)
231 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
232
233 return ret;
234}
235
236static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
237 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100238{
239 u8 pos;
240 int ret;
241 struct pullud *pullud;
242
243 if (!pct->soc->pullud) {
244 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
245 __func__);
246 ret = -EPERM;
247 goto out;
248 }
249
250 pullud = pct->soc->pullud;
251
252 if ((offset < pullud->first_pin)
253 || (offset > pullud->last_pin)) {
254 ret = -EINVAL;
255 goto out;
256 }
Patrice Chotard10a8be52013-05-24 14:06:29 +0200257 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100258
259 ret = abx500_mask_and_set_register_interruptible(pct->dev,
260 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
261 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
262
263out:
264 if (ret < 0)
265 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000266
Patrice Chotard0493e642013-01-08 10:41:02 +0100267 return ret;
268}
269
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200270static bool abx500_pullud_supported(struct gpio_chip *chip, unsigned gpio)
271{
272 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
273 struct pullud *pullud = pct->soc->pullud;
274
275 return (pullud &&
276 gpio >= pullud->first_pin &&
277 gpio <= pullud->last_pin);
278}
279
Patrice Chotard0493e642013-01-08 10:41:02 +0100280static int abx500_gpio_direction_output(struct gpio_chip *chip,
281 unsigned offset,
282 int val)
283{
284 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Patrice Chotard0493e642013-01-08 10:41:02 +0100285 unsigned gpio;
286 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000287
Patrice Chotard0493e642013-01-08 10:41:02 +0100288 /* set direction as output */
289 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
290 if (ret < 0)
291 return ret;
292
293 /* disable pull down */
294 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
295 if (ret < 0)
296 return ret;
297
298 /* if supported, disable both pull down and pull up */
299 gpio = offset + 1;
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200300 if (abx500_pullud_supported(chip, gpio)) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200301 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100302 gpio,
303 ABX500_GPIO_PULL_NONE);
304 if (ret < 0)
305 return ret;
306 }
Lee Jones83b423c2013-01-23 13:24:08 +0000307
Patrice Chotard0493e642013-01-08 10:41:02 +0100308 /* set the output as 1 or 0 */
309 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
310}
311
312static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
313{
314 /* set the register as input */
315 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
316}
317
318static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
319{
320 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000321 /* The AB8500 GPIO numbers are off by one */
322 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000323 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100324 int i;
325
326 for (i = 0; i < pct->irq_cluster_size; i++) {
327 struct abx500_gpio_irq_cluster *cluster =
328 &pct->irq_cluster[i];
329
Lee Jonesa6a16d22013-01-31 09:57:52 +0000330 if (gpio >= cluster->start && gpio <= cluster->end) {
331 /*
332 * The ABx500 GPIO's associated IRQs are clustered together
333 * throughout the interrupt numbers at irregular intervals.
334 * To solve this quandry, we have placed the read-in values
335 * into the cluster information table.
336 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100337 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000338 return irq_create_mapping(pct->parent->domain, hwirq);
339 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100340 }
341
342 return -EINVAL;
343}
344
345static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000346 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100347{
348 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
349 struct alternate_functions af = pct->soc->alternate_functions[gpio];
350 int ret;
351 int val;
352 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000353
Patrice Chotard0493e642013-01-08 10:41:02 +0100354 const char *modes[] = {
355 [ABX500_DEFAULT] = "default",
356 [ABX500_ALT_A] = "altA",
357 [ABX500_ALT_B] = "altB",
358 [ABX500_ALT_C] = "altC",
359 };
360
361 /* sanity check */
362 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
363 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
364 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
365 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
366 modes[alt_setting]);
367 return -EINVAL;
368 }
369
370 /* on ABx5xx, there is no GPIO0, so adjust the offset */
371 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000372
Patrice Chotard0493e642013-01-08 10:41:02 +0100373 switch (alt_setting) {
374 case ABX500_DEFAULT:
375 /*
376 * for ABx5xx family, default mode is always selected by
377 * writing 0 to GPIOSELx register, except for pins which
378 * support at least ALT_B mode, default mode is selected
379 * by writing 1 to GPIOSELx register
380 */
381 val = 0;
382 if (af.alt_bit1 != UNUSED)
383 val++;
384
385 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
386 offset, val);
387 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000388
Patrice Chotard0493e642013-01-08 10:41:02 +0100389 case ABX500_ALT_A:
390 /*
391 * for ABx5xx family, alt_a mode is always selected by
392 * writing 1 to GPIOSELx register, except for pins which
393 * support at least ALT_B mode, alt_a mode is selected
394 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
395 * register
396 */
397 if (af.alt_bit1 != UNUSED) {
398 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
399 offset, 0);
400 ret = abx500_gpio_set_bits(chip,
401 AB8500_GPIO_ALTFUN_REG,
402 af.alt_bit1,
403 !!(af.alta_val && BIT(0)));
404 if (af.alt_bit2 != UNUSED)
405 ret = abx500_gpio_set_bits(chip,
406 AB8500_GPIO_ALTFUN_REG,
407 af.alt_bit2,
408 !!(af.alta_val && BIT(1)));
409 } else
410 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
411 offset, 1);
412 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000413
Patrice Chotard0493e642013-01-08 10:41:02 +0100414 case ABX500_ALT_B:
415 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
416 offset, 0);
417 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
418 af.alt_bit1, !!(af.altb_val && BIT(0)));
419 if (af.alt_bit2 != UNUSED)
420 ret = abx500_gpio_set_bits(chip,
421 AB8500_GPIO_ALTFUN_REG,
422 af.alt_bit2,
423 !!(af.altb_val && BIT(1)));
424 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000425
Patrice Chotard0493e642013-01-08 10:41:02 +0100426 case ABX500_ALT_C:
427 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
428 offset, 0);
429 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
430 af.alt_bit2, !!(af.altc_val && BIT(0)));
431 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
432 af.alt_bit2, !!(af.altc_val && BIT(1)));
433 break;
434
435 default:
436 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000437
Patrice Chotard0493e642013-01-08 10:41:02 +0100438 return -EINVAL;
439 }
Lee Jones83b423c2013-01-23 13:24:08 +0000440
Patrice Chotard0493e642013-01-08 10:41:02 +0100441 return ret;
442}
443
444static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000445 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100446{
447 u8 mode;
448 bool bit_mode;
449 bool alt_bit1;
450 bool alt_bit2;
451 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
452 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100453 /* on ABx5xx, there is no GPIO0, so adjust the offset */
454 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100455
456 /*
457 * if gpiosel_bit is set to unused,
458 * it means no GPIO or special case
459 */
460 if (af.gpiosel_bit == UNUSED)
461 return ABX500_DEFAULT;
462
463 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100464 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100465 af.gpiosel_bit, &bit_mode);
466 mode = bit_mode;
467
468 /* sanity check */
469 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
470 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
471 dev_err(pct->dev,
472 "alt_bitX value not in correct range (-1 to 7)\n");
473 return -EINVAL;
474 }
Lee Jones83b423c2013-01-23 13:24:08 +0000475
Patrice Chotard0493e642013-01-08 10:41:02 +0100476 /* if alt_bit2 is used, alt_bit1 must be used too */
477 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
478 dev_err(pct->dev,
479 "if alt_bit2 is used, alt_bit1 can't be unused\n");
480 return -EINVAL;
481 }
482
483 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800484 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100485 return mode;
486 /*
487 * if pin GPIOSEL bit is set and pin supports alternate function,
488 * it means DEFAULT mode
489 */
490 if (mode)
491 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000492
Patrice Chotard0493e642013-01-08 10:41:02 +0100493 /*
494 * pin use the AlternatFunction register
495 * read alt_bit1 value
496 */
497 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
498 af.alt_bit1, &alt_bit1);
499
500 if (af.alt_bit2 != UNUSED)
501 /* read alt_bit2 value */
502 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
503 &alt_bit2);
504 else
505 alt_bit2 = 0;
506
507 mode = (alt_bit2 << 1) + alt_bit1;
508 if (mode == af.alta_val)
509 return ABX500_ALT_A;
510 else if (mode == af.altb_val)
511 return ABX500_ALT_B;
512 else
513 return ABX500_ALT_C;
514}
515
516#ifdef CONFIG_DEBUG_FS
517
518#include <linux/seq_file.h>
519
520static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000521 struct pinctrl_dev *pctldev,
522 struct gpio_chip *chip,
523 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100524{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200525 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100526 const char *label = gpiochip_is_requested(chip, offset - 1);
527 u8 gpio_offset = offset - 1;
528 int mode = -1;
529 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200530 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200531 enum abx500_gpio_pull_updown pud = 0;
Lee Jones83b423c2013-01-23 13:24:08 +0000532
Patrice Chotard0493e642013-01-08 10:41:02 +0100533 const char *modes[] = {
534 [ABX500_DEFAULT] = "default",
535 [ABX500_ALT_A] = "altA",
536 [ABX500_ALT_B] = "altB",
537 [ABX500_ALT_C] = "altC",
538 };
539
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200540 const char *pull_up_down[] = {
541 [ABX500_GPIO_PULL_DOWN] = "pull down",
542 [ABX500_GPIO_PULL_NONE] = "pull none",
543 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
544 [ABX500_GPIO_PULL_UP] = "pull up",
545 };
546
Patrice Chotard0493e642013-01-08 10:41:02 +0100547 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200548
549 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
550 gpio, label ?: "(none)",
551 is_out ? "out" : "in ");
552
553 if (!is_out) {
Patrice Chotard8b5abd12013-06-20 16:05:44 +0200554 if (abx500_pullud_supported(chip, offset)) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200555 abx500_get_pull_updown(pct, offset, &pud);
556 seq_printf(s, " %-9s", pull_up_down[pud]);
557 } else {
558 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
559 gpio_offset, &pd);
560 seq_printf(s, " %-9s", pull_up_down[pd]);
561 }
562 } else
563 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100564
565 if (pctldev)
566 mode = abx500_get_mode(pctldev, chip, offset);
567
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200568 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100569}
570
571static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
572{
573 unsigned i;
574 unsigned gpio = chip->base;
575 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
576 struct pinctrl_dev *pctldev = pct->pctldev;
577
578 for (i = 0; i < chip->ngpio; i++, gpio++) {
579 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
580 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
581 seq_printf(s, "\n");
582 }
583}
584
585#else
586static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000587 struct pinctrl_dev *pctldev,
588 struct gpio_chip *chip,
589 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100590{
591}
592#define abx500_gpio_dbg_show NULL
593#endif
594
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530595static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100596{
597 int gpio = chip->base + offset;
598
599 return pinctrl_request_gpio(gpio);
600}
601
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530602static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100603{
604 int gpio = chip->base + offset;
605
606 pinctrl_free_gpio(gpio);
607}
608
609static struct gpio_chip abx500gpio_chip = {
610 .label = "abx500-gpio",
611 .owner = THIS_MODULE,
612 .request = abx500_gpio_request,
613 .free = abx500_gpio_free,
614 .direction_input = abx500_gpio_direction_input,
615 .get = abx500_gpio_get,
616 .direction_output = abx500_gpio_direction_output,
617 .set = abx500_gpio_set,
618 .to_irq = abx500_gpio_to_irq,
619 .dbg_show = abx500_gpio_dbg_show,
620};
621
Patrice Chotard0493e642013-01-08 10:41:02 +0100622static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
623{
624 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
625
626 return pct->soc->nfunctions;
627}
628
629static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
630 unsigned function)
631{
632 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
633
634 return pct->soc->functions[function].name;
635}
636
637static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000638 unsigned function,
639 const char * const **groups,
640 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100641{
642 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
643
644 *groups = pct->soc->functions[function].groups;
645 *num_groups = pct->soc->functions[function].ngroups;
646
647 return 0;
648}
649
Patrice Chotard0493e642013-01-08 10:41:02 +0100650static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000651 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100652{
653 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
654 struct gpio_chip *chip = &pct->chip;
655 const struct abx500_pingroup *g;
656 int i;
657 int ret = 0;
658
659 g = &pct->soc->groups[group];
660 if (g->altsetting < 0)
661 return -EINVAL;
662
663 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
664
665 for (i = 0; i < g->npins; i++) {
666 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
667 g->pins[i], g->altsetting);
668
Patrice Chotard0493e642013-01-08 10:41:02 +0100669 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
670 }
Lee Jones83b423c2013-01-23 13:24:08 +0000671
Patrice Chotard0493e642013-01-08 10:41:02 +0100672 return ret;
673}
674
675static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000676 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100677{
678 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
679 const struct abx500_pingroup *g;
680
681 g = &pct->soc->groups[group];
682 if (g->altsetting < 0)
683 return;
684
685 /* FIXME: poke out the mux, set the pin to some default state? */
686 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
687}
688
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530689static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000690 struct pinctrl_gpio_range *range,
691 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100692{
693 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
694 const struct abx500_pinrange *p;
695 int ret;
696 int i;
697
698 /*
699 * Different ranges have different ways to enable GPIO function on a
700 * pin, so refer back to our local range type, where we handily define
701 * what altfunc enables GPIO for a certain pin.
702 */
703 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
704 p = &pct->soc->gpio_ranges[i];
705 if ((offset >= p->offset) &&
706 (offset < (p->offset + p->npins)))
707 break;
708 }
709
710 if (i == pct->soc->gpio_num_ranges) {
711 dev_err(pct->dev, "%s failed to locate range\n", __func__);
712 return -ENODEV;
713 }
714
715 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
716 p->altfunc, offset);
717
718 ret = abx500_set_mode(pct->pctldev, &pct->chip,
719 offset, p->altfunc);
720 if (ret < 0) {
721 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
722 return ret;
723 }
724
725 return ret;
726}
727
728static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000729 struct pinctrl_gpio_range *range,
730 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100731{
732}
733
Laurent Pinchart022ab142013-02-16 10:25:07 +0100734static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100735 .get_functions_count = abx500_pmx_get_funcs_cnt,
736 .get_function_name = abx500_pmx_get_func_name,
737 .get_function_groups = abx500_pmx_get_func_groups,
738 .enable = abx500_pmx_enable,
739 .disable = abx500_pmx_disable,
740 .gpio_request_enable = abx500_gpio_request_enable,
741 .gpio_disable_free = abx500_gpio_disable_free,
742};
743
744static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
745{
746 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
747
748 return pct->soc->ngroups;
749}
750
751static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000752 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100753{
754 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
755
756 return pct->soc->groups[selector].name;
757}
758
759static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000760 unsigned selector,
761 const unsigned **pins,
762 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100763{
764 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
765
766 *pins = pct->soc->groups[selector].pins;
767 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000768
Patrice Chotard0493e642013-01-08 10:41:02 +0100769 return 0;
770}
771
772static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000773 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100774{
775 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
776 struct gpio_chip *chip = &pct->chip;
777
778 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
779 chip->base + offset - 1);
780}
781
Patrice Chotard64a45c92013-06-20 16:04:59 +0200782static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
783 struct pinctrl_map *map, unsigned num_maps)
784{
785 int i;
786
787 for (i = 0; i < num_maps; i++)
788 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
789 kfree(map[i].data.configs.configs);
790 kfree(map);
791}
792
793static int abx500_dt_reserve_map(struct pinctrl_map **map,
794 unsigned *reserved_maps,
795 unsigned *num_maps,
796 unsigned reserve)
797{
798 unsigned old_num = *reserved_maps;
799 unsigned new_num = *num_maps + reserve;
800 struct pinctrl_map *new_map;
801
802 if (old_num >= new_num)
803 return 0;
804
805 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
806 if (!new_map)
807 return -ENOMEM;
808
809 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
810
811 *map = new_map;
812 *reserved_maps = new_num;
813
814 return 0;
815}
816
817static int abx500_dt_add_map_mux(struct pinctrl_map **map,
818 unsigned *reserved_maps,
819 unsigned *num_maps, const char *group,
820 const char *function)
821{
822 if (*num_maps == *reserved_maps)
823 return -ENOSPC;
824
825 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
826 (*map)[*num_maps].data.mux.group = group;
827 (*map)[*num_maps].data.mux.function = function;
828 (*num_maps)++;
829
830 return 0;
831}
832
833static int abx500_dt_add_map_configs(struct pinctrl_map **map,
834 unsigned *reserved_maps,
835 unsigned *num_maps, const char *group,
836 unsigned long *configs, unsigned num_configs)
837{
838 unsigned long *dup_configs;
839
840 if (*num_maps == *reserved_maps)
841 return -ENOSPC;
842
843 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
844 GFP_KERNEL);
845 if (!dup_configs)
846 return -ENOMEM;
847
848 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
849
850 (*map)[*num_maps].data.configs.group_or_pin = group;
851 (*map)[*num_maps].data.configs.configs = dup_configs;
852 (*map)[*num_maps].data.configs.num_configs = num_configs;
853 (*num_maps)++;
854
855 return 0;
856}
857
858static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
859 const char *pin_name)
860{
861 int i, pin_number;
862 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
863
864 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
865 for (i = 0; i < npct->soc->npins; i++)
866 if (npct->soc->pins[i].number == pin_number)
867 return npct->soc->pins[i].name;
868 return NULL;
869}
870
871static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
872 struct device_node *np,
873 struct pinctrl_map **map,
874 unsigned *reserved_maps,
875 unsigned *num_maps)
876{
877 int ret;
878 const char *function = NULL;
879 unsigned long *configs;
880 unsigned int nconfigs = 0;
881 bool has_config = 0;
882 unsigned reserve = 0;
883 struct property *prop;
884 const char *group, *gpio_name;
885 struct device_node *np_config;
886
887 ret = of_property_read_string(np, "ste,function", &function);
888 if (ret >= 0)
889 reserve = 1;
890
891 ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
892 if (nconfigs)
893 has_config = 1;
894
895 np_config = of_parse_phandle(np, "ste,config", 0);
896 if (np_config) {
897 ret = pinconf_generic_parse_dt_config(np_config, &configs,
898 &nconfigs);
899 if (ret)
900 goto exit;
901 has_config |= nconfigs;
902 }
903
904 ret = of_property_count_strings(np, "ste,pins");
905 if (ret < 0)
906 goto exit;
907
908 if (has_config)
909 reserve++;
910
911 reserve *= ret;
912
913 ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
914 if (ret < 0)
915 goto exit;
916
917 of_property_for_each_string(np, "ste,pins", prop, group) {
918 if (function) {
919 ret = abx500_dt_add_map_mux(map, reserved_maps,
920 num_maps, group, function);
921 if (ret < 0)
922 goto exit;
923 }
924 if (has_config) {
925 gpio_name = abx500_find_pin_name(pctldev, group);
926
927 ret = abx500_dt_add_map_configs(map, reserved_maps,
928 num_maps, gpio_name, configs, 1);
929 if (ret < 0)
930 goto exit;
931 }
932
933 }
934exit:
935 return ret;
936}
937
938static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
939 struct device_node *np_config,
940 struct pinctrl_map **map, unsigned *num_maps)
941{
942 unsigned reserved_maps;
943 struct device_node *np;
944 int ret;
945
946 reserved_maps = 0;
947 *map = NULL;
948 *num_maps = 0;
949
950 for_each_child_of_node(np_config, np) {
951 ret = abx500_dt_subnode_to_map(pctldev, np, map,
952 &reserved_maps, num_maps);
953 if (ret < 0) {
954 abx500_dt_free_map(pctldev, *map, *num_maps);
955 return ret;
956 }
957 }
958
959 return 0;
960}
961
Laurent Pinchart022ab142013-02-16 10:25:07 +0100962static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100963 .get_groups_count = abx500_get_groups_cnt,
964 .get_group_name = abx500_get_group_name,
965 .get_group_pins = abx500_get_group_pins,
966 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200967 .dt_node_to_map = abx500_dt_node_to_map,
968 .dt_free_map = abx500_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100969};
970
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530971static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000972 unsigned pin,
973 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100974{
Lee Jones1abeebe2012-12-20 11:11:19 +0000975 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100976}
977
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530978static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000979 unsigned pin,
980 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100981{
982 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
Patrice Chotard0493e642013-01-08 10:41:02 +0100983 struct gpio_chip *chip = &pct->chip;
984 unsigned offset;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200985 int ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100986 enum pin_config_param param = pinconf_to_config_param(config);
987 enum pin_config_param argument = pinconf_to_config_argument(config);
988
989 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
990 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
991 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
992 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000993
Patrice Chotard0493e642013-01-08 10:41:02 +0100994 /* on ABx500, there is no GPIO0, so adjust the offset */
995 offset = pin - 1;
996
997 switch (param) {
Patrice Chotard61ce1352013-06-20 16:05:00 +0200998 case PIN_CONFIG_BIAS_DISABLE:
999 ret = abx500_gpio_direction_input(chip, offset);
1000 /*
1001 * Some chips only support pull down, while some actually
1002 * support both pull up and pull down. Such chips have
1003 * a "pullud" range specified for the pins that support
1004 * both features. If the pin is not within that range, we
1005 * fall back to the old bit set that only support pull down.
1006 */
Patrice Chotard8b5abd12013-06-20 16:05:44 +02001007 if (abx500_pullud_supported(chip, pin))
Patrice Chotard61ce1352013-06-20 16:05:00 +02001008 ret = abx500_set_pull_updown(pct,
1009 pin,
1010 ABX500_GPIO_PULL_NONE);
1011 else
1012 /* Chip only supports pull down */
1013 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1014 offset, ABX500_GPIO_PULL_NONE);
1015 break;
1016
Patrice Chotard0493e642013-01-08 10:41:02 +01001017 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001018 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard0493e642013-01-08 10:41:02 +01001019 /*
1020 * if argument = 1 set the pull down
1021 * else clear the pull down
Patrice Chotard0493e642013-01-08 10:41:02 +01001022 * Some chips only support pull down, while some actually
1023 * support both pull up and pull down. Such chips have
1024 * a "pullud" range specified for the pins that support
1025 * both features. If the pin is not within that range, we
1026 * fall back to the old bit set that only support pull down.
1027 */
Patrice Chotard8b5abd12013-06-20 16:05:44 +02001028 if (abx500_pullud_supported(chip, pin))
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001029 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +01001030 pin,
1031 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
1032 else
1033 /* Chip only supports pull down */
1034 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1035 offset, argument ? 0 : 1);
1036 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001037
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001038 case PIN_CONFIG_BIAS_PULL_UP:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001039 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001040 /*
1041 * if argument = 1 set the pull up
1042 * else clear the pull up
1043 */
1044 ret = abx500_gpio_direction_input(chip, offset);
1045 /*
1046 * Some chips only support pull down, while some actually
1047 * support both pull up and pull down. Such chips have
1048 * a "pullud" range specified for the pins that support
1049 * both features. If the pin is not within that range, do
1050 * nothing
1051 */
Patrice Chotard8b5abd12013-06-20 16:05:44 +02001052 if (abx500_pullud_supported(chip, pin))
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001053 ret = abx500_set_pull_updown(pct,
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001054 pin,
1055 argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001056 break;
1057
Patrice Chotard0493e642013-01-08 10:41:02 +01001058 case PIN_CONFIG_OUTPUT:
1059 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +00001060
Patrice Chotard0493e642013-01-08 10:41:02 +01001061 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001062
Patrice Chotard0493e642013-01-08 10:41:02 +01001063 default:
1064 dev_err(chip->dev, "illegal configuration requested\n");
Patrice Chotard0493e642013-01-08 10:41:02 +01001065 }
Lee Jones83b423c2013-01-23 13:24:08 +00001066
Patrice Chotard0493e642013-01-08 10:41:02 +01001067 return ret;
1068}
1069
Laurent Pinchart022ab142013-02-16 10:25:07 +01001070static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001071 .pin_config_get = abx500_pin_config_get,
1072 .pin_config_set = abx500_pin_config_set,
1073};
1074
1075static struct pinctrl_desc abx500_pinctrl_desc = {
1076 .name = "pinctrl-abx500",
1077 .pctlops = &abx500_pinctrl_ops,
1078 .pmxops = &abx500_pinmux_ops,
1079 .confops = &abx500_pinconf_ops,
1080 .owner = THIS_MODULE,
1081};
1082
1083static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1084{
1085 unsigned int lowest = 0;
1086 unsigned int highest = 0;
1087 unsigned int npins = 0;
1088 int i;
1089
1090 /*
1091 * Compute number of GPIOs from the last SoC gpio range descriptors
1092 * These ranges may include "holes" but the GPIO number space shall
1093 * still be homogeneous, so we need to detect and account for any
1094 * such holes so that these are included in the number of GPIO pins.
1095 */
1096 for (i = 0; i < soc->gpio_num_ranges; i++) {
1097 unsigned gstart;
1098 unsigned gend;
1099 const struct abx500_pinrange *p;
1100
1101 p = &soc->gpio_ranges[i];
1102 gstart = p->offset;
1103 gend = p->offset + p->npins - 1;
1104
1105 if (i == 0) {
1106 /* First iteration, set start values */
1107 lowest = gstart;
1108 highest = gend;
1109 } else {
1110 if (gstart < lowest)
1111 lowest = gstart;
1112 if (gend > highest)
1113 highest = gend;
1114 }
1115 }
1116 /* this gives the absolute number of pins */
1117 npins = highest - lowest + 1;
1118 return npins;
1119}
1120
Lee Jonesf30a3832013-01-31 11:07:40 +00001121static const struct of_device_id abx500_gpio_match[] = {
1122 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1123 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1124 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1125 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001126 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001127};
1128
Patrice Chotard0493e642013-01-08 10:41:02 +01001129static int abx500_gpio_probe(struct platform_device *pdev)
1130{
1131 struct ab8500_platform_data *abx500_pdata =
1132 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +00001133 struct abx500_gpio_platform_data *pdata = NULL;
1134 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +01001135 struct abx500_pinctrl *pct;
1136 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +00001137 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +00001138 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +01001139 int i;
1140
Lee Jonesf30a3832013-01-31 11:07:40 +00001141 if (abx500_pdata)
1142 pdata = abx500_pdata->gpio;
Lee Jonesf30a3832013-01-31 11:07:40 +00001143
Lee Jones86c976e2013-05-08 14:29:08 +01001144 if (!(pdata || np)) {
1145 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
1146 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001147 }
1148
1149 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1150 GFP_KERNEL);
1151 if (pct == NULL) {
1152 dev_err(&pdev->dev,
1153 "failed to allocate memory for pct\n");
1154 return -ENOMEM;
1155 }
1156
1157 pct->dev = &pdev->dev;
1158 pct->parent = dev_get_drvdata(pdev->dev.parent);
1159 pct->chip = abx500gpio_chip;
1160 pct->chip.dev = &pdev->dev;
Lee Jonesf30a3832013-01-31 11:07:40 +00001161 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +01001162
Lee Jones86c976e2013-05-08 14:29:08 +01001163 if (platid)
1164 id = platid->driver_data;
1165 else if (np) {
1166 const struct of_device_id *match;
1167
1168 match = of_match_device(abx500_gpio_match, &pdev->dev);
1169 if (match)
1170 id = (unsigned long)match->data;
1171 }
1172
Patrice Chotard0493e642013-01-08 10:41:02 +01001173 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001174 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001175 case PINCTRL_AB8500:
1176 abx500_pinctrl_ab8500_init(&pct->soc);
1177 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001178 case PINCTRL_AB8540:
1179 abx500_pinctrl_ab8540_init(&pct->soc);
1180 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001181 case PINCTRL_AB9540:
1182 abx500_pinctrl_ab9540_init(&pct->soc);
1183 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001184 case PINCTRL_AB8505:
1185 abx500_pinctrl_ab8505_init(&pct->soc);
1186 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001187 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001188 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001189 return -EINVAL;
1190 }
1191
1192 if (!pct->soc) {
1193 dev_err(&pdev->dev, "Invalid SOC data\n");
1194 return -EINVAL;
1195 }
1196
1197 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1198 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1199 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001200
Patrice Chotard0493e642013-01-08 10:41:02 +01001201 ret = gpiochip_add(&pct->chip);
1202 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001203 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001204 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001205 }
1206 dev_info(&pdev->dev, "added gpiochip\n");
1207
1208 abx500_pinctrl_desc.pins = pct->soc->pins;
1209 abx500_pinctrl_desc.npins = pct->soc->npins;
1210 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
1211 if (!pct->pctldev) {
1212 dev_err(&pdev->dev,
1213 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +00001214 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +01001215 goto out_rem_chip;
1216 }
1217 dev_info(&pdev->dev, "registered pin controller\n");
1218
1219 /* We will handle a range of GPIO pins */
1220 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1221 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1222
1223 ret = gpiochip_add_pin_range(&pct->chip,
1224 dev_name(&pdev->dev),
1225 p->offset - 1, p->offset, p->npins);
1226 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001227 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001228 }
1229
1230 platform_set_drvdata(pdev, pct);
1231 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1232
1233 return 0;
1234
1235out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +00001236 err = gpiochip_remove(&pct->chip);
1237 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +01001238 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +00001239
Patrice Chotard0493e642013-01-08 10:41:02 +01001240 return ret;
1241}
1242
Lee Jones83b423c2013-01-23 13:24:08 +00001243/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001244 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001245 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001246 */
1247static int abx500_gpio_remove(struct platform_device *pdev)
1248{
1249 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1250 int ret;
1251
1252 ret = gpiochip_remove(&pct->chip);
1253 if (ret < 0) {
1254 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
1255 ret);
1256 return ret;
1257 }
1258
Patrice Chotard0493e642013-01-08 10:41:02 +01001259 return 0;
1260}
1261
1262static const struct platform_device_id abx500_pinctrl_id[] = {
1263 { "pinctrl-ab8500", PINCTRL_AB8500 },
1264 { "pinctrl-ab8540", PINCTRL_AB8540 },
1265 { "pinctrl-ab9540", PINCTRL_AB9540 },
1266 { "pinctrl-ab8505", PINCTRL_AB8505 },
1267 { },
1268};
1269
1270static struct platform_driver abx500_gpio_driver = {
1271 .driver = {
1272 .name = "abx500-gpio",
1273 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001274 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001275 },
1276 .probe = abx500_gpio_probe,
1277 .remove = abx500_gpio_remove,
1278 .id_table = abx500_pinctrl_id,
1279};
1280
1281static int __init abx500_gpio_init(void)
1282{
1283 return platform_driver_register(&abx500_gpio_driver);
1284}
1285core_initcall(abx500_gpio_init);
1286
1287MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1288MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1289MODULE_ALIAS("platform:abx500-gpio");
1290MODULE_LICENSE("GPL v2");