blob: d9d7ffc35c3eee00bc9ba4cae53b9e08b0939471 [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
270static int abx500_gpio_direction_output(struct gpio_chip *chip,
271 unsigned offset,
272 int val)
273{
274 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
275 struct pullud *pullud = pct->soc->pullud;
276 unsigned gpio;
277 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000278
Patrice Chotard0493e642013-01-08 10:41:02 +0100279 /* set direction as output */
280 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
281 if (ret < 0)
282 return ret;
283
284 /* disable pull down */
285 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
286 if (ret < 0)
287 return ret;
288
289 /* if supported, disable both pull down and pull up */
290 gpio = offset + 1;
291 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200292 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +0100293 gpio,
294 ABX500_GPIO_PULL_NONE);
295 if (ret < 0)
296 return ret;
297 }
Lee Jones83b423c2013-01-23 13:24:08 +0000298
Patrice Chotard0493e642013-01-08 10:41:02 +0100299 /* set the output as 1 or 0 */
300 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
301}
302
303static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
304{
305 /* set the register as input */
306 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
307}
308
309static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
310{
311 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000312 /* The AB8500 GPIO numbers are off by one */
313 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000314 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100315 int i;
316
317 for (i = 0; i < pct->irq_cluster_size; i++) {
318 struct abx500_gpio_irq_cluster *cluster =
319 &pct->irq_cluster[i];
320
Lee Jonesa6a16d22013-01-31 09:57:52 +0000321 if (gpio >= cluster->start && gpio <= cluster->end) {
322 /*
323 * The ABx500 GPIO's associated IRQs are clustered together
324 * throughout the interrupt numbers at irregular intervals.
325 * To solve this quandry, we have placed the read-in values
326 * into the cluster information table.
327 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100328 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000329 return irq_create_mapping(pct->parent->domain, hwirq);
330 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100331 }
332
333 return -EINVAL;
334}
335
336static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000337 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100338{
339 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
340 struct alternate_functions af = pct->soc->alternate_functions[gpio];
341 int ret;
342 int val;
343 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000344
Patrice Chotard0493e642013-01-08 10:41:02 +0100345 const char *modes[] = {
346 [ABX500_DEFAULT] = "default",
347 [ABX500_ALT_A] = "altA",
348 [ABX500_ALT_B] = "altB",
349 [ABX500_ALT_C] = "altC",
350 };
351
352 /* sanity check */
353 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
354 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
355 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
356 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
357 modes[alt_setting]);
358 return -EINVAL;
359 }
360
361 /* on ABx5xx, there is no GPIO0, so adjust the offset */
362 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000363
Patrice Chotard0493e642013-01-08 10:41:02 +0100364 switch (alt_setting) {
365 case ABX500_DEFAULT:
366 /*
367 * for ABx5xx family, default mode is always selected by
368 * writing 0 to GPIOSELx register, except for pins which
369 * support at least ALT_B mode, default mode is selected
370 * by writing 1 to GPIOSELx register
371 */
372 val = 0;
373 if (af.alt_bit1 != UNUSED)
374 val++;
375
376 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
377 offset, val);
378 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000379
Patrice Chotard0493e642013-01-08 10:41:02 +0100380 case ABX500_ALT_A:
381 /*
382 * for ABx5xx family, alt_a mode is always selected by
383 * writing 1 to GPIOSELx register, except for pins which
384 * support at least ALT_B mode, alt_a mode is selected
385 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
386 * register
387 */
388 if (af.alt_bit1 != UNUSED) {
389 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
390 offset, 0);
391 ret = abx500_gpio_set_bits(chip,
392 AB8500_GPIO_ALTFUN_REG,
393 af.alt_bit1,
394 !!(af.alta_val && BIT(0)));
395 if (af.alt_bit2 != UNUSED)
396 ret = abx500_gpio_set_bits(chip,
397 AB8500_GPIO_ALTFUN_REG,
398 af.alt_bit2,
399 !!(af.alta_val && BIT(1)));
400 } else
401 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
402 offset, 1);
403 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000404
Patrice Chotard0493e642013-01-08 10:41:02 +0100405 case ABX500_ALT_B:
406 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
407 offset, 0);
408 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
409 af.alt_bit1, !!(af.altb_val && BIT(0)));
410 if (af.alt_bit2 != UNUSED)
411 ret = abx500_gpio_set_bits(chip,
412 AB8500_GPIO_ALTFUN_REG,
413 af.alt_bit2,
414 !!(af.altb_val && BIT(1)));
415 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000416
Patrice Chotard0493e642013-01-08 10:41:02 +0100417 case ABX500_ALT_C:
418 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
419 offset, 0);
420 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
421 af.alt_bit2, !!(af.altc_val && BIT(0)));
422 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
423 af.alt_bit2, !!(af.altc_val && BIT(1)));
424 break;
425
426 default:
427 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000428
Patrice Chotard0493e642013-01-08 10:41:02 +0100429 return -EINVAL;
430 }
Lee Jones83b423c2013-01-23 13:24:08 +0000431
Patrice Chotard0493e642013-01-08 10:41:02 +0100432 return ret;
433}
434
435static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000436 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100437{
438 u8 mode;
439 bool bit_mode;
440 bool alt_bit1;
441 bool alt_bit2;
442 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
443 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100444 /* on ABx5xx, there is no GPIO0, so adjust the offset */
445 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100446
447 /*
448 * if gpiosel_bit is set to unused,
449 * it means no GPIO or special case
450 */
451 if (af.gpiosel_bit == UNUSED)
452 return ABX500_DEFAULT;
453
454 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100455 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100456 af.gpiosel_bit, &bit_mode);
457 mode = bit_mode;
458
459 /* sanity check */
460 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
461 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
462 dev_err(pct->dev,
463 "alt_bitX value not in correct range (-1 to 7)\n");
464 return -EINVAL;
465 }
Lee Jones83b423c2013-01-23 13:24:08 +0000466
Patrice Chotard0493e642013-01-08 10:41:02 +0100467 /* if alt_bit2 is used, alt_bit1 must be used too */
468 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
469 dev_err(pct->dev,
470 "if alt_bit2 is used, alt_bit1 can't be unused\n");
471 return -EINVAL;
472 }
473
474 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800475 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100476 return mode;
477 /*
478 * if pin GPIOSEL bit is set and pin supports alternate function,
479 * it means DEFAULT mode
480 */
481 if (mode)
482 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000483
Patrice Chotard0493e642013-01-08 10:41:02 +0100484 /*
485 * pin use the AlternatFunction register
486 * read alt_bit1 value
487 */
488 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
489 af.alt_bit1, &alt_bit1);
490
491 if (af.alt_bit2 != UNUSED)
492 /* read alt_bit2 value */
493 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
494 &alt_bit2);
495 else
496 alt_bit2 = 0;
497
498 mode = (alt_bit2 << 1) + alt_bit1;
499 if (mode == af.alta_val)
500 return ABX500_ALT_A;
501 else if (mode == af.altb_val)
502 return ABX500_ALT_B;
503 else
504 return ABX500_ALT_C;
505}
506
507#ifdef CONFIG_DEBUG_FS
508
509#include <linux/seq_file.h>
510
511static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000512 struct pinctrl_dev *pctldev,
513 struct gpio_chip *chip,
514 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100515{
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200516 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
517 struct pullud *pullud = pct->soc->pullud;
Patrice Chotard0493e642013-01-08 10:41:02 +0100518 const char *label = gpiochip_is_requested(chip, offset - 1);
519 u8 gpio_offset = offset - 1;
520 int mode = -1;
521 bool is_out;
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200522 bool pd;
Patrice Chotardce06f402013-06-11 10:48:21 +0200523 enum abx500_gpio_pull_updown pud = 0;
Lee Jones83b423c2013-01-23 13:24:08 +0000524
Patrice Chotard0493e642013-01-08 10:41:02 +0100525 const char *modes[] = {
526 [ABX500_DEFAULT] = "default",
527 [ABX500_ALT_A] = "altA",
528 [ABX500_ALT_B] = "altB",
529 [ABX500_ALT_C] = "altC",
530 };
531
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200532 const char *pull_up_down[] = {
533 [ABX500_GPIO_PULL_DOWN] = "pull down",
534 [ABX500_GPIO_PULL_NONE] = "pull none",
535 [ABX500_GPIO_PULL_NONE + 1] = "pull none",
536 [ABX500_GPIO_PULL_UP] = "pull up",
537 };
538
Patrice Chotard0493e642013-01-08 10:41:02 +0100539 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200540
541 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
542 gpio, label ?: "(none)",
543 is_out ? "out" : "in ");
544
545 if (!is_out) {
546 if (pullud &&
547 (offset >= pullud->first_pin) &&
548 (offset <= pullud->last_pin)) {
549 abx500_get_pull_updown(pct, offset, &pud);
550 seq_printf(s, " %-9s", pull_up_down[pud]);
551 } else {
552 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
553 gpio_offset, &pd);
554 seq_printf(s, " %-9s", pull_up_down[pd]);
555 }
556 } else
557 seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
Patrice Chotard0493e642013-01-08 10:41:02 +0100558
559 if (pctldev)
560 mode = abx500_get_mode(pctldev, chip, offset);
561
Patrice Chotardd2752ae2013-05-24 14:06:31 +0200562 seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100563}
564
565static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
566{
567 unsigned i;
568 unsigned gpio = chip->base;
569 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
570 struct pinctrl_dev *pctldev = pct->pctldev;
571
572 for (i = 0; i < chip->ngpio; i++, gpio++) {
573 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
574 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
575 seq_printf(s, "\n");
576 }
577}
578
579#else
580static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000581 struct pinctrl_dev *pctldev,
582 struct gpio_chip *chip,
583 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100584{
585}
586#define abx500_gpio_dbg_show NULL
587#endif
588
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530589static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100590{
591 int gpio = chip->base + offset;
592
593 return pinctrl_request_gpio(gpio);
594}
595
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530596static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100597{
598 int gpio = chip->base + offset;
599
600 pinctrl_free_gpio(gpio);
601}
602
603static struct gpio_chip abx500gpio_chip = {
604 .label = "abx500-gpio",
605 .owner = THIS_MODULE,
606 .request = abx500_gpio_request,
607 .free = abx500_gpio_free,
608 .direction_input = abx500_gpio_direction_input,
609 .get = abx500_gpio_get,
610 .direction_output = abx500_gpio_direction_output,
611 .set = abx500_gpio_set,
612 .to_irq = abx500_gpio_to_irq,
613 .dbg_show = abx500_gpio_dbg_show,
614};
615
Patrice Chotard0493e642013-01-08 10:41:02 +0100616static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
617{
618 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
619
620 return pct->soc->nfunctions;
621}
622
623static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
624 unsigned function)
625{
626 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
627
628 return pct->soc->functions[function].name;
629}
630
631static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000632 unsigned function,
633 const char * const **groups,
634 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100635{
636 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
637
638 *groups = pct->soc->functions[function].groups;
639 *num_groups = pct->soc->functions[function].ngroups;
640
641 return 0;
642}
643
Patrice Chotard0493e642013-01-08 10:41:02 +0100644static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000645 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100646{
647 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
648 struct gpio_chip *chip = &pct->chip;
649 const struct abx500_pingroup *g;
650 int i;
651 int ret = 0;
652
653 g = &pct->soc->groups[group];
654 if (g->altsetting < 0)
655 return -EINVAL;
656
657 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
658
659 for (i = 0; i < g->npins; i++) {
660 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
661 g->pins[i], g->altsetting);
662
Patrice Chotard0493e642013-01-08 10:41:02 +0100663 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
664 }
Lee Jones83b423c2013-01-23 13:24:08 +0000665
Patrice Chotard0493e642013-01-08 10:41:02 +0100666 return ret;
667}
668
669static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000670 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100671{
672 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
673 const struct abx500_pingroup *g;
674
675 g = &pct->soc->groups[group];
676 if (g->altsetting < 0)
677 return;
678
679 /* FIXME: poke out the mux, set the pin to some default state? */
680 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
681}
682
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530683static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000684 struct pinctrl_gpio_range *range,
685 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100686{
687 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
688 const struct abx500_pinrange *p;
689 int ret;
690 int i;
691
692 /*
693 * Different ranges have different ways to enable GPIO function on a
694 * pin, so refer back to our local range type, where we handily define
695 * what altfunc enables GPIO for a certain pin.
696 */
697 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
698 p = &pct->soc->gpio_ranges[i];
699 if ((offset >= p->offset) &&
700 (offset < (p->offset + p->npins)))
701 break;
702 }
703
704 if (i == pct->soc->gpio_num_ranges) {
705 dev_err(pct->dev, "%s failed to locate range\n", __func__);
706 return -ENODEV;
707 }
708
709 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
710 p->altfunc, offset);
711
712 ret = abx500_set_mode(pct->pctldev, &pct->chip,
713 offset, p->altfunc);
714 if (ret < 0) {
715 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
716 return ret;
717 }
718
719 return ret;
720}
721
722static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000723 struct pinctrl_gpio_range *range,
724 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100725{
726}
727
Laurent Pinchart022ab142013-02-16 10:25:07 +0100728static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100729 .get_functions_count = abx500_pmx_get_funcs_cnt,
730 .get_function_name = abx500_pmx_get_func_name,
731 .get_function_groups = abx500_pmx_get_func_groups,
732 .enable = abx500_pmx_enable,
733 .disable = abx500_pmx_disable,
734 .gpio_request_enable = abx500_gpio_request_enable,
735 .gpio_disable_free = abx500_gpio_disable_free,
736};
737
738static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
739{
740 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
741
742 return pct->soc->ngroups;
743}
744
745static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000746 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100747{
748 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
749
750 return pct->soc->groups[selector].name;
751}
752
753static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000754 unsigned selector,
755 const unsigned **pins,
756 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100757{
758 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
759
760 *pins = pct->soc->groups[selector].pins;
761 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000762
Patrice Chotard0493e642013-01-08 10:41:02 +0100763 return 0;
764}
765
766static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000767 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100768{
769 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
770 struct gpio_chip *chip = &pct->chip;
771
772 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
773 chip->base + offset - 1);
774}
775
Patrice Chotard64a45c92013-06-20 16:04:59 +0200776static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
777 struct pinctrl_map *map, unsigned num_maps)
778{
779 int i;
780
781 for (i = 0; i < num_maps; i++)
782 if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
783 kfree(map[i].data.configs.configs);
784 kfree(map);
785}
786
787static int abx500_dt_reserve_map(struct pinctrl_map **map,
788 unsigned *reserved_maps,
789 unsigned *num_maps,
790 unsigned reserve)
791{
792 unsigned old_num = *reserved_maps;
793 unsigned new_num = *num_maps + reserve;
794 struct pinctrl_map *new_map;
795
796 if (old_num >= new_num)
797 return 0;
798
799 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
800 if (!new_map)
801 return -ENOMEM;
802
803 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
804
805 *map = new_map;
806 *reserved_maps = new_num;
807
808 return 0;
809}
810
811static int abx500_dt_add_map_mux(struct pinctrl_map **map,
812 unsigned *reserved_maps,
813 unsigned *num_maps, const char *group,
814 const char *function)
815{
816 if (*num_maps == *reserved_maps)
817 return -ENOSPC;
818
819 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
820 (*map)[*num_maps].data.mux.group = group;
821 (*map)[*num_maps].data.mux.function = function;
822 (*num_maps)++;
823
824 return 0;
825}
826
827static int abx500_dt_add_map_configs(struct pinctrl_map **map,
828 unsigned *reserved_maps,
829 unsigned *num_maps, const char *group,
830 unsigned long *configs, unsigned num_configs)
831{
832 unsigned long *dup_configs;
833
834 if (*num_maps == *reserved_maps)
835 return -ENOSPC;
836
837 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
838 GFP_KERNEL);
839 if (!dup_configs)
840 return -ENOMEM;
841
842 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
843
844 (*map)[*num_maps].data.configs.group_or_pin = group;
845 (*map)[*num_maps].data.configs.configs = dup_configs;
846 (*map)[*num_maps].data.configs.num_configs = num_configs;
847 (*num_maps)++;
848
849 return 0;
850}
851
852static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
853 const char *pin_name)
854{
855 int i, pin_number;
856 struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
857
858 if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
859 for (i = 0; i < npct->soc->npins; i++)
860 if (npct->soc->pins[i].number == pin_number)
861 return npct->soc->pins[i].name;
862 return NULL;
863}
864
865static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
866 struct device_node *np,
867 struct pinctrl_map **map,
868 unsigned *reserved_maps,
869 unsigned *num_maps)
870{
871 int ret;
872 const char *function = NULL;
873 unsigned long *configs;
874 unsigned int nconfigs = 0;
875 bool has_config = 0;
876 unsigned reserve = 0;
877 struct property *prop;
878 const char *group, *gpio_name;
879 struct device_node *np_config;
880
881 ret = of_property_read_string(np, "ste,function", &function);
882 if (ret >= 0)
883 reserve = 1;
884
885 ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
886 if (nconfigs)
887 has_config = 1;
888
889 np_config = of_parse_phandle(np, "ste,config", 0);
890 if (np_config) {
891 ret = pinconf_generic_parse_dt_config(np_config, &configs,
892 &nconfigs);
893 if (ret)
894 goto exit;
895 has_config |= nconfigs;
896 }
897
898 ret = of_property_count_strings(np, "ste,pins");
899 if (ret < 0)
900 goto exit;
901
902 if (has_config)
903 reserve++;
904
905 reserve *= ret;
906
907 ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
908 if (ret < 0)
909 goto exit;
910
911 of_property_for_each_string(np, "ste,pins", prop, group) {
912 if (function) {
913 ret = abx500_dt_add_map_mux(map, reserved_maps,
914 num_maps, group, function);
915 if (ret < 0)
916 goto exit;
917 }
918 if (has_config) {
919 gpio_name = abx500_find_pin_name(pctldev, group);
920
921 ret = abx500_dt_add_map_configs(map, reserved_maps,
922 num_maps, gpio_name, configs, 1);
923 if (ret < 0)
924 goto exit;
925 }
926
927 }
928exit:
929 return ret;
930}
931
932static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
933 struct device_node *np_config,
934 struct pinctrl_map **map, unsigned *num_maps)
935{
936 unsigned reserved_maps;
937 struct device_node *np;
938 int ret;
939
940 reserved_maps = 0;
941 *map = NULL;
942 *num_maps = 0;
943
944 for_each_child_of_node(np_config, np) {
945 ret = abx500_dt_subnode_to_map(pctldev, np, map,
946 &reserved_maps, num_maps);
947 if (ret < 0) {
948 abx500_dt_free_map(pctldev, *map, *num_maps);
949 return ret;
950 }
951 }
952
953 return 0;
954}
955
Laurent Pinchart022ab142013-02-16 10:25:07 +0100956static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100957 .get_groups_count = abx500_get_groups_cnt,
958 .get_group_name = abx500_get_group_name,
959 .get_group_pins = abx500_get_group_pins,
960 .pin_dbg_show = abx500_pin_dbg_show,
Patrice Chotard64a45c92013-06-20 16:04:59 +0200961 .dt_node_to_map = abx500_dt_node_to_map,
962 .dt_free_map = abx500_dt_free_map,
Patrice Chotard0493e642013-01-08 10:41:02 +0100963};
964
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530965static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000966 unsigned pin,
967 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100968{
Lee Jones1abeebe2012-12-20 11:11:19 +0000969 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100970}
971
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530972static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000973 unsigned pin,
974 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100975{
976 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
977 struct pullud *pullud = pct->soc->pullud;
978 struct gpio_chip *chip = &pct->chip;
979 unsigned offset;
Patrice Chotard61ce1352013-06-20 16:05:00 +0200980 int ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100981 enum pin_config_param param = pinconf_to_config_param(config);
982 enum pin_config_param argument = pinconf_to_config_argument(config);
983
984 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
985 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
986 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
987 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000988
Patrice Chotard0493e642013-01-08 10:41:02 +0100989 /* on ABx500, there is no GPIO0, so adjust the offset */
990 offset = pin - 1;
991
992 switch (param) {
Patrice Chotard61ce1352013-06-20 16:05:00 +0200993 case PIN_CONFIG_BIAS_DISABLE:
994 ret = abx500_gpio_direction_input(chip, offset);
995 /*
996 * Some chips only support pull down, while some actually
997 * support both pull up and pull down. Such chips have
998 * a "pullud" range specified for the pins that support
999 * both features. If the pin is not within that range, we
1000 * fall back to the old bit set that only support pull down.
1001 */
1002 if (pullud &&
1003 pin >= pullud->first_pin &&
1004 pin <= pullud->last_pin)
1005 ret = abx500_set_pull_updown(pct,
1006 pin,
1007 ABX500_GPIO_PULL_NONE);
1008 else
1009 /* Chip only supports pull down */
1010 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1011 offset, ABX500_GPIO_PULL_NONE);
1012 break;
1013
Patrice Chotard0493e642013-01-08 10:41:02 +01001014 case PIN_CONFIG_BIAS_PULL_DOWN:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001015 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard0493e642013-01-08 10:41:02 +01001016 /*
1017 * if argument = 1 set the pull down
1018 * else clear the pull down
Patrice Chotard0493e642013-01-08 10:41:02 +01001019 * Some chips only support pull down, while some actually
1020 * support both pull up and pull down. Such chips have
1021 * a "pullud" range specified for the pins that support
1022 * both features. If the pin is not within that range, we
1023 * fall back to the old bit set that only support pull down.
1024 */
1025 if (pullud &&
1026 pin >= pullud->first_pin &&
1027 pin <= pullud->last_pin)
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001028 ret = abx500_set_pull_updown(pct,
Patrice Chotard0493e642013-01-08 10:41:02 +01001029 pin,
1030 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
1031 else
1032 /* Chip only supports pull down */
1033 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
1034 offset, argument ? 0 : 1);
1035 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001036
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001037 case PIN_CONFIG_BIAS_PULL_UP:
Patrice Chotard61ce1352013-06-20 16:05:00 +02001038 ret = abx500_gpio_direction_input(chip, offset);
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001039 /*
1040 * if argument = 1 set the pull up
1041 * else clear the pull up
1042 */
1043 ret = abx500_gpio_direction_input(chip, offset);
1044 /*
1045 * Some chips only support pull down, while some actually
1046 * support both pull up and pull down. Such chips have
1047 * a "pullud" range specified for the pins that support
1048 * both features. If the pin is not within that range, do
1049 * nothing
1050 */
1051 if (pullud &&
1052 pin >= pullud->first_pin &&
1053 pin <= pullud->last_pin) {
Patrice Chotardd2752ae2013-05-24 14:06:31 +02001054 ret = abx500_set_pull_updown(pct,
Patrice Chotard9ed3cd32013-05-24 14:06:30 +02001055 pin,
1056 argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
1057 }
1058 break;
1059
Patrice Chotard0493e642013-01-08 10:41:02 +01001060 case PIN_CONFIG_OUTPUT:
1061 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +00001062
Patrice Chotard0493e642013-01-08 10:41:02 +01001063 break;
Lee Jones83b423c2013-01-23 13:24:08 +00001064
Patrice Chotard0493e642013-01-08 10:41:02 +01001065 default:
1066 dev_err(chip->dev, "illegal configuration requested\n");
Patrice Chotard0493e642013-01-08 10:41:02 +01001067 }
Lee Jones83b423c2013-01-23 13:24:08 +00001068
Patrice Chotard0493e642013-01-08 10:41:02 +01001069 return ret;
1070}
1071
Laurent Pinchart022ab142013-02-16 10:25:07 +01001072static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +01001073 .pin_config_get = abx500_pin_config_get,
1074 .pin_config_set = abx500_pin_config_set,
1075};
1076
1077static struct pinctrl_desc abx500_pinctrl_desc = {
1078 .name = "pinctrl-abx500",
1079 .pctlops = &abx500_pinctrl_ops,
1080 .pmxops = &abx500_pinmux_ops,
1081 .confops = &abx500_pinconf_ops,
1082 .owner = THIS_MODULE,
1083};
1084
1085static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
1086{
1087 unsigned int lowest = 0;
1088 unsigned int highest = 0;
1089 unsigned int npins = 0;
1090 int i;
1091
1092 /*
1093 * Compute number of GPIOs from the last SoC gpio range descriptors
1094 * These ranges may include "holes" but the GPIO number space shall
1095 * still be homogeneous, so we need to detect and account for any
1096 * such holes so that these are included in the number of GPIO pins.
1097 */
1098 for (i = 0; i < soc->gpio_num_ranges; i++) {
1099 unsigned gstart;
1100 unsigned gend;
1101 const struct abx500_pinrange *p;
1102
1103 p = &soc->gpio_ranges[i];
1104 gstart = p->offset;
1105 gend = p->offset + p->npins - 1;
1106
1107 if (i == 0) {
1108 /* First iteration, set start values */
1109 lowest = gstart;
1110 highest = gend;
1111 } else {
1112 if (gstart < lowest)
1113 lowest = gstart;
1114 if (gend > highest)
1115 highest = gend;
1116 }
1117 }
1118 /* this gives the absolute number of pins */
1119 npins = highest - lowest + 1;
1120 return npins;
1121}
1122
Lee Jonesf30a3832013-01-31 11:07:40 +00001123static const struct of_device_id abx500_gpio_match[] = {
1124 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
1125 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
1126 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
1127 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +08001128 { }
Lee Jonesf30a3832013-01-31 11:07:40 +00001129};
1130
Patrice Chotard0493e642013-01-08 10:41:02 +01001131static int abx500_gpio_probe(struct platform_device *pdev)
1132{
1133 struct ab8500_platform_data *abx500_pdata =
1134 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +00001135 struct abx500_gpio_platform_data *pdata = NULL;
1136 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +01001137 struct abx500_pinctrl *pct;
1138 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +00001139 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +00001140 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +01001141 int i;
1142
Lee Jonesf30a3832013-01-31 11:07:40 +00001143 if (abx500_pdata)
1144 pdata = abx500_pdata->gpio;
Lee Jonesf30a3832013-01-31 11:07:40 +00001145
Lee Jones86c976e2013-05-08 14:29:08 +01001146 if (!(pdata || np)) {
1147 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
1148 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +01001149 }
1150
1151 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
1152 GFP_KERNEL);
1153 if (pct == NULL) {
1154 dev_err(&pdev->dev,
1155 "failed to allocate memory for pct\n");
1156 return -ENOMEM;
1157 }
1158
1159 pct->dev = &pdev->dev;
1160 pct->parent = dev_get_drvdata(pdev->dev.parent);
1161 pct->chip = abx500gpio_chip;
1162 pct->chip.dev = &pdev->dev;
Lee Jonesf30a3832013-01-31 11:07:40 +00001163 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +01001164
Lee Jones86c976e2013-05-08 14:29:08 +01001165 if (platid)
1166 id = platid->driver_data;
1167 else if (np) {
1168 const struct of_device_id *match;
1169
1170 match = of_match_device(abx500_gpio_match, &pdev->dev);
1171 if (match)
1172 id = (unsigned long)match->data;
1173 }
1174
Patrice Chotard0493e642013-01-08 10:41:02 +01001175 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +00001176 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +01001177 case PINCTRL_AB8500:
1178 abx500_pinctrl_ab8500_init(&pct->soc);
1179 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +01001180 case PINCTRL_AB8540:
1181 abx500_pinctrl_ab8540_init(&pct->soc);
1182 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +01001183 case PINCTRL_AB9540:
1184 abx500_pinctrl_ab9540_init(&pct->soc);
1185 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +01001186 case PINCTRL_AB8505:
1187 abx500_pinctrl_ab8505_init(&pct->soc);
1188 break;
Patrice Chotard0493e642013-01-08 10:41:02 +01001189 default:
Lee Jones2fcad122013-05-08 14:29:07 +01001190 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +01001191 return -EINVAL;
1192 }
1193
1194 if (!pct->soc) {
1195 dev_err(&pdev->dev, "Invalid SOC data\n");
1196 return -EINVAL;
1197 }
1198
1199 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
1200 pct->irq_cluster = pct->soc->gpio_irq_cluster;
1201 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +01001202
Patrice Chotard0493e642013-01-08 10:41:02 +01001203 ret = gpiochip_add(&pct->chip);
1204 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +00001205 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +00001206 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +01001207 }
1208 dev_info(&pdev->dev, "added gpiochip\n");
1209
1210 abx500_pinctrl_desc.pins = pct->soc->pins;
1211 abx500_pinctrl_desc.npins = pct->soc->npins;
1212 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
1213 if (!pct->pctldev) {
1214 dev_err(&pdev->dev,
1215 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +00001216 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +01001217 goto out_rem_chip;
1218 }
1219 dev_info(&pdev->dev, "registered pin controller\n");
1220
1221 /* We will handle a range of GPIO pins */
1222 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
1223 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
1224
1225 ret = gpiochip_add_pin_range(&pct->chip,
1226 dev_name(&pdev->dev),
1227 p->offset - 1, p->offset, p->npins);
1228 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +00001229 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +01001230 }
1231
1232 platform_set_drvdata(pdev, pct);
1233 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
1234
1235 return 0;
1236
1237out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +00001238 err = gpiochip_remove(&pct->chip);
1239 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +01001240 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +00001241
Patrice Chotard0493e642013-01-08 10:41:02 +01001242 return ret;
1243}
1244
Lee Jones83b423c2013-01-23 13:24:08 +00001245/**
Patrice Chotard0493e642013-01-08 10:41:02 +01001246 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +00001247 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +01001248 */
1249static int abx500_gpio_remove(struct platform_device *pdev)
1250{
1251 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
1252 int ret;
1253
1254 ret = gpiochip_remove(&pct->chip);
1255 if (ret < 0) {
1256 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
1257 ret);
1258 return ret;
1259 }
1260
Patrice Chotard0493e642013-01-08 10:41:02 +01001261 return 0;
1262}
1263
1264static const struct platform_device_id abx500_pinctrl_id[] = {
1265 { "pinctrl-ab8500", PINCTRL_AB8500 },
1266 { "pinctrl-ab8540", PINCTRL_AB8540 },
1267 { "pinctrl-ab9540", PINCTRL_AB9540 },
1268 { "pinctrl-ab8505", PINCTRL_AB8505 },
1269 { },
1270};
1271
1272static struct platform_driver abx500_gpio_driver = {
1273 .driver = {
1274 .name = "abx500-gpio",
1275 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001276 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001277 },
1278 .probe = abx500_gpio_probe,
1279 .remove = abx500_gpio_remove,
1280 .id_table = abx500_pinctrl_id,
1281};
1282
1283static int __init abx500_gpio_init(void)
1284{
1285 return platform_driver_register(&abx500_gpio_driver);
1286}
1287core_initcall(abx500_gpio_init);
1288
1289MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1290MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1291MODULE_ALIAS("platform:abx500-gpio");
1292MODULE_LICENSE("GPL v2");