2 * Copyright (C) ST-Ericsson SA 2013
4 * Author: Patrice Chotard <patrice.chotard@st.com>
5 * License terms: GNU General Public License (GPL) version 2
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.
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>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/gpio.h>
21 #include <linux/irq.h>
22 #include <linux/irqdomain.h>
23 #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/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf.h>
31 #include <linux/pinctrl/pinconf-generic.h>
32 #include <linux/pinctrl/machine.h>
34 #include "pinctrl-abx500.h"
36 #include "../pinconf.h"
37 #include "../pinctrl-utils.h"
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.
49 * GPIO registers offset
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
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
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
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
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
94 #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
99 #define ABX500_GPIO_INPUT 0
100 #define ABX500_GPIO_OUTPUT 1
102 struct abx500_pinctrl
{
104 struct pinctrl_dev
*pctldev
;
105 struct abx500_pinctrl_soc_data
*soc
;
106 struct gpio_chip chip
;
107 struct ab8500
*parent
;
108 struct abx500_gpio_irq_cluster
*irq_cluster
;
109 int irq_cluster_size
;
112 static int abx500_gpio_get_bit(struct gpio_chip
*chip
, u8 reg
,
113 unsigned offset
, bool *bit
)
115 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
121 ret
= abx500_get_register_interruptible(pct
->dev
,
122 AB8500_MISC
, reg
, &val
);
124 *bit
= !!(val
& BIT(pos
));
128 "%s read reg =%x, offset=%x failed (%d)\n",
129 __func__
, reg
, offset
, ret
);
134 static int abx500_gpio_set_bits(struct gpio_chip
*chip
, u8 reg
,
135 unsigned offset
, int val
)
137 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
142 ret
= abx500_mask_and_set_register_interruptible(pct
->dev
,
143 AB8500_MISC
, reg
, BIT(pos
), val
<< pos
);
145 dev_err(pct
->dev
, "%s write reg, %x offset %x failed (%d)\n",
146 __func__
, reg
, offset
, ret
);
152 * abx500_gpio_get() - Get the particular GPIO value
154 * @offset: GPIO number to read
156 static int abx500_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
158 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
161 u8 gpio_offset
= offset
- 1;
164 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_DIR1_REG
,
165 gpio_offset
, &is_out
);
170 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_OUT1_REG
,
173 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_IN1_REG
,
177 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
184 static void abx500_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
186 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
189 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_OUT1_REG
, offset
, val
);
191 dev_err(pct
->dev
, "%s write failed (%d)\n", __func__
, ret
);
194 #ifdef CONFIG_DEBUG_FS
195 static int abx500_get_pull_updown(struct abx500_pinctrl
*pct
, int offset
,
196 enum abx500_gpio_pull_updown
*pull_updown
)
201 struct pullud
*pullud
;
203 if (!pct
->soc
->pullud
) {
204 dev_err(pct
->dev
, "%s AB chip doesn't support pull up/down feature",
210 pullud
= pct
->soc
->pullud
;
212 if ((offset
< pullud
->first_pin
)
213 || (offset
> pullud
->last_pin
)) {
218 ret
= abx500_get_register_interruptible(pct
->dev
,
219 AB8500_MISC
, AB8540_GPIO_PULL_UPDOWN_REG
, &val
);
221 pos
= (offset
- pullud
->first_pin
) << 1;
222 *pull_updown
= (val
>> pos
) & AB8540_GPIO_PULL_UPDOWN_MASK
;
226 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
232 static int abx500_set_pull_updown(struct abx500_pinctrl
*pct
,
233 int offset
, enum abx500_gpio_pull_updown val
)
237 struct pullud
*pullud
;
239 if (!pct
->soc
->pullud
) {
240 dev_err(pct
->dev
, "%s AB chip doesn't support pull up/down feature",
246 pullud
= pct
->soc
->pullud
;
248 if ((offset
< pullud
->first_pin
)
249 || (offset
> pullud
->last_pin
)) {
253 pos
= (offset
- pullud
->first_pin
) << 1;
255 ret
= abx500_mask_and_set_register_interruptible(pct
->dev
,
256 AB8500_MISC
, AB8540_GPIO_PULL_UPDOWN_REG
,
257 AB8540_GPIO_PULL_UPDOWN_MASK
<< pos
, val
<< pos
);
261 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
266 static bool abx500_pullud_supported(struct gpio_chip
*chip
, unsigned gpio
)
268 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
269 struct pullud
*pullud
= pct
->soc
->pullud
;
272 gpio
>= pullud
->first_pin
&&
273 gpio
<= pullud
->last_pin
);
276 static int abx500_gpio_direction_output(struct gpio_chip
*chip
,
280 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
284 /* set direction as output */
285 ret
= abx500_gpio_set_bits(chip
,
286 AB8500_GPIO_DIR1_REG
,
292 /* disable pull down */
293 ret
= abx500_gpio_set_bits(chip
,
294 AB8500_GPIO_PUD1_REG
,
296 ABX500_GPIO_PULL_NONE
);
300 /* if supported, disable both pull down and pull up */
302 if (abx500_pullud_supported(chip
, gpio
)) {
303 ret
= abx500_set_pull_updown(pct
,
305 ABX500_GPIO_PULL_NONE
);
309 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
313 /* set the output as 1 or 0 */
314 return abx500_gpio_set_bits(chip
, AB8500_GPIO_OUT1_REG
, offset
, val
);
317 static int abx500_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
319 /* set the register as input */
320 return abx500_gpio_set_bits(chip
,
321 AB8500_GPIO_DIR1_REG
,
326 static int abx500_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
328 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
329 /* The AB8500 GPIO numbers are off by one */
330 int gpio
= offset
+ 1;
334 for (i
= 0; i
< pct
->irq_cluster_size
; i
++) {
335 struct abx500_gpio_irq_cluster
*cluster
=
336 &pct
->irq_cluster
[i
];
338 if (gpio
>= cluster
->start
&& gpio
<= cluster
->end
) {
340 * The ABx500 GPIO's associated IRQs are clustered together
341 * throughout the interrupt numbers at irregular intervals.
342 * To solve this quandry, we have placed the read-in values
343 * into the cluster information table.
345 hwirq
= gpio
- cluster
->start
+ cluster
->to_irq
;
346 return irq_create_mapping(pct
->parent
->domain
, hwirq
);
353 static int abx500_set_mode(struct pinctrl_dev
*pctldev
, struct gpio_chip
*chip
,
354 unsigned gpio
, int alt_setting
)
356 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
357 struct alternate_functions af
= pct
->soc
->alternate_functions
[gpio
];
362 const char *modes
[] = {
363 [ABX500_DEFAULT
] = "default",
364 [ABX500_ALT_A
] = "altA",
365 [ABX500_ALT_B
] = "altB",
366 [ABX500_ALT_C
] = "altC",
370 if (((alt_setting
== ABX500_ALT_A
) && (af
.gpiosel_bit
== UNUSED
)) ||
371 ((alt_setting
== ABX500_ALT_B
) && (af
.alt_bit1
== UNUSED
)) ||
372 ((alt_setting
== ABX500_ALT_C
) && (af
.alt_bit2
== UNUSED
))) {
373 dev_dbg(pct
->dev
, "pin %d doesn't support %s mode\n", gpio
,
378 /* on ABx5xx, there is no GPIO0, so adjust the offset */
381 switch (alt_setting
) {
384 * for ABx5xx family, default mode is always selected by
385 * writing 0 to GPIOSELx register, except for pins which
386 * support at least ALT_B mode, default mode is selected
387 * by writing 1 to GPIOSELx register
390 if (af
.alt_bit1
!= UNUSED
)
393 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_SEL1_REG
,
399 * for ABx5xx family, alt_a mode is always selected by
400 * writing 1 to GPIOSELx register, except for pins which
401 * support at least ALT_B mode, alt_a mode is selected
402 * by writing 0 to GPIOSELx register and 0 in ALTFUNC
405 if (af
.alt_bit1
!= UNUSED
) {
406 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_SEL1_REG
,
411 ret
= abx500_gpio_set_bits(chip
,
412 AB8500_GPIO_ALTFUN_REG
,
414 !!(af
.alta_val
& BIT(0)));
418 if (af
.alt_bit2
!= UNUSED
)
419 ret
= abx500_gpio_set_bits(chip
,
420 AB8500_GPIO_ALTFUN_REG
,
422 !!(af
.alta_val
& BIT(1)));
424 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_SEL1_REG
,
429 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_SEL1_REG
,
434 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_ALTFUN_REG
,
435 af
.alt_bit1
, !!(af
.altb_val
& BIT(0)));
439 if (af
.alt_bit2
!= UNUSED
)
440 ret
= abx500_gpio_set_bits(chip
,
441 AB8500_GPIO_ALTFUN_REG
,
443 !!(af
.altb_val
& BIT(1)));
447 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_SEL1_REG
,
452 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_ALTFUN_REG
,
453 af
.alt_bit2
, !!(af
.altc_val
& BIT(0)));
457 ret
= abx500_gpio_set_bits(chip
, AB8500_GPIO_ALTFUN_REG
,
458 af
.alt_bit2
, !!(af
.altc_val
& BIT(1)));
462 dev_dbg(pct
->dev
, "unknown alt_setting %d\n", alt_setting
);
468 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
473 #ifdef CONFIG_DEBUG_FS
474 static int abx500_get_mode(struct pinctrl_dev
*pctldev
, struct gpio_chip
*chip
,
481 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
482 struct alternate_functions af
= pct
->soc
->alternate_functions
[gpio
];
483 /* on ABx5xx, there is no GPIO0, so adjust the offset */
484 unsigned offset
= gpio
- 1;
488 * if gpiosel_bit is set to unused,
489 * it means no GPIO or special case
491 if (af
.gpiosel_bit
== UNUSED
)
492 return ABX500_DEFAULT
;
494 /* read GpioSelx register */
495 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_SEL1_REG
+ (offset
/ 8),
496 af
.gpiosel_bit
, &bit_mode
);
503 if ((af
.alt_bit1
< UNUSED
) || (af
.alt_bit1
> 7) ||
504 (af
.alt_bit2
< UNUSED
) || (af
.alt_bit2
> 7)) {
506 "alt_bitX value not in correct range (-1 to 7)\n");
510 /* if alt_bit2 is used, alt_bit1 must be used too */
511 if ((af
.alt_bit2
!= UNUSED
) && (af
.alt_bit1
== UNUSED
)) {
513 "if alt_bit2 is used, alt_bit1 can't be unused\n");
517 /* check if pin use AlternateFunction register */
518 if ((af
.alt_bit1
== UNUSED
) && (af
.alt_bit2
== UNUSED
))
521 * if pin GPIOSEL bit is set and pin supports alternate function,
522 * it means DEFAULT mode
525 return ABX500_DEFAULT
;
528 * pin use the AlternatFunction register
529 * read alt_bit1 value
531 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_ALTFUN_REG
,
532 af
.alt_bit1
, &alt_bit1
);
536 if (af
.alt_bit2
!= UNUSED
) {
537 /* read alt_bit2 value */
538 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_ALTFUN_REG
,
546 mode
= (alt_bit2
<< 1) + alt_bit1
;
547 if (mode
== af
.alta_val
)
549 else if (mode
== af
.altb_val
)
555 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
559 #include <linux/seq_file.h>
561 static void abx500_gpio_dbg_show_one(struct seq_file
*s
,
562 struct pinctrl_dev
*pctldev
,
563 struct gpio_chip
*chip
,
564 unsigned offset
, unsigned gpio
)
566 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
567 const char *label
= gpiochip_is_requested(chip
, offset
- 1);
568 u8 gpio_offset
= offset
- 1;
572 enum abx500_gpio_pull_updown pud
= 0;
575 const char *modes
[] = {
576 [ABX500_DEFAULT
] = "default",
577 [ABX500_ALT_A
] = "altA",
578 [ABX500_ALT_B
] = "altB",
579 [ABX500_ALT_C
] = "altC",
582 const char *pull_up_down
[] = {
583 [ABX500_GPIO_PULL_DOWN
] = "pull down",
584 [ABX500_GPIO_PULL_NONE
] = "pull none",
585 [ABX500_GPIO_PULL_NONE
+ 1] = "pull none",
586 [ABX500_GPIO_PULL_UP
] = "pull up",
589 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_DIR1_REG
,
590 gpio_offset
, &is_out
);
594 seq_printf(s
, " gpio-%-3d (%-20.20s) %-3s",
595 gpio
, label
?: "(none)",
596 is_out
? "out" : "in ");
599 if (abx500_pullud_supported(chip
, offset
)) {
600 ret
= abx500_get_pull_updown(pct
, offset
, &pud
);
604 seq_printf(s
, " %-9s", pull_up_down
[pud
]);
606 ret
= abx500_gpio_get_bit(chip
, AB8500_GPIO_PUD1_REG
,
611 seq_printf(s
, " %-9s", pull_up_down
[pd
]);
614 seq_printf(s
, " %-9s", chip
->get(chip
, offset
) ? "hi" : "lo");
616 mode
= abx500_get_mode(pctldev
, chip
, offset
);
618 seq_printf(s
, " %s", (mode
< 0) ? "unknown" : modes
[mode
]);
622 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
625 static void abx500_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
628 unsigned gpio
= chip
->base
;
629 struct abx500_pinctrl
*pct
= gpiochip_get_data(chip
);
630 struct pinctrl_dev
*pctldev
= pct
->pctldev
;
632 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
633 /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
634 abx500_gpio_dbg_show_one(s
, pctldev
, chip
, i
+ 1, gpio
);
640 static inline void abx500_gpio_dbg_show_one(struct seq_file
*s
,
641 struct pinctrl_dev
*pctldev
,
642 struct gpio_chip
*chip
,
643 unsigned offset
, unsigned gpio
)
646 #define abx500_gpio_dbg_show NULL
649 static struct gpio_chip abx500gpio_chip
= {
650 .label
= "abx500-gpio",
651 .owner
= THIS_MODULE
,
652 .request
= gpiochip_generic_request
,
653 .free
= gpiochip_generic_free
,
654 .direction_input
= abx500_gpio_direction_input
,
655 .get
= abx500_gpio_get
,
656 .direction_output
= abx500_gpio_direction_output
,
657 .set
= abx500_gpio_set
,
658 .to_irq
= abx500_gpio_to_irq
,
659 .dbg_show
= abx500_gpio_dbg_show
,
662 static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
664 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
666 return pct
->soc
->nfunctions
;
669 static const char *abx500_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
672 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
674 return pct
->soc
->functions
[function
].name
;
677 static int abx500_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
679 const char * const **groups
,
680 unsigned * const num_groups
)
682 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
684 *groups
= pct
->soc
->functions
[function
].groups
;
685 *num_groups
= pct
->soc
->functions
[function
].ngroups
;
690 static int abx500_pmx_set(struct pinctrl_dev
*pctldev
, unsigned function
,
693 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
694 struct gpio_chip
*chip
= &pct
->chip
;
695 const struct abx500_pingroup
*g
;
699 g
= &pct
->soc
->groups
[group
];
700 if (g
->altsetting
< 0)
703 dev_dbg(pct
->dev
, "enable group %s, %u pins\n", g
->name
, g
->npins
);
705 for (i
= 0; i
< g
->npins
; i
++) {
706 dev_dbg(pct
->dev
, "setting pin %d to altsetting %d\n",
707 g
->pins
[i
], g
->altsetting
);
709 ret
= abx500_set_mode(pctldev
, chip
, g
->pins
[i
], g
->altsetting
);
713 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
718 static int abx500_gpio_request_enable(struct pinctrl_dev
*pctldev
,
719 struct pinctrl_gpio_range
*range
,
722 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
723 const struct abx500_pinrange
*p
;
728 * Different ranges have different ways to enable GPIO function on a
729 * pin, so refer back to our local range type, where we handily define
730 * what altfunc enables GPIO for a certain pin.
732 for (i
= 0; i
< pct
->soc
->gpio_num_ranges
; i
++) {
733 p
= &pct
->soc
->gpio_ranges
[i
];
734 if ((offset
>= p
->offset
) &&
735 (offset
< (p
->offset
+ p
->npins
)))
739 if (i
== pct
->soc
->gpio_num_ranges
) {
740 dev_err(pct
->dev
, "%s failed to locate range\n", __func__
);
744 dev_dbg(pct
->dev
, "enable GPIO by altfunc %d at gpio %d\n",
747 ret
= abx500_set_mode(pct
->pctldev
, &pct
->chip
,
750 dev_err(pct
->dev
, "%s setting altfunc failed\n", __func__
);
755 static void abx500_gpio_disable_free(struct pinctrl_dev
*pctldev
,
756 struct pinctrl_gpio_range
*range
,
761 static const struct pinmux_ops abx500_pinmux_ops
= {
762 .get_functions_count
= abx500_pmx_get_funcs_cnt
,
763 .get_function_name
= abx500_pmx_get_func_name
,
764 .get_function_groups
= abx500_pmx_get_func_groups
,
765 .set_mux
= abx500_pmx_set
,
766 .gpio_request_enable
= abx500_gpio_request_enable
,
767 .gpio_disable_free
= abx500_gpio_disable_free
,
770 static int abx500_get_groups_cnt(struct pinctrl_dev
*pctldev
)
772 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
774 return pct
->soc
->ngroups
;
777 static const char *abx500_get_group_name(struct pinctrl_dev
*pctldev
,
780 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
782 return pct
->soc
->groups
[selector
].name
;
785 static int abx500_get_group_pins(struct pinctrl_dev
*pctldev
,
787 const unsigned **pins
,
790 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
792 *pins
= pct
->soc
->groups
[selector
].pins
;
793 *num_pins
= pct
->soc
->groups
[selector
].npins
;
798 static void abx500_pin_dbg_show(struct pinctrl_dev
*pctldev
,
799 struct seq_file
*s
, unsigned offset
)
801 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
802 struct gpio_chip
*chip
= &pct
->chip
;
804 abx500_gpio_dbg_show_one(s
, pctldev
, chip
, offset
,
805 chip
->base
+ offset
- 1);
808 static int abx500_dt_add_map_mux(struct pinctrl_map
**map
,
809 unsigned *reserved_maps
,
810 unsigned *num_maps
, const char *group
,
811 const char *function
)
813 if (*num_maps
== *reserved_maps
)
816 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
817 (*map
)[*num_maps
].data
.mux
.group
= group
;
818 (*map
)[*num_maps
].data
.mux
.function
= function
;
824 static int abx500_dt_add_map_configs(struct pinctrl_map
**map
,
825 unsigned *reserved_maps
,
826 unsigned *num_maps
, const char *group
,
827 unsigned long *configs
, unsigned num_configs
)
829 unsigned long *dup_configs
;
831 if (*num_maps
== *reserved_maps
)
834 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
839 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
841 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
842 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
843 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
849 static const char *abx500_find_pin_name(struct pinctrl_dev
*pctldev
,
850 const char *pin_name
)
853 struct abx500_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
855 if (sscanf((char *)pin_name
, "GPIO%d", &pin_number
) == 1)
856 for (i
= 0; i
< npct
->soc
->npins
; i
++)
857 if (npct
->soc
->pins
[i
].number
== pin_number
)
858 return npct
->soc
->pins
[i
].name
;
862 static int abx500_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
863 struct device_node
*np
,
864 struct pinctrl_map
**map
,
865 unsigned *reserved_maps
,
869 const char *function
= NULL
;
870 unsigned long *configs
;
871 unsigned int nconfigs
= 0;
872 struct property
*prop
;
874 ret
= of_property_read_string(np
, "function", &function
);
878 ret
= of_property_count_strings(np
, "groups");
882 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
,
887 of_property_for_each_string(np
, "groups", prop
, group
) {
888 ret
= abx500_dt_add_map_mux(map
, reserved_maps
,
889 num_maps
, group
, function
);
895 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
, &nconfigs
);
897 const char *gpio_name
;
900 ret
= of_property_count_strings(np
, "pins");
904 ret
= pinctrl_utils_reserve_map(pctldev
, map
,
910 of_property_for_each_string(np
, "pins", prop
, pin
) {
911 gpio_name
= abx500_find_pin_name(pctldev
, pin
);
913 ret
= abx500_dt_add_map_configs(map
, reserved_maps
,
914 num_maps
, gpio_name
, configs
, 1);
924 static int abx500_dt_node_to_map(struct pinctrl_dev
*pctldev
,
925 struct device_node
*np_config
,
926 struct pinctrl_map
**map
, unsigned *num_maps
)
928 unsigned reserved_maps
;
929 struct device_node
*np
;
936 for_each_child_of_node(np_config
, np
) {
937 ret
= abx500_dt_subnode_to_map(pctldev
, np
, map
,
938 &reserved_maps
, num_maps
);
940 pinctrl_utils_dt_free_map(pctldev
, *map
, *num_maps
);
948 static const struct pinctrl_ops abx500_pinctrl_ops
= {
949 .get_groups_count
= abx500_get_groups_cnt
,
950 .get_group_name
= abx500_get_group_name
,
951 .get_group_pins
= abx500_get_group_pins
,
952 .pin_dbg_show
= abx500_pin_dbg_show
,
953 .dt_node_to_map
= abx500_dt_node_to_map
,
954 .dt_free_map
= pinctrl_utils_dt_free_map
,
957 static int abx500_pin_config_get(struct pinctrl_dev
*pctldev
,
959 unsigned long *config
)
964 static int abx500_pin_config_set(struct pinctrl_dev
*pctldev
,
966 unsigned long *configs
,
967 unsigned num_configs
)
969 struct abx500_pinctrl
*pct
= pinctrl_dev_get_drvdata(pctldev
);
970 struct gpio_chip
*chip
= &pct
->chip
;
974 enum pin_config_param param
;
975 enum pin_config_param argument
;
977 for (i
= 0; i
< num_configs
; i
++) {
978 param
= pinconf_to_config_param(configs
[i
]);
979 argument
= pinconf_to_config_argument(configs
[i
]);
981 dev_dbg(chip
->parent
, "pin %d [%#lx]: %s %s\n",
983 (param
== PIN_CONFIG_OUTPUT
) ? "output " : "input",
984 (param
== PIN_CONFIG_OUTPUT
) ?
985 (argument
? "high" : "low") :
986 (argument
? "pull up" : "pull down"));
988 /* on ABx500, there is no GPIO0, so adjust the offset */
992 case PIN_CONFIG_BIAS_DISABLE
:
993 ret
= abx500_gpio_direction_input(chip
, offset
);
997 * Some chips only support pull down, while some
998 * actually support both pull up and pull down. Such
999 * chips have a "pullud" range specified for the pins
1000 * that support both features. If the pin is not
1001 * within that range, we fall back to the old bit set
1002 * that only support pull down.
1004 if (abx500_pullud_supported(chip
, pin
))
1005 ret
= abx500_set_pull_updown(pct
,
1007 ABX500_GPIO_PULL_NONE
);
1009 /* Chip only supports pull down */
1010 ret
= abx500_gpio_set_bits(chip
,
1011 AB8500_GPIO_PUD1_REG
, offset
,
1012 ABX500_GPIO_PULL_NONE
);
1015 case PIN_CONFIG_BIAS_PULL_DOWN
:
1016 ret
= abx500_gpio_direction_input(chip
, offset
);
1020 * if argument = 1 set the pull down
1021 * else clear the pull down
1022 * Some chips only support pull down, while some
1023 * actually support both pull up and pull down. Such
1024 * chips have a "pullud" range specified for the pins
1025 * that support both features. If the pin is not
1026 * within that range, we fall back to the old bit set
1027 * that only support pull down.
1029 if (abx500_pullud_supported(chip
, pin
))
1030 ret
= abx500_set_pull_updown(pct
,
1032 argument
? ABX500_GPIO_PULL_DOWN
:
1033 ABX500_GPIO_PULL_NONE
);
1035 /* Chip only supports pull down */
1036 ret
= abx500_gpio_set_bits(chip
,
1037 AB8500_GPIO_PUD1_REG
,
1039 argument
? ABX500_GPIO_PULL_DOWN
:
1040 ABX500_GPIO_PULL_NONE
);
1043 case PIN_CONFIG_BIAS_PULL_UP
:
1044 ret
= abx500_gpio_direction_input(chip
, offset
);
1048 * if argument = 1 set the pull up
1049 * else clear the pull up
1051 ret
= abx500_gpio_direction_input(chip
, offset
);
1053 * Some chips only support pull down, while some
1054 * actually support both pull up and pull down. Such
1055 * chips have a "pullud" range specified for the pins
1056 * that support both features. If the pin is not
1057 * within that range, do nothing
1059 if (abx500_pullud_supported(chip
, pin
))
1060 ret
= abx500_set_pull_updown(pct
,
1062 argument
? ABX500_GPIO_PULL_UP
:
1063 ABX500_GPIO_PULL_NONE
);
1066 case PIN_CONFIG_OUTPUT
:
1067 ret
= abx500_gpio_direction_output(chip
, offset
,
1072 dev_err(chip
->parent
,
1073 "illegal configuration requested\n");
1075 } /* for each config */
1078 dev_err(pct
->dev
, "%s failed (%d)\n", __func__
, ret
);
1083 static const struct pinconf_ops abx500_pinconf_ops
= {
1084 .pin_config_get
= abx500_pin_config_get
,
1085 .pin_config_set
= abx500_pin_config_set
,
1089 static struct pinctrl_desc abx500_pinctrl_desc
= {
1090 .name
= "pinctrl-abx500",
1091 .pctlops
= &abx500_pinctrl_ops
,
1092 .pmxops
= &abx500_pinmux_ops
,
1093 .confops
= &abx500_pinconf_ops
,
1094 .owner
= THIS_MODULE
,
1097 static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data
*soc
)
1099 unsigned int lowest
= 0;
1100 unsigned int highest
= 0;
1101 unsigned int npins
= 0;
1105 * Compute number of GPIOs from the last SoC gpio range descriptors
1106 * These ranges may include "holes" but the GPIO number space shall
1107 * still be homogeneous, so we need to detect and account for any
1108 * such holes so that these are included in the number of GPIO pins.
1110 for (i
= 0; i
< soc
->gpio_num_ranges
; i
++) {
1113 const struct abx500_pinrange
*p
;
1115 p
= &soc
->gpio_ranges
[i
];
1117 gend
= p
->offset
+ p
->npins
- 1;
1120 /* First iteration, set start values */
1124 if (gstart
< lowest
)
1130 /* this gives the absolute number of pins */
1131 npins
= highest
- lowest
+ 1;
1135 static const struct of_device_id abx500_gpio_match
[] = {
1136 { .compatible
= "stericsson,ab8500-gpio", .data
= (void *)PINCTRL_AB8500
, },
1137 { .compatible
= "stericsson,ab8505-gpio", .data
= (void *)PINCTRL_AB8505
, },
1138 { .compatible
= "stericsson,ab8540-gpio", .data
= (void *)PINCTRL_AB8540
, },
1139 { .compatible
= "stericsson,ab9540-gpio", .data
= (void *)PINCTRL_AB9540
, },
1143 static int abx500_gpio_probe(struct platform_device
*pdev
)
1145 struct device_node
*np
= pdev
->dev
.of_node
;
1146 const struct of_device_id
*match
;
1147 struct abx500_pinctrl
*pct
;
1148 unsigned int id
= -1;
1153 dev_err(&pdev
->dev
, "gpio dt node missing\n");
1157 pct
= devm_kzalloc(&pdev
->dev
, sizeof(struct abx500_pinctrl
),
1161 "failed to allocate memory for pct\n");
1165 pct
->dev
= &pdev
->dev
;
1166 pct
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
1167 pct
->chip
= abx500gpio_chip
;
1168 pct
->chip
.parent
= &pdev
->dev
;
1169 pct
->chip
.base
= -1; /* Dynamic allocation */
1171 match
= of_match_device(abx500_gpio_match
, &pdev
->dev
);
1173 dev_err(&pdev
->dev
, "gpio dt not matching\n");
1176 id
= (unsigned long)match
->data
;
1178 /* Poke in other ASIC variants here */
1180 case PINCTRL_AB8500
:
1181 abx500_pinctrl_ab8500_init(&pct
->soc
);
1183 case PINCTRL_AB8540
:
1184 abx500_pinctrl_ab8540_init(&pct
->soc
);
1186 case PINCTRL_AB9540
:
1187 abx500_pinctrl_ab9540_init(&pct
->soc
);
1189 case PINCTRL_AB8505
:
1190 abx500_pinctrl_ab8505_init(&pct
->soc
);
1193 dev_err(&pdev
->dev
, "Unsupported pinctrl sub driver (%d)\n", id
);
1198 dev_err(&pdev
->dev
, "Invalid SOC data\n");
1202 pct
->chip
.ngpio
= abx500_get_gpio_num(pct
->soc
);
1203 pct
->irq_cluster
= pct
->soc
->gpio_irq_cluster
;
1204 pct
->irq_cluster_size
= pct
->soc
->ngpio_irq_cluster
;
1206 ret
= gpiochip_add_data(&pct
->chip
, pct
);
1208 dev_err(&pdev
->dev
, "unable to add gpiochip: %d\n", ret
);
1211 dev_info(&pdev
->dev
, "added gpiochip\n");
1213 abx500_pinctrl_desc
.pins
= pct
->soc
->pins
;
1214 abx500_pinctrl_desc
.npins
= pct
->soc
->npins
;
1215 pct
->pctldev
= pinctrl_register(&abx500_pinctrl_desc
, &pdev
->dev
, pct
);
1216 if (IS_ERR(pct
->pctldev
)) {
1218 "could not register abx500 pinctrl driver\n");
1219 ret
= PTR_ERR(pct
->pctldev
);
1222 dev_info(&pdev
->dev
, "registered pin controller\n");
1224 /* We will handle a range of GPIO pins */
1225 for (i
= 0; i
< pct
->soc
->gpio_num_ranges
; i
++) {
1226 const struct abx500_pinrange
*p
= &pct
->soc
->gpio_ranges
[i
];
1228 ret
= gpiochip_add_pin_range(&pct
->chip
,
1229 dev_name(&pdev
->dev
),
1230 p
->offset
- 1, p
->offset
, p
->npins
);
1235 platform_set_drvdata(pdev
, pct
);
1236 dev_info(&pdev
->dev
, "initialized abx500 pinctrl driver\n");
1241 gpiochip_remove(&pct
->chip
);
1246 * abx500_gpio_remove() - remove Ab8500-gpio driver
1247 * @pdev: Platform device registered
1249 static int abx500_gpio_remove(struct platform_device
*pdev
)
1251 struct abx500_pinctrl
*pct
= platform_get_drvdata(pdev
);
1253 gpiochip_remove(&pct
->chip
);
1257 static struct platform_driver abx500_gpio_driver
= {
1259 .name
= "abx500-gpio",
1260 .of_match_table
= abx500_gpio_match
,
1262 .probe
= abx500_gpio_probe
,
1263 .remove
= abx500_gpio_remove
,
1266 static int __init
abx500_gpio_init(void)
1268 return platform_driver_register(&abx500_gpio_driver
);
1270 core_initcall(abx500_gpio_init
);
1272 MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
1273 MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
1274 MODULE_ALIAS("platform:abx500-gpio");
1275 MODULE_LICENSE("GPL v2");