blob: 363712bad7efd3019a0e9093f00bc75c59c82dfc [file] [log] [blame]
Patrice Chotard0493e642013-01-08 10:41:02 +01001/*
2 * Copyright (C) ST-Ericsson SA 2013
3 *
4 * Author: Patrice Chotard <patrice.chotard@st.com>
5 * License terms: GNU General Public License (GPL) version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/err.h>
Lee Jonesf30a3832013-01-31 11:07:40 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010019#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/irq.h>
Lee Jonesac652d72013-01-31 10:43:00 +000022#include <linux/irqdomain.h>
Patrice Chotard0493e642013-01-08 10:41:02 +010023#include <linux/interrupt.h>
24#include <linux/bitops.h>
25#include <linux/mfd/abx500.h>
26#include <linux/mfd/abx500/ab8500.h>
27#include <linux/mfd/abx500/ab8500-gpio.h>
28#include <linux/pinctrl/pinctrl.h>
29#include <linux/pinctrl/consumer.h>
30#include <linux/pinctrl/pinmux.h>
31#include <linux/pinctrl/pinconf.h>
32#include <linux/pinctrl/pinconf-generic.h>
33
34#include "pinctrl-abx500.h"
35
36/*
37 * The AB9540 and AB8540 GPIO support are extended versions
38 * of the AB8500 GPIO support.
39 * The AB9540 supports an additional (7th) register so that
40 * more GPIO may be configured and used.
41 * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
42 * internal pull-up and pull-down capabilities.
43 */
44
45/*
46 * GPIO registers offset
47 * Bank: 0x10
48 */
49#define AB8500_GPIO_SEL1_REG 0x00
50#define AB8500_GPIO_SEL2_REG 0x01
51#define AB8500_GPIO_SEL3_REG 0x02
52#define AB8500_GPIO_SEL4_REG 0x03
53#define AB8500_GPIO_SEL5_REG 0x04
54#define AB8500_GPIO_SEL6_REG 0x05
55#define AB9540_GPIO_SEL7_REG 0x06
56
57#define AB8500_GPIO_DIR1_REG 0x10
58#define AB8500_GPIO_DIR2_REG 0x11
59#define AB8500_GPIO_DIR3_REG 0x12
60#define AB8500_GPIO_DIR4_REG 0x13
61#define AB8500_GPIO_DIR5_REG 0x14
62#define AB8500_GPIO_DIR6_REG 0x15
63#define AB9540_GPIO_DIR7_REG 0x16
64
65#define AB8500_GPIO_OUT1_REG 0x20
66#define AB8500_GPIO_OUT2_REG 0x21
67#define AB8500_GPIO_OUT3_REG 0x22
68#define AB8500_GPIO_OUT4_REG 0x23
69#define AB8500_GPIO_OUT5_REG 0x24
70#define AB8500_GPIO_OUT6_REG 0x25
71#define AB9540_GPIO_OUT7_REG 0x26
72
73#define AB8500_GPIO_PUD1_REG 0x30
74#define AB8500_GPIO_PUD2_REG 0x31
75#define AB8500_GPIO_PUD3_REG 0x32
76#define AB8500_GPIO_PUD4_REG 0x33
77#define AB8500_GPIO_PUD5_REG 0x34
78#define AB8500_GPIO_PUD6_REG 0x35
79#define AB9540_GPIO_PUD7_REG 0x36
80
81#define AB8500_GPIO_IN1_REG 0x40
82#define AB8500_GPIO_IN2_REG 0x41
83#define AB8500_GPIO_IN3_REG 0x42
84#define AB8500_GPIO_IN4_REG 0x43
85#define AB8500_GPIO_IN5_REG 0x44
86#define AB8500_GPIO_IN6_REG 0x45
87#define AB9540_GPIO_IN7_REG 0x46
88#define AB8540_GPIO_VINSEL_REG 0x47
89#define AB8540_GPIO_PULL_UPDOWN_REG 0x48
90#define AB8500_GPIO_ALTFUN_REG 0x50
Patrice Chotard0493e642013-01-08 10:41:02 +010091#define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
92#define AB8540_GPIO_VINSEL_MASK 0x03
93#define AB8540_GPIOX_VBAT_START 51
94#define AB8540_GPIOX_VBAT_END 54
95
Patrice Chotard0493e642013-01-08 10:41:02 +010096struct abx500_pinctrl {
97 struct device *dev;
98 struct pinctrl_dev *pctldev;
99 struct abx500_pinctrl_soc_data *soc;
100 struct gpio_chip chip;
101 struct ab8500 *parent;
Patrice Chotard0493e642013-01-08 10:41:02 +0100102 struct abx500_gpio_irq_cluster *irq_cluster;
103 int irq_cluster_size;
Patrice Chotard0493e642013-01-08 10:41:02 +0100104};
105
106/**
107 * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
108 * @chip: Member of the structure abx500_pinctrl
109 */
110static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
111{
112 return container_of(chip, struct abx500_pinctrl, chip);
113}
114
115static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000116 unsigned offset, bool *bit)
Patrice Chotard0493e642013-01-08 10:41:02 +0100117{
118 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
119 u8 pos = offset % 8;
120 u8 val;
121 int ret;
122
123 reg += offset / 8;
124 ret = abx500_get_register_interruptible(pct->dev,
125 AB8500_MISC, reg, &val);
126
127 *bit = !!(val & BIT(pos));
128
129 if (ret < 0)
130 dev_err(pct->dev,
131 "%s read reg =%x, offset=%x failed\n",
132 __func__, reg, offset);
133
134 return ret;
135}
136
137static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
Lee Jones83b423c2013-01-23 13:24:08 +0000138 unsigned offset, int val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100139{
140 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
141 u8 pos = offset % 8;
142 int ret;
143
144 reg += offset / 8;
145 ret = abx500_mask_and_set_register_interruptible(pct->dev,
Lee Jones49dcf082013-01-23 13:26:02 +0000146 AB8500_MISC, reg, BIT(pos), val << pos);
Patrice Chotard0493e642013-01-08 10:41:02 +0100147 if (ret < 0)
148 dev_err(pct->dev, "%s write failed\n", __func__);
Lee Jones83b423c2013-01-23 13:24:08 +0000149
Patrice Chotard0493e642013-01-08 10:41:02 +0100150 return ret;
151}
Lee Jones83b423c2013-01-23 13:24:08 +0000152
Patrice Chotard0493e642013-01-08 10:41:02 +0100153/**
154 * abx500_gpio_get() - Get the particular GPIO value
Lee Jones83b423c2013-01-23 13:24:08 +0000155 * @chip: Gpio device
156 * @offset: GPIO number to read
Patrice Chotard0493e642013-01-08 10:41:02 +0100157 */
158static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
159{
160 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
161 bool bit;
162 int ret;
163
164 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
165 offset, &bit);
166 if (ret < 0) {
167 dev_err(pct->dev, "%s failed\n", __func__);
168 return ret;
169 }
Lee Jones83b423c2013-01-23 13:24:08 +0000170
Patrice Chotard0493e642013-01-08 10:41:02 +0100171 return bit;
172}
173
174static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
175{
176 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
177 int ret;
178
179 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
180 if (ret < 0)
181 dev_err(pct->dev, "%s write failed\n", __func__);
182}
183
184static int abx500_config_pull_updown(struct abx500_pinctrl *pct,
Lee Jones83b423c2013-01-23 13:24:08 +0000185 int offset, enum abx500_gpio_pull_updown val)
Patrice Chotard0493e642013-01-08 10:41:02 +0100186{
187 u8 pos;
188 int ret;
189 struct pullud *pullud;
190
191 if (!pct->soc->pullud) {
192 dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
193 __func__);
194 ret = -EPERM;
195 goto out;
196 }
197
198 pullud = pct->soc->pullud;
199
200 if ((offset < pullud->first_pin)
201 || (offset > pullud->last_pin)) {
202 ret = -EINVAL;
203 goto out;
204 }
205
Patrice Chotard10a8be52013-05-24 14:06:29 +0200206 pos = (offset - pullud->first_pin) << 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100207
208 ret = abx500_mask_and_set_register_interruptible(pct->dev,
209 AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
210 AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
211
212out:
213 if (ret < 0)
214 dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
Lee Jones83b423c2013-01-23 13:24:08 +0000215
Patrice Chotard0493e642013-01-08 10:41:02 +0100216 return ret;
217}
218
219static int abx500_gpio_direction_output(struct gpio_chip *chip,
220 unsigned offset,
221 int val)
222{
223 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
224 struct pullud *pullud = pct->soc->pullud;
225 unsigned gpio;
226 int ret;
Lee Jones83b423c2013-01-23 13:24:08 +0000227
Patrice Chotard0493e642013-01-08 10:41:02 +0100228 /* set direction as output */
229 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
230 if (ret < 0)
231 return ret;
232
233 /* disable pull down */
234 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
235 if (ret < 0)
236 return ret;
237
238 /* if supported, disable both pull down and pull up */
239 gpio = offset + 1;
240 if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
241 ret = abx500_config_pull_updown(pct,
242 gpio,
243 ABX500_GPIO_PULL_NONE);
244 if (ret < 0)
245 return ret;
246 }
Lee Jones83b423c2013-01-23 13:24:08 +0000247
Patrice Chotard0493e642013-01-08 10:41:02 +0100248 /* set the output as 1 or 0 */
249 return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
250}
251
252static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
253{
254 /* set the register as input */
255 return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
256}
257
258static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
259{
260 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
Lee Jonesb9fab6e2013-01-31 09:45:17 +0000261 /* The AB8500 GPIO numbers are off by one */
262 int gpio = offset + 1;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000263 int hwirq;
Patrice Chotard0493e642013-01-08 10:41:02 +0100264 int i;
265
266 for (i = 0; i < pct->irq_cluster_size; i++) {
267 struct abx500_gpio_irq_cluster *cluster =
268 &pct->irq_cluster[i];
269
Lee Jonesa6a16d22013-01-31 09:57:52 +0000270 if (gpio >= cluster->start && gpio <= cluster->end) {
271 /*
272 * The ABx500 GPIO's associated IRQs are clustered together
273 * throughout the interrupt numbers at irregular intervals.
274 * To solve this quandry, we have placed the read-in values
275 * into the cluster information table.
276 */
Linus Walleij43a255d2013-02-04 15:21:41 +0100277 hwirq = gpio - cluster->start + cluster->to_irq;
Lee Jonesa6a16d22013-01-31 09:57:52 +0000278 return irq_create_mapping(pct->parent->domain, hwirq);
279 }
Patrice Chotard0493e642013-01-08 10:41:02 +0100280 }
281
282 return -EINVAL;
283}
284
285static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000286 unsigned gpio, int alt_setting)
Patrice Chotard0493e642013-01-08 10:41:02 +0100287{
288 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
289 struct alternate_functions af = pct->soc->alternate_functions[gpio];
290 int ret;
291 int val;
292 unsigned offset;
Lee Jones83b423c2013-01-23 13:24:08 +0000293
Patrice Chotard0493e642013-01-08 10:41:02 +0100294 const char *modes[] = {
295 [ABX500_DEFAULT] = "default",
296 [ABX500_ALT_A] = "altA",
297 [ABX500_ALT_B] = "altB",
298 [ABX500_ALT_C] = "altC",
299 };
300
301 /* sanity check */
302 if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
303 ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
304 ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
305 dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
306 modes[alt_setting]);
307 return -EINVAL;
308 }
309
310 /* on ABx5xx, there is no GPIO0, so adjust the offset */
311 offset = gpio - 1;
Lee Jones83b423c2013-01-23 13:24:08 +0000312
Patrice Chotard0493e642013-01-08 10:41:02 +0100313 switch (alt_setting) {
314 case ABX500_DEFAULT:
315 /*
316 * for ABx5xx family, default mode is always selected by
317 * writing 0 to GPIOSELx register, except for pins which
318 * support at least ALT_B mode, default mode is selected
319 * by writing 1 to GPIOSELx register
320 */
321 val = 0;
322 if (af.alt_bit1 != UNUSED)
323 val++;
324
325 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
326 offset, val);
327 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000328
Patrice Chotard0493e642013-01-08 10:41:02 +0100329 case ABX500_ALT_A:
330 /*
331 * for ABx5xx family, alt_a mode is always selected by
332 * writing 1 to GPIOSELx register, except for pins which
333 * support at least ALT_B mode, alt_a mode is selected
334 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
335 * register
336 */
337 if (af.alt_bit1 != UNUSED) {
338 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
339 offset, 0);
340 ret = abx500_gpio_set_bits(chip,
341 AB8500_GPIO_ALTFUN_REG,
342 af.alt_bit1,
343 !!(af.alta_val && BIT(0)));
344 if (af.alt_bit2 != UNUSED)
345 ret = abx500_gpio_set_bits(chip,
346 AB8500_GPIO_ALTFUN_REG,
347 af.alt_bit2,
348 !!(af.alta_val && BIT(1)));
349 } else
350 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
351 offset, 1);
352 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000353
Patrice Chotard0493e642013-01-08 10:41:02 +0100354 case ABX500_ALT_B:
355 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
356 offset, 0);
357 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
358 af.alt_bit1, !!(af.altb_val && BIT(0)));
359 if (af.alt_bit2 != UNUSED)
360 ret = abx500_gpio_set_bits(chip,
361 AB8500_GPIO_ALTFUN_REG,
362 af.alt_bit2,
363 !!(af.altb_val && BIT(1)));
364 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000365
Patrice Chotard0493e642013-01-08 10:41:02 +0100366 case ABX500_ALT_C:
367 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
368 offset, 0);
369 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
370 af.alt_bit2, !!(af.altc_val && BIT(0)));
371 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
372 af.alt_bit2, !!(af.altc_val && BIT(1)));
373 break;
374
375 default:
376 dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
Lee Jones83b423c2013-01-23 13:24:08 +0000377
Patrice Chotard0493e642013-01-08 10:41:02 +0100378 return -EINVAL;
379 }
Lee Jones83b423c2013-01-23 13:24:08 +0000380
Patrice Chotard0493e642013-01-08 10:41:02 +0100381 return ret;
382}
383
384static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
Lee Jones83b423c2013-01-23 13:24:08 +0000385 unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100386{
387 u8 mode;
388 bool bit_mode;
389 bool alt_bit1;
390 bool alt_bit2;
391 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
392 struct alternate_functions af = pct->soc->alternate_functions[gpio];
Linus Walleija950cb72013-02-05 20:10:57 +0100393 /* on ABx5xx, there is no GPIO0, so adjust the offset */
394 unsigned offset = gpio - 1;
Patrice Chotard0493e642013-01-08 10:41:02 +0100395
396 /*
397 * if gpiosel_bit is set to unused,
398 * it means no GPIO or special case
399 */
400 if (af.gpiosel_bit == UNUSED)
401 return ABX500_DEFAULT;
402
403 /* read GpioSelx register */
Linus Walleija950cb72013-02-05 20:10:57 +0100404 abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
Patrice Chotard0493e642013-01-08 10:41:02 +0100405 af.gpiosel_bit, &bit_mode);
406 mode = bit_mode;
407
408 /* sanity check */
409 if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
410 (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
411 dev_err(pct->dev,
412 "alt_bitX value not in correct range (-1 to 7)\n");
413 return -EINVAL;
414 }
Lee Jones83b423c2013-01-23 13:24:08 +0000415
Patrice Chotard0493e642013-01-08 10:41:02 +0100416 /* if alt_bit2 is used, alt_bit1 must be used too */
417 if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
418 dev_err(pct->dev,
419 "if alt_bit2 is used, alt_bit1 can't be unused\n");
420 return -EINVAL;
421 }
422
423 /* check if pin use AlternateFunction register */
Axel Lin6a40cdd2013-03-05 14:58:53 +0800424 if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
Patrice Chotard0493e642013-01-08 10:41:02 +0100425 return mode;
426 /*
427 * if pin GPIOSEL bit is set and pin supports alternate function,
428 * it means DEFAULT mode
429 */
430 if (mode)
431 return ABX500_DEFAULT;
Lee Jones83b423c2013-01-23 13:24:08 +0000432
Patrice Chotard0493e642013-01-08 10:41:02 +0100433 /*
434 * pin use the AlternatFunction register
435 * read alt_bit1 value
436 */
437 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
438 af.alt_bit1, &alt_bit1);
439
440 if (af.alt_bit2 != UNUSED)
441 /* read alt_bit2 value */
442 abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
443 &alt_bit2);
444 else
445 alt_bit2 = 0;
446
447 mode = (alt_bit2 << 1) + alt_bit1;
448 if (mode == af.alta_val)
449 return ABX500_ALT_A;
450 else if (mode == af.altb_val)
451 return ABX500_ALT_B;
452 else
453 return ABX500_ALT_C;
454}
455
456#ifdef CONFIG_DEBUG_FS
457
458#include <linux/seq_file.h>
459
460static void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000461 struct pinctrl_dev *pctldev,
462 struct gpio_chip *chip,
463 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100464{
Patrice Chotard0493e642013-01-08 10:41:02 +0100465 const char *label = gpiochip_is_requested(chip, offset - 1);
466 u8 gpio_offset = offset - 1;
467 int mode = -1;
468 bool is_out;
469 bool pull;
Lee Jones83b423c2013-01-23 13:24:08 +0000470
Patrice Chotard0493e642013-01-08 10:41:02 +0100471 const char *modes[] = {
472 [ABX500_DEFAULT] = "default",
473 [ABX500_ALT_A] = "altA",
474 [ABX500_ALT_B] = "altB",
475 [ABX500_ALT_C] = "altC",
476 };
477
478 abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
479 abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, gpio_offset, &pull);
480
481 if (pctldev)
482 mode = abx500_get_mode(pctldev, chip, offset);
483
484 seq_printf(s, " gpio-%-3d (%-20.20s) %-3s %-9s %s",
485 gpio, label ?: "(none)",
486 is_out ? "out" : "in ",
487 is_out ?
488 (chip->get
489 ? (chip->get(chip, offset) ? "hi" : "lo")
490 : "? ")
491 : (pull ? "pull up" : "pull down"),
492 (mode < 0) ? "unknown" : modes[mode]);
Patrice Chotard0493e642013-01-08 10:41:02 +0100493}
494
495static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
496{
497 unsigned i;
498 unsigned gpio = chip->base;
499 struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
500 struct pinctrl_dev *pctldev = pct->pctldev;
501
502 for (i = 0; i < chip->ngpio; i++, gpio++) {
503 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
504 abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
505 seq_printf(s, "\n");
506 }
507}
508
509#else
510static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
Lee Jones83b423c2013-01-23 13:24:08 +0000511 struct pinctrl_dev *pctldev,
512 struct gpio_chip *chip,
513 unsigned offset, unsigned gpio)
Patrice Chotard0493e642013-01-08 10:41:02 +0100514{
515}
516#define abx500_gpio_dbg_show NULL
517#endif
518
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530519static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100520{
521 int gpio = chip->base + offset;
522
523 return pinctrl_request_gpio(gpio);
524}
525
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530526static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100527{
528 int gpio = chip->base + offset;
529
530 pinctrl_free_gpio(gpio);
531}
532
533static struct gpio_chip abx500gpio_chip = {
534 .label = "abx500-gpio",
535 .owner = THIS_MODULE,
536 .request = abx500_gpio_request,
537 .free = abx500_gpio_free,
538 .direction_input = abx500_gpio_direction_input,
539 .get = abx500_gpio_get,
540 .direction_output = abx500_gpio_direction_output,
541 .set = abx500_gpio_set,
542 .to_irq = abx500_gpio_to_irq,
543 .dbg_show = abx500_gpio_dbg_show,
544};
545
Patrice Chotard0493e642013-01-08 10:41:02 +0100546static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
547{
548 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
549
550 return pct->soc->nfunctions;
551}
552
553static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
554 unsigned function)
555{
556 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
557
558 return pct->soc->functions[function].name;
559}
560
561static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000562 unsigned function,
563 const char * const **groups,
564 unsigned * const num_groups)
Patrice Chotard0493e642013-01-08 10:41:02 +0100565{
566 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
567
568 *groups = pct->soc->functions[function].groups;
569 *num_groups = pct->soc->functions[function].ngroups;
570
571 return 0;
572}
573
Patrice Chotard0493e642013-01-08 10:41:02 +0100574static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
Lee Jones83b423c2013-01-23 13:24:08 +0000575 unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100576{
577 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
578 struct gpio_chip *chip = &pct->chip;
579 const struct abx500_pingroup *g;
580 int i;
581 int ret = 0;
582
583 g = &pct->soc->groups[group];
584 if (g->altsetting < 0)
585 return -EINVAL;
586
587 dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
588
589 for (i = 0; i < g->npins; i++) {
590 dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
591 g->pins[i], g->altsetting);
592
Patrice Chotard0493e642013-01-08 10:41:02 +0100593 ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
594 }
Lee Jones83b423c2013-01-23 13:24:08 +0000595
Patrice Chotard0493e642013-01-08 10:41:02 +0100596 return ret;
597}
598
599static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000600 unsigned function, unsigned group)
Patrice Chotard0493e642013-01-08 10:41:02 +0100601{
602 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
603 const struct abx500_pingroup *g;
604
605 g = &pct->soc->groups[group];
606 if (g->altsetting < 0)
607 return;
608
609 /* FIXME: poke out the mux, set the pin to some default state? */
610 dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
611}
612
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530613static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000614 struct pinctrl_gpio_range *range,
615 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100616{
617 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
618 const struct abx500_pinrange *p;
619 int ret;
620 int i;
621
622 /*
623 * Different ranges have different ways to enable GPIO function on a
624 * pin, so refer back to our local range type, where we handily define
625 * what altfunc enables GPIO for a certain pin.
626 */
627 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
628 p = &pct->soc->gpio_ranges[i];
629 if ((offset >= p->offset) &&
630 (offset < (p->offset + p->npins)))
631 break;
632 }
633
634 if (i == pct->soc->gpio_num_ranges) {
635 dev_err(pct->dev, "%s failed to locate range\n", __func__);
636 return -ENODEV;
637 }
638
639 dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
640 p->altfunc, offset);
641
642 ret = abx500_set_mode(pct->pctldev, &pct->chip,
643 offset, p->altfunc);
644 if (ret < 0) {
645 dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
646 return ret;
647 }
648
649 return ret;
650}
651
652static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000653 struct pinctrl_gpio_range *range,
654 unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100655{
656}
657
Laurent Pinchart022ab142013-02-16 10:25:07 +0100658static const struct pinmux_ops abx500_pinmux_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100659 .get_functions_count = abx500_pmx_get_funcs_cnt,
660 .get_function_name = abx500_pmx_get_func_name,
661 .get_function_groups = abx500_pmx_get_func_groups,
662 .enable = abx500_pmx_enable,
663 .disable = abx500_pmx_disable,
664 .gpio_request_enable = abx500_gpio_request_enable,
665 .gpio_disable_free = abx500_gpio_disable_free,
666};
667
668static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
669{
670 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
671
672 return pct->soc->ngroups;
673}
674
675static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000676 unsigned selector)
Patrice Chotard0493e642013-01-08 10:41:02 +0100677{
678 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
679
680 return pct->soc->groups[selector].name;
681}
682
683static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000684 unsigned selector,
685 const unsigned **pins,
686 unsigned *num_pins)
Patrice Chotard0493e642013-01-08 10:41:02 +0100687{
688 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
689
690 *pins = pct->soc->groups[selector].pins;
691 *num_pins = pct->soc->groups[selector].npins;
Lee Jones83b423c2013-01-23 13:24:08 +0000692
Patrice Chotard0493e642013-01-08 10:41:02 +0100693 return 0;
694}
695
696static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000697 struct seq_file *s, unsigned offset)
Patrice Chotard0493e642013-01-08 10:41:02 +0100698{
699 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
700 struct gpio_chip *chip = &pct->chip;
701
702 abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
703 chip->base + offset - 1);
704}
705
Laurent Pinchart022ab142013-02-16 10:25:07 +0100706static const struct pinctrl_ops abx500_pinctrl_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100707 .get_groups_count = abx500_get_groups_cnt,
708 .get_group_name = abx500_get_group_name,
709 .get_group_pins = abx500_get_group_pins,
710 .pin_dbg_show = abx500_pin_dbg_show,
711};
712
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530713static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000714 unsigned pin,
715 unsigned long *config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100716{
Lee Jones1abeebe2012-12-20 11:11:19 +0000717 return -ENOSYS;
Patrice Chotard0493e642013-01-08 10:41:02 +0100718}
719
Sachin Kamat9c4154e2013-03-19 12:01:17 +0530720static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
Lee Jones83b423c2013-01-23 13:24:08 +0000721 unsigned pin,
722 unsigned long config)
Patrice Chotard0493e642013-01-08 10:41:02 +0100723{
724 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
725 struct pullud *pullud = pct->soc->pullud;
726 struct gpio_chip *chip = &pct->chip;
727 unsigned offset;
Patrice Chotard9ed3cd32013-05-24 14:06:30 +0200728 int ret = 0;
Patrice Chotard0493e642013-01-08 10:41:02 +0100729 enum pin_config_param param = pinconf_to_config_param(config);
730 enum pin_config_param argument = pinconf_to_config_argument(config);
731
732 dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
733 pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
734 (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
735 (argument ? "pull up" : "pull down"));
Lee Jones83b423c2013-01-23 13:24:08 +0000736
Patrice Chotard0493e642013-01-08 10:41:02 +0100737 /* on ABx500, there is no GPIO0, so adjust the offset */
738 offset = pin - 1;
739
740 switch (param) {
741 case PIN_CONFIG_BIAS_PULL_DOWN:
742 /*
743 * if argument = 1 set the pull down
744 * else clear the pull down
745 */
746 ret = abx500_gpio_direction_input(chip, offset);
747 /*
748 * Some chips only support pull down, while some actually
749 * support both pull up and pull down. Such chips have
750 * a "pullud" range specified for the pins that support
751 * both features. If the pin is not within that range, we
752 * fall back to the old bit set that only support pull down.
753 */
754 if (pullud &&
755 pin >= pullud->first_pin &&
756 pin <= pullud->last_pin)
757 ret = abx500_config_pull_updown(pct,
758 pin,
759 argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
760 else
761 /* Chip only supports pull down */
762 ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
763 offset, argument ? 0 : 1);
764 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000765
Patrice Chotard9ed3cd32013-05-24 14:06:30 +0200766 case PIN_CONFIG_BIAS_PULL_UP:
767 /*
768 * if argument = 1 set the pull up
769 * else clear the pull up
770 */
771 ret = abx500_gpio_direction_input(chip, offset);
772 /*
773 * Some chips only support pull down, while some actually
774 * support both pull up and pull down. Such chips have
775 * a "pullud" range specified for the pins that support
776 * both features. If the pin is not within that range, do
777 * nothing
778 */
779 if (pullud &&
780 pin >= pullud->first_pin &&
781 pin <= pullud->last_pin) {
782 ret = abx500_config_pull_updown(pct,
783 pin,
784 argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
785 }
786 break;
787
Patrice Chotard0493e642013-01-08 10:41:02 +0100788 case PIN_CONFIG_OUTPUT:
789 ret = abx500_gpio_direction_output(chip, offset, argument);
Lee Jones83b423c2013-01-23 13:24:08 +0000790
Patrice Chotard0493e642013-01-08 10:41:02 +0100791 break;
Lee Jones83b423c2013-01-23 13:24:08 +0000792
Patrice Chotard0493e642013-01-08 10:41:02 +0100793 default:
794 dev_err(chip->dev, "illegal configuration requested\n");
Lee Jones83b423c2013-01-23 13:24:08 +0000795
Patrice Chotard0493e642013-01-08 10:41:02 +0100796 return -EINVAL;
797 }
Lee Jones83b423c2013-01-23 13:24:08 +0000798
Patrice Chotard0493e642013-01-08 10:41:02 +0100799 return ret;
800}
801
Laurent Pinchart022ab142013-02-16 10:25:07 +0100802static const struct pinconf_ops abx500_pinconf_ops = {
Patrice Chotard0493e642013-01-08 10:41:02 +0100803 .pin_config_get = abx500_pin_config_get,
804 .pin_config_set = abx500_pin_config_set,
805};
806
807static struct pinctrl_desc abx500_pinctrl_desc = {
808 .name = "pinctrl-abx500",
809 .pctlops = &abx500_pinctrl_ops,
810 .pmxops = &abx500_pinmux_ops,
811 .confops = &abx500_pinconf_ops,
812 .owner = THIS_MODULE,
813};
814
815static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
816{
817 unsigned int lowest = 0;
818 unsigned int highest = 0;
819 unsigned int npins = 0;
820 int i;
821
822 /*
823 * Compute number of GPIOs from the last SoC gpio range descriptors
824 * These ranges may include "holes" but the GPIO number space shall
825 * still be homogeneous, so we need to detect and account for any
826 * such holes so that these are included in the number of GPIO pins.
827 */
828 for (i = 0; i < soc->gpio_num_ranges; i++) {
829 unsigned gstart;
830 unsigned gend;
831 const struct abx500_pinrange *p;
832
833 p = &soc->gpio_ranges[i];
834 gstart = p->offset;
835 gend = p->offset + p->npins - 1;
836
837 if (i == 0) {
838 /* First iteration, set start values */
839 lowest = gstart;
840 highest = gend;
841 } else {
842 if (gstart < lowest)
843 lowest = gstart;
844 if (gend > highest)
845 highest = gend;
846 }
847 }
848 /* this gives the absolute number of pins */
849 npins = highest - lowest + 1;
850 return npins;
851}
852
Lee Jonesf30a3832013-01-31 11:07:40 +0000853static const struct of_device_id abx500_gpio_match[] = {
854 { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
855 { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
856 { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
857 { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
Axel Line3929712013-02-17 21:58:47 +0800858 { }
Lee Jonesf30a3832013-01-31 11:07:40 +0000859};
860
Patrice Chotard0493e642013-01-08 10:41:02 +0100861static int abx500_gpio_probe(struct platform_device *pdev)
862{
863 struct ab8500_platform_data *abx500_pdata =
864 dev_get_platdata(pdev->dev.parent);
Lee Jonesf30a3832013-01-31 11:07:40 +0000865 struct abx500_gpio_platform_data *pdata = NULL;
866 struct device_node *np = pdev->dev.of_node;
Patrice Chotard0493e642013-01-08 10:41:02 +0100867 struct abx500_pinctrl *pct;
868 const struct platform_device_id *platid = platform_get_device_id(pdev);
Lee Jonesf30a3832013-01-31 11:07:40 +0000869 unsigned int id = -1;
Lee Jonesfa1ec992013-01-31 11:06:33 +0000870 int ret, err;
Patrice Chotard0493e642013-01-08 10:41:02 +0100871 int i;
872
Lee Jonesf30a3832013-01-31 11:07:40 +0000873 if (abx500_pdata)
874 pdata = abx500_pdata->gpio;
Lee Jonesf30a3832013-01-31 11:07:40 +0000875
Lee Jones86c976e2013-05-08 14:29:08 +0100876 if (!(pdata || np)) {
877 dev_err(&pdev->dev, "gpio dt and platform data missing\n");
878 return -ENODEV;
Patrice Chotard0493e642013-01-08 10:41:02 +0100879 }
880
881 pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
882 GFP_KERNEL);
883 if (pct == NULL) {
884 dev_err(&pdev->dev,
885 "failed to allocate memory for pct\n");
886 return -ENOMEM;
887 }
888
889 pct->dev = &pdev->dev;
890 pct->parent = dev_get_drvdata(pdev->dev.parent);
891 pct->chip = abx500gpio_chip;
892 pct->chip.dev = &pdev->dev;
Lee Jonesf30a3832013-01-31 11:07:40 +0000893 pct->chip.base = (np) ? -1 : pdata->gpio_base;
Patrice Chotard0493e642013-01-08 10:41:02 +0100894
Lee Jones86c976e2013-05-08 14:29:08 +0100895 if (platid)
896 id = platid->driver_data;
897 else if (np) {
898 const struct of_device_id *match;
899
900 match = of_match_device(abx500_gpio_match, &pdev->dev);
901 if (match)
902 id = (unsigned long)match->data;
903 }
904
Patrice Chotard0493e642013-01-08 10:41:02 +0100905 /* Poke in other ASIC variants here */
Lee Jonesf30a3832013-01-31 11:07:40 +0000906 switch (id) {
Patrice Chotard3c937992013-01-08 10:59:53 +0100907 case PINCTRL_AB8500:
908 abx500_pinctrl_ab8500_init(&pct->soc);
909 break;
Patrice Chotarda8f96e42013-01-28 14:35:19 +0100910 case PINCTRL_AB8540:
911 abx500_pinctrl_ab8540_init(&pct->soc);
912 break;
Patrice Chotard09dbec32013-01-28 14:29:35 +0100913 case PINCTRL_AB9540:
914 abx500_pinctrl_ab9540_init(&pct->soc);
915 break;
Patrice Chotard1aa2d8d2013-01-28 14:23:45 +0100916 case PINCTRL_AB8505:
917 abx500_pinctrl_ab8505_init(&pct->soc);
918 break;
Patrice Chotard0493e642013-01-08 10:41:02 +0100919 default:
Lee Jones2fcad122013-05-08 14:29:07 +0100920 dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
Patrice Chotard0493e642013-01-08 10:41:02 +0100921 return -EINVAL;
922 }
923
924 if (!pct->soc) {
925 dev_err(&pdev->dev, "Invalid SOC data\n");
926 return -EINVAL;
927 }
928
929 pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
930 pct->irq_cluster = pct->soc->gpio_irq_cluster;
931 pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
Patrice Chotard0493e642013-01-08 10:41:02 +0100932
Patrice Chotard0493e642013-01-08 10:41:02 +0100933 ret = gpiochip_add(&pct->chip);
934 if (ret) {
Lee Jones83b423c2013-01-23 13:24:08 +0000935 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
Lee Jonesac652d72013-01-31 10:43:00 +0000936 return ret;
Patrice Chotard0493e642013-01-08 10:41:02 +0100937 }
938 dev_info(&pdev->dev, "added gpiochip\n");
939
940 abx500_pinctrl_desc.pins = pct->soc->pins;
941 abx500_pinctrl_desc.npins = pct->soc->npins;
942 pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
943 if (!pct->pctldev) {
944 dev_err(&pdev->dev,
945 "could not register abx500 pinctrl driver\n");
Lee Jonesfa1ec992013-01-31 11:06:33 +0000946 ret = -EINVAL;
Patrice Chotard0493e642013-01-08 10:41:02 +0100947 goto out_rem_chip;
948 }
949 dev_info(&pdev->dev, "registered pin controller\n");
950
951 /* We will handle a range of GPIO pins */
952 for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
953 const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
954
955 ret = gpiochip_add_pin_range(&pct->chip,
956 dev_name(&pdev->dev),
957 p->offset - 1, p->offset, p->npins);
958 if (ret < 0)
Lee Jonesfa1ec992013-01-31 11:06:33 +0000959 goto out_rem_chip;
Patrice Chotard0493e642013-01-08 10:41:02 +0100960 }
961
962 platform_set_drvdata(pdev, pct);
963 dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
964
965 return 0;
966
967out_rem_chip:
Lee Jonesfa1ec992013-01-31 11:06:33 +0000968 err = gpiochip_remove(&pct->chip);
969 if (err)
Patrice Chotard0493e642013-01-08 10:41:02 +0100970 dev_info(&pdev->dev, "failed to remove gpiochip\n");
Lee Jonesac652d72013-01-31 10:43:00 +0000971
Patrice Chotard0493e642013-01-08 10:41:02 +0100972 return ret;
973}
974
Lee Jones83b423c2013-01-23 13:24:08 +0000975/**
Patrice Chotard0493e642013-01-08 10:41:02 +0100976 * abx500_gpio_remove() - remove Ab8500-gpio driver
Lee Jones83b423c2013-01-23 13:24:08 +0000977 * @pdev: Platform device registered
Patrice Chotard0493e642013-01-08 10:41:02 +0100978 */
979static int abx500_gpio_remove(struct platform_device *pdev)
980{
981 struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
982 int ret;
983
984 ret = gpiochip_remove(&pct->chip);
985 if (ret < 0) {
986 dev_err(pct->dev, "unable to remove gpiochip: %d\n",
987 ret);
988 return ret;
989 }
990
Patrice Chotard0493e642013-01-08 10:41:02 +0100991 return 0;
992}
993
994static const struct platform_device_id abx500_pinctrl_id[] = {
995 { "pinctrl-ab8500", PINCTRL_AB8500 },
996 { "pinctrl-ab8540", PINCTRL_AB8540 },
997 { "pinctrl-ab9540", PINCTRL_AB9540 },
998 { "pinctrl-ab8505", PINCTRL_AB8505 },
999 { },
1000};
1001
1002static struct platform_driver abx500_gpio_driver = {
1003 .driver = {
1004 .name = "abx500-gpio",
1005 .owner = THIS_MODULE,
Lee Jonesf30a3832013-01-31 11:07:40 +00001006 .of_match_table = abx500_gpio_match,
Patrice Chotard0493e642013-01-08 10:41:02 +01001007 },
1008 .probe = abx500_gpio_probe,
1009 .remove = abx500_gpio_remove,
1010 .id_table = abx500_pinctrl_id,
1011};
1012
1013static int __init abx500_gpio_init(void)
1014{
1015 return platform_driver_register(&abx500_gpio_driver);
1016}
1017core_initcall(abx500_gpio_init);
1018
1019MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1020MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1021MODULE_ALIAS("platform:abx500-gpio");
1022MODULE_LICENSE("GPL v2");