2 * Copyright (c) 2009-2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com/
5 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
10 * SAMSUNG - GPIOlib support
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/irq.h>
20 #include <linux/gpio.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/device.h>
26 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/of_address.h>
34 #include <mach/regs-gpio.h>
35 #include <mach/gpio-samsung.h>
38 #include <plat/gpio-core.h>
39 #include <plat/gpio-cfg.h>
40 #include <plat/gpio-cfg-helpers.h>
43 int samsung_gpio_setpull_updown(struct samsung_gpio_chip
*chip
,
44 unsigned int off
, samsung_gpio_pull_t pull
)
46 void __iomem
*reg
= chip
->base
+ 0x08;
50 pup
= __raw_readl(reg
);
53 __raw_writel(pup
, reg
);
58 samsung_gpio_pull_t
samsung_gpio_getpull_updown(struct samsung_gpio_chip
*chip
,
61 void __iomem
*reg
= chip
->base
+ 0x08;
63 u32 pup
= __raw_readl(reg
);
68 return (__force samsung_gpio_pull_t
)pup
;
71 int s3c2443_gpio_setpull(struct samsung_gpio_chip
*chip
,
72 unsigned int off
, samsung_gpio_pull_t pull
)
75 case S3C_GPIO_PULL_NONE
:
78 case S3C_GPIO_PULL_UP
:
81 case S3C_GPIO_PULL_DOWN
:
85 return samsung_gpio_setpull_updown(chip
, off
, pull
);
88 samsung_gpio_pull_t
s3c2443_gpio_getpull(struct samsung_gpio_chip
*chip
,
91 samsung_gpio_pull_t pull
;
93 pull
= samsung_gpio_getpull_updown(chip
, off
);
97 pull
= S3C_GPIO_PULL_UP
;
101 pull
= S3C_GPIO_PULL_NONE
;
104 pull
= S3C_GPIO_PULL_DOWN
;
111 static int s3c24xx_gpio_setpull_1(struct samsung_gpio_chip
*chip
,
112 unsigned int off
, samsung_gpio_pull_t pull
,
113 samsung_gpio_pull_t updown
)
115 void __iomem
*reg
= chip
->base
+ 0x08;
116 u32 pup
= __raw_readl(reg
);
120 else if (pull
== S3C_GPIO_PULL_NONE
)
125 __raw_writel(pup
, reg
);
129 static samsung_gpio_pull_t
s3c24xx_gpio_getpull_1(struct samsung_gpio_chip
*chip
,
131 samsung_gpio_pull_t updown
)
133 void __iomem
*reg
= chip
->base
+ 0x08;
134 u32 pup
= __raw_readl(reg
);
137 return pup
? S3C_GPIO_PULL_NONE
: updown
;
140 samsung_gpio_pull_t
s3c24xx_gpio_getpull_1up(struct samsung_gpio_chip
*chip
,
143 return s3c24xx_gpio_getpull_1(chip
, off
, S3C_GPIO_PULL_UP
);
146 int s3c24xx_gpio_setpull_1up(struct samsung_gpio_chip
*chip
,
147 unsigned int off
, samsung_gpio_pull_t pull
)
149 return s3c24xx_gpio_setpull_1(chip
, off
, pull
, S3C_GPIO_PULL_UP
);
152 samsung_gpio_pull_t
s3c24xx_gpio_getpull_1down(struct samsung_gpio_chip
*chip
,
155 return s3c24xx_gpio_getpull_1(chip
, off
, S3C_GPIO_PULL_DOWN
);
158 int s3c24xx_gpio_setpull_1down(struct samsung_gpio_chip
*chip
,
159 unsigned int off
, samsung_gpio_pull_t pull
)
161 return s3c24xx_gpio_setpull_1(chip
, off
, pull
, S3C_GPIO_PULL_DOWN
);
165 * samsung_gpio_setcfg_2bit - Samsung 2bit style GPIO configuration.
166 * @chip: The gpio chip that is being configured.
167 * @off: The offset for the GPIO being configured.
168 * @cfg: The configuration value to set.
170 * This helper deal with the GPIO cases where the control register
171 * has two bits of configuration per gpio, which have the following
175 * 1x = special function
178 static int samsung_gpio_setcfg_2bit(struct samsung_gpio_chip
*chip
,
179 unsigned int off
, unsigned int cfg
)
181 void __iomem
*reg
= chip
->base
;
182 unsigned int shift
= off
* 2;
185 if (samsung_gpio_is_cfg_special(cfg
)) {
193 con
= __raw_readl(reg
);
194 con
&= ~(0x3 << shift
);
196 __raw_writel(con
, reg
);
202 * samsung_gpio_getcfg_2bit - Samsung 2bit style GPIO configuration read.
203 * @chip: The gpio chip that is being configured.
204 * @off: The offset for the GPIO being configured.
206 * The reverse of samsung_gpio_setcfg_2bit(). Will return a value which
207 * could be directly passed back to samsung_gpio_setcfg_2bit(), from the
208 * S3C_GPIO_SPECIAL() macro.
211 static unsigned int samsung_gpio_getcfg_2bit(struct samsung_gpio_chip
*chip
,
216 con
= __raw_readl(chip
->base
);
220 /* this conversion works for IN and OUT as well as special mode */
221 return S3C_GPIO_SPECIAL(con
);
225 * samsung_gpio_setcfg_4bit - Samsung 4bit single register GPIO config.
226 * @chip: The gpio chip that is being configured.
227 * @off: The offset for the GPIO being configured.
228 * @cfg: The configuration value to set.
230 * This helper deal with the GPIO cases where the control register has 4 bits
231 * of control per GPIO, generally in the form of:
234 * others = Special functions (dependent on bank)
236 * Note, since the code to deal with the case where there are two control
237 * registers instead of one, we do not have a separate set of functions for
241 static int samsung_gpio_setcfg_4bit(struct samsung_gpio_chip
*chip
,
242 unsigned int off
, unsigned int cfg
)
244 void __iomem
*reg
= chip
->base
;
245 unsigned int shift
= (off
& 7) * 4;
248 if (off
< 8 && chip
->chip
.ngpio
> 8)
251 if (samsung_gpio_is_cfg_special(cfg
)) {
256 con
= __raw_readl(reg
);
257 con
&= ~(0xf << shift
);
259 __raw_writel(con
, reg
);
265 * samsung_gpio_getcfg_4bit - Samsung 4bit single register GPIO config read.
266 * @chip: The gpio chip that is being configured.
267 * @off: The offset for the GPIO being configured.
269 * The reverse of samsung_gpio_setcfg_4bit(), turning a gpio configuration
270 * register setting into a value the software can use, such as could be passed
271 * to samsung_gpio_setcfg_4bit().
273 * @sa samsung_gpio_getcfg_2bit
276 static unsigned samsung_gpio_getcfg_4bit(struct samsung_gpio_chip
*chip
,
279 void __iomem
*reg
= chip
->base
;
280 unsigned int shift
= (off
& 7) * 4;
283 if (off
< 8 && chip
->chip
.ngpio
> 8)
286 con
= __raw_readl(reg
);
290 /* this conversion works for IN and OUT as well as special mode */
291 return S3C_GPIO_SPECIAL(con
);
294 #ifdef CONFIG_PLAT_S3C24XX
296 * s3c24xx_gpio_setcfg_abank - S3C24XX style GPIO configuration (Bank A)
297 * @chip: The gpio chip that is being configured.
298 * @off: The offset for the GPIO being configured.
299 * @cfg: The configuration value to set.
301 * This helper deal with the GPIO cases where the control register
302 * has one bit of configuration for the gpio, where setting the bit
303 * means the pin is in special function mode and unset means output.
306 static int s3c24xx_gpio_setcfg_abank(struct samsung_gpio_chip
*chip
,
307 unsigned int off
, unsigned int cfg
)
309 void __iomem
*reg
= chip
->base
;
310 unsigned int shift
= off
;
313 if (samsung_gpio_is_cfg_special(cfg
)) {
316 /* Map output to 0, and SFN2 to 1 */
324 con
= __raw_readl(reg
);
325 con
&= ~(0x1 << shift
);
327 __raw_writel(con
, reg
);
333 * s3c24xx_gpio_getcfg_abank - S3C24XX style GPIO configuration read (Bank A)
334 * @chip: The gpio chip that is being configured.
335 * @off: The offset for the GPIO being configured.
337 * The reverse of s3c24xx_gpio_setcfg_abank() turning an GPIO into a usable
338 * GPIO configuration value.
340 * @sa samsung_gpio_getcfg_2bit
341 * @sa samsung_gpio_getcfg_4bit
344 static unsigned s3c24xx_gpio_getcfg_abank(struct samsung_gpio_chip
*chip
,
349 con
= __raw_readl(chip
->base
);
354 return S3C_GPIO_SFN(con
);
358 static void __init
samsung_gpiolib_set_cfg(struct samsung_gpio_cfg
*chipcfg
,
361 for (; nr_chips
> 0; nr_chips
--, chipcfg
++) {
362 if (!chipcfg
->set_config
)
363 chipcfg
->set_config
= samsung_gpio_setcfg_4bit
;
364 if (!chipcfg
->get_config
)
365 chipcfg
->get_config
= samsung_gpio_getcfg_4bit
;
366 if (!chipcfg
->set_pull
)
367 chipcfg
->set_pull
= samsung_gpio_setpull_updown
;
368 if (!chipcfg
->get_pull
)
369 chipcfg
->get_pull
= samsung_gpio_getpull_updown
;
373 struct samsung_gpio_cfg s3c24xx_gpiocfg_default
= {
374 .set_config
= samsung_gpio_setcfg_2bit
,
375 .get_config
= samsung_gpio_getcfg_2bit
,
378 #ifdef CONFIG_PLAT_S3C24XX
379 static struct samsung_gpio_cfg s3c24xx_gpiocfg_banka
= {
380 .set_config
= s3c24xx_gpio_setcfg_abank
,
381 .get_config
= s3c24xx_gpio_getcfg_abank
,
385 static struct samsung_gpio_cfg samsung_gpio_cfgs
[] = {
400 .set_config
= samsung_gpio_setcfg_2bit
,
401 .get_config
= samsung_gpio_getcfg_2bit
,
405 .set_config
= samsung_gpio_setcfg_2bit
,
406 .get_config
= samsung_gpio_getcfg_2bit
,
410 .set_config
= samsung_gpio_setcfg_2bit
,
411 .get_config
= samsung_gpio_getcfg_2bit
,
414 .set_config
= samsung_gpio_setcfg_2bit
,
415 .get_config
= samsung_gpio_getcfg_2bit
,
420 * Default routines for controlling GPIO, based on the original S3C24XX
421 * GPIO functions which deal with the case where each gpio bank of the
422 * chip is as following:
424 * base + 0x00: Control register, 2 bits per gpio
425 * gpio n: 2 bits starting at (2*n)
426 * 00 = input, 01 = output, others mean special-function
427 * base + 0x04: Data register, 1 bit per gpio
431 static int samsung_gpiolib_2bit_input(struct gpio_chip
*chip
, unsigned offset
)
433 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
434 void __iomem
*base
= ourchip
->base
;
438 samsung_gpio_lock(ourchip
, flags
);
440 con
= __raw_readl(base
+ 0x00);
441 con
&= ~(3 << (offset
* 2));
443 __raw_writel(con
, base
+ 0x00);
445 samsung_gpio_unlock(ourchip
, flags
);
449 static int samsung_gpiolib_2bit_output(struct gpio_chip
*chip
,
450 unsigned offset
, int value
)
452 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
453 void __iomem
*base
= ourchip
->base
;
458 samsung_gpio_lock(ourchip
, flags
);
460 dat
= __raw_readl(base
+ 0x04);
461 dat
&= ~(1 << offset
);
464 __raw_writel(dat
, base
+ 0x04);
466 con
= __raw_readl(base
+ 0x00);
467 con
&= ~(3 << (offset
* 2));
468 con
|= 1 << (offset
* 2);
470 __raw_writel(con
, base
+ 0x00);
471 __raw_writel(dat
, base
+ 0x04);
473 samsung_gpio_unlock(ourchip
, flags
);
478 * The samsung_gpiolib_4bit routines are to control the gpio banks where
479 * the gpio configuration register (GPxCON) has 4 bits per GPIO, as the
482 * base + 0x00: Control register, 4 bits per gpio
483 * gpio n: 4 bits starting at (4*n)
484 * 0000 = input, 0001 = output, others mean special-function
485 * base + 0x04: Data register, 1 bit per gpio
488 * Note, since the data register is one bit per gpio and is at base + 0x4
489 * we can use samsung_gpiolib_get and samsung_gpiolib_set to change the
490 * state of the output.
493 static int samsung_gpiolib_4bit_input(struct gpio_chip
*chip
,
496 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
497 void __iomem
*base
= ourchip
->base
;
500 con
= __raw_readl(base
+ GPIOCON_OFF
);
501 if (ourchip
->bitmap_gpio_int
& BIT(offset
))
502 con
|= 0xf << con_4bit_shift(offset
);
504 con
&= ~(0xf << con_4bit_shift(offset
));
505 __raw_writel(con
, base
+ GPIOCON_OFF
);
507 pr_debug("%s: %p: CON now %08lx\n", __func__
, base
, con
);
512 static int samsung_gpiolib_4bit_output(struct gpio_chip
*chip
,
513 unsigned int offset
, int value
)
515 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
516 void __iomem
*base
= ourchip
->base
;
520 con
= __raw_readl(base
+ GPIOCON_OFF
);
521 con
&= ~(0xf << con_4bit_shift(offset
));
522 con
|= 0x1 << con_4bit_shift(offset
);
524 dat
= __raw_readl(base
+ GPIODAT_OFF
);
529 dat
&= ~(1 << offset
);
531 __raw_writel(dat
, base
+ GPIODAT_OFF
);
532 __raw_writel(con
, base
+ GPIOCON_OFF
);
533 __raw_writel(dat
, base
+ GPIODAT_OFF
);
535 pr_debug("%s: %p: CON %08lx, DAT %08lx\n", __func__
, base
, con
, dat
);
541 * The next set of routines are for the case where the GPIO configuration
542 * registers are 4 bits per GPIO but there is more than one register (the
543 * bank has more than 8 GPIOs.
545 * This case is the similar to the 4 bit case, but the registers are as
548 * base + 0x00: Control register, 4 bits per gpio (lower 8 GPIOs)
549 * gpio n: 4 bits starting at (4*n)
550 * 0000 = input, 0001 = output, others mean special-function
551 * base + 0x04: Control register, 4 bits per gpio (up to 8 additions GPIOs)
552 * gpio n: 4 bits starting at (4*n)
553 * 0000 = input, 0001 = output, others mean special-function
554 * base + 0x08: Data register, 1 bit per gpio
557 * To allow us to use the samsung_gpiolib_get and samsung_gpiolib_set
558 * routines we store the 'base + 0x4' address so that these routines see
559 * the data register at ourchip->base + 0x04.
562 static int samsung_gpiolib_4bit2_input(struct gpio_chip
*chip
,
565 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
566 void __iomem
*base
= ourchip
->base
;
567 void __iomem
*regcon
= base
;
575 con
= __raw_readl(regcon
);
576 con
&= ~(0xf << con_4bit_shift(offset
));
577 __raw_writel(con
, regcon
);
579 pr_debug("%s: %p: CON %08lx\n", __func__
, base
, con
);
584 static int samsung_gpiolib_4bit2_output(struct gpio_chip
*chip
,
585 unsigned int offset
, int value
)
587 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
588 void __iomem
*base
= ourchip
->base
;
589 void __iomem
*regcon
= base
;
592 unsigned con_offset
= offset
;
599 con
= __raw_readl(regcon
);
600 con
&= ~(0xf << con_4bit_shift(con_offset
));
601 con
|= 0x1 << con_4bit_shift(con_offset
);
603 dat
= __raw_readl(base
+ GPIODAT_OFF
);
608 dat
&= ~(1 << offset
);
610 __raw_writel(dat
, base
+ GPIODAT_OFF
);
611 __raw_writel(con
, regcon
);
612 __raw_writel(dat
, base
+ GPIODAT_OFF
);
614 pr_debug("%s: %p: CON %08lx, DAT %08lx\n", __func__
, base
, con
, dat
);
619 #ifdef CONFIG_PLAT_S3C24XX
620 /* The next set of routines are for the case of s3c24xx bank a */
622 static int s3c24xx_gpiolib_banka_input(struct gpio_chip
*chip
, unsigned offset
)
627 static int s3c24xx_gpiolib_banka_output(struct gpio_chip
*chip
,
628 unsigned offset
, int value
)
630 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
631 void __iomem
*base
= ourchip
->base
;
636 local_irq_save(flags
);
638 con
= __raw_readl(base
+ 0x00);
639 dat
= __raw_readl(base
+ 0x04);
641 dat
&= ~(1 << offset
);
645 __raw_writel(dat
, base
+ 0x04);
647 con
&= ~(1 << offset
);
649 __raw_writel(con
, base
+ 0x00);
650 __raw_writel(dat
, base
+ 0x04);
652 local_irq_restore(flags
);
657 static void samsung_gpiolib_set(struct gpio_chip
*chip
,
658 unsigned offset
, int value
)
660 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
661 void __iomem
*base
= ourchip
->base
;
665 samsung_gpio_lock(ourchip
, flags
);
667 dat
= __raw_readl(base
+ 0x04);
668 dat
&= ~(1 << offset
);
671 __raw_writel(dat
, base
+ 0x04);
673 samsung_gpio_unlock(ourchip
, flags
);
676 static int samsung_gpiolib_get(struct gpio_chip
*chip
, unsigned offset
)
678 struct samsung_gpio_chip
*ourchip
= to_samsung_gpio(chip
);
681 val
= __raw_readl(ourchip
->base
+ 0x04);
689 * CONFIG_S3C_GPIO_TRACK enables the tracking of the s3c specific gpios
690 * for use with the configuration calls, and other parts of the s3c gpiolib
693 * Not all s3c support code will need this, as some configurations of cpu
694 * may only support one or two different configuration options and have an
695 * easy gpio to samsung_gpio_chip mapping function. If this is the case, then
696 * the machine support file should provide its own samsung_gpiolib_getchip()
697 * and any other necessary functions.
700 #ifdef CONFIG_S3C_GPIO_TRACK
701 struct samsung_gpio_chip
*s3c_gpios
[S3C_GPIO_END
];
703 static __init
void s3c_gpiolib_track(struct samsung_gpio_chip
*chip
)
708 gpn
= chip
->chip
.base
;
709 for (i
= 0; i
< chip
->chip
.ngpio
; i
++, gpn
++) {
710 BUG_ON(gpn
>= ARRAY_SIZE(s3c_gpios
));
711 s3c_gpios
[gpn
] = chip
;
714 #endif /* CONFIG_S3C_GPIO_TRACK */
717 * samsung_gpiolib_add() - add the Samsung gpio_chip.
718 * @chip: The chip to register
720 * This is a wrapper to gpiochip_add() that takes our specific gpio chip
721 * information and makes the necessary alterations for the platform and
722 * notes the information for use with the configuration systems and any
723 * other parts of the system.
726 static void __init
samsung_gpiolib_add(struct samsung_gpio_chip
*chip
)
728 struct gpio_chip
*gc
= &chip
->chip
;
735 spin_lock_init(&chip
->lock
);
737 if (!gc
->direction_input
)
738 gc
->direction_input
= samsung_gpiolib_2bit_input
;
739 if (!gc
->direction_output
)
740 gc
->direction_output
= samsung_gpiolib_2bit_output
;
742 gc
->set
= samsung_gpiolib_set
;
744 gc
->get
= samsung_gpiolib_get
;
747 if (chip
->pm
!= NULL
) {
748 if (!chip
->pm
->save
|| !chip
->pm
->resume
)
749 pr_err("gpio: %s has missing PM functions\n",
752 pr_err("gpio: %s has no PM function\n", gc
->label
);
755 /* gpiochip_add() prints own failure message on error. */
756 ret
= gpiochip_add(gc
);
758 s3c_gpiolib_track(chip
);
761 static void __init
s3c24xx_gpiolib_add_chips(struct samsung_gpio_chip
*chip
,
762 int nr_chips
, void __iomem
*base
)
765 struct gpio_chip
*gc
= &chip
->chip
;
767 for (i
= 0 ; i
< nr_chips
; i
++, chip
++) {
768 /* skip banks not present on SoC */
769 if (chip
->chip
.base
>= S3C_GPIO_END
)
773 chip
->config
= &s3c24xx_gpiocfg_default
;
775 chip
->pm
= __gpio_pm(&samsung_gpio_pm_2bit
);
776 if ((base
!= NULL
) && (chip
->base
== NULL
))
777 chip
->base
= base
+ ((i
) * 0x10);
779 if (!gc
->direction_input
)
780 gc
->direction_input
= samsung_gpiolib_2bit_input
;
781 if (!gc
->direction_output
)
782 gc
->direction_output
= samsung_gpiolib_2bit_output
;
784 samsung_gpiolib_add(chip
);
788 static void __init
samsung_gpiolib_add_2bit_chips(struct samsung_gpio_chip
*chip
,
789 int nr_chips
, void __iomem
*base
,
794 for (i
= 0 ; i
< nr_chips
; i
++, chip
++) {
795 chip
->chip
.direction_input
= samsung_gpiolib_2bit_input
;
796 chip
->chip
.direction_output
= samsung_gpiolib_2bit_output
;
799 chip
->config
= &samsung_gpio_cfgs
[7];
801 chip
->pm
= __gpio_pm(&samsung_gpio_pm_2bit
);
802 if ((base
!= NULL
) && (chip
->base
== NULL
))
803 chip
->base
= base
+ ((i
) * offset
);
805 samsung_gpiolib_add(chip
);
810 * samsung_gpiolib_add_4bit_chips - 4bit single register GPIO config.
811 * @chip: The gpio chip that is being configured.
812 * @nr_chips: The no of chips (gpio ports) for the GPIO being configured.
814 * This helper deal with the GPIO cases where the control register has 4 bits
815 * of control per GPIO, generally in the form of:
818 * others = Special functions (dependent on bank)
820 * Note, since the code to deal with the case where there are two control
821 * registers instead of one, we do not have a separate set of function
822 * (samsung_gpiolib_add_4bit2_chips)for each case.
825 static void __init
samsung_gpiolib_add_4bit_chips(struct samsung_gpio_chip
*chip
,
826 int nr_chips
, void __iomem
*base
)
830 for (i
= 0 ; i
< nr_chips
; i
++, chip
++) {
831 chip
->chip
.direction_input
= samsung_gpiolib_4bit_input
;
832 chip
->chip
.direction_output
= samsung_gpiolib_4bit_output
;
835 chip
->config
= &samsung_gpio_cfgs
[2];
837 chip
->pm
= __gpio_pm(&samsung_gpio_pm_4bit
);
838 if ((base
!= NULL
) && (chip
->base
== NULL
))
839 chip
->base
= base
+ ((i
) * 0x20);
841 chip
->bitmap_gpio_int
= 0;
843 samsung_gpiolib_add(chip
);
847 static void __init
samsung_gpiolib_add_4bit2_chips(struct samsung_gpio_chip
*chip
,
850 for (; nr_chips
> 0; nr_chips
--, chip
++) {
851 chip
->chip
.direction_input
= samsung_gpiolib_4bit2_input
;
852 chip
->chip
.direction_output
= samsung_gpiolib_4bit2_output
;
855 chip
->config
= &samsung_gpio_cfgs
[2];
857 chip
->pm
= __gpio_pm(&samsung_gpio_pm_4bit
);
859 samsung_gpiolib_add(chip
);
863 int samsung_gpiolib_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
865 struct samsung_gpio_chip
*samsung_chip
= container_of(chip
, struct samsung_gpio_chip
, chip
);
867 return samsung_chip
->irq_base
+ offset
;
870 #ifdef CONFIG_PLAT_S3C24XX
871 static int s3c24xx_gpiolib_fbank_to_irq(struct gpio_chip
*chip
, unsigned offset
)
874 if (soc_is_s3c2412())
875 return IRQ_EINT0_2412
+ offset
;
877 return IRQ_EINT0
+ offset
;
881 return IRQ_EINT4
+ offset
- 4;
887 #ifdef CONFIG_ARCH_S3C64XX
888 static int s3c64xx_gpiolib_mbank_to_irq(struct gpio_chip
*chip
, unsigned pin
)
890 return pin
< 5 ? IRQ_EINT(23) + pin
: -ENXIO
;
893 static int s3c64xx_gpiolib_lbank_to_irq(struct gpio_chip
*chip
, unsigned pin
)
895 return pin
>= 8 ? IRQ_EINT(16) + pin
- 8 : -ENXIO
;
899 struct samsung_gpio_chip s3c24xx_gpios
[] = {
900 #ifdef CONFIG_PLAT_S3C24XX
902 .config
= &s3c24xx_gpiocfg_banka
,
904 .base
= S3C2410_GPA(0),
905 .owner
= THIS_MODULE
,
908 .direction_input
= s3c24xx_gpiolib_banka_input
,
909 .direction_output
= s3c24xx_gpiolib_banka_output
,
913 .base
= S3C2410_GPB(0),
914 .owner
= THIS_MODULE
,
920 .base
= S3C2410_GPC(0),
921 .owner
= THIS_MODULE
,
927 .base
= S3C2410_GPD(0),
928 .owner
= THIS_MODULE
,
934 .base
= S3C2410_GPE(0),
936 .owner
= THIS_MODULE
,
941 .base
= S3C2410_GPF(0),
942 .owner
= THIS_MODULE
,
945 .to_irq
= s3c24xx_gpiolib_fbank_to_irq
,
948 .irq_base
= IRQ_EINT8
,
950 .base
= S3C2410_GPG(0),
951 .owner
= THIS_MODULE
,
954 .to_irq
= samsung_gpiolib_to_irq
,
958 .base
= S3C2410_GPH(0),
959 .owner
= THIS_MODULE
,
964 /* GPIOS for the S3C2443 and later devices. */
966 .base
= S3C2440_GPJCON
,
968 .base
= S3C2410_GPJ(0),
969 .owner
= THIS_MODULE
,
974 .base
= S3C2443_GPKCON
,
976 .base
= S3C2410_GPK(0),
977 .owner
= THIS_MODULE
,
982 .base
= S3C2443_GPLCON
,
984 .base
= S3C2410_GPL(0),
985 .owner
= THIS_MODULE
,
990 .base
= S3C2443_GPMCON
,
992 .base
= S3C2410_GPM(0),
993 .owner
= THIS_MODULE
,
1002 * GPIO bank summary:
1004 * Bank GPIOs Style SlpCon ExtInt Group
1010 * F 16 2Bit Yes 4 [1]
1012 * H 10 4Bit[2] Yes 6
1013 * I 16 2Bit Yes None
1014 * J 12 2Bit Yes None
1015 * K 16 4Bit[2] No None
1016 * L 15 4Bit[2] No None
1017 * M 6 4Bit No IRQ_EINT
1018 * N 16 2Bit No IRQ_EINT
1023 * [1] BANKF pins 14,15 do not form part of the external interrupt sources
1024 * [2] BANK has two control registers, GPxCON0 and GPxCON1
1027 static struct samsung_gpio_chip s3c64xx_gpios_4bit
[] = {
1028 #ifdef CONFIG_ARCH_S3C64XX
1031 .base
= S3C64XX_GPA(0),
1032 .ngpio
= S3C64XX_GPIO_A_NR
,
1037 .base
= S3C64XX_GPB(0),
1038 .ngpio
= S3C64XX_GPIO_B_NR
,
1043 .base
= S3C64XX_GPC(0),
1044 .ngpio
= S3C64XX_GPIO_C_NR
,
1049 .base
= S3C64XX_GPD(0),
1050 .ngpio
= S3C64XX_GPIO_D_NR
,
1054 .config
= &samsung_gpio_cfgs
[0],
1056 .base
= S3C64XX_GPE(0),
1057 .ngpio
= S3C64XX_GPIO_E_NR
,
1061 .base
= S3C64XX_GPG_BASE
,
1063 .base
= S3C64XX_GPG(0),
1064 .ngpio
= S3C64XX_GPIO_G_NR
,
1068 .base
= S3C64XX_GPM_BASE
,
1069 .config
= &samsung_gpio_cfgs
[1],
1071 .base
= S3C64XX_GPM(0),
1072 .ngpio
= S3C64XX_GPIO_M_NR
,
1074 .to_irq
= s3c64xx_gpiolib_mbank_to_irq
,
1080 static struct samsung_gpio_chip s3c64xx_gpios_4bit2
[] = {
1081 #ifdef CONFIG_ARCH_S3C64XX
1083 .base
= S3C64XX_GPH_BASE
+ 0x4,
1085 .base
= S3C64XX_GPH(0),
1086 .ngpio
= S3C64XX_GPIO_H_NR
,
1090 .base
= S3C64XX_GPK_BASE
+ 0x4,
1091 .config
= &samsung_gpio_cfgs
[0],
1093 .base
= S3C64XX_GPK(0),
1094 .ngpio
= S3C64XX_GPIO_K_NR
,
1098 .base
= S3C64XX_GPL_BASE
+ 0x4,
1099 .config
= &samsung_gpio_cfgs
[1],
1101 .base
= S3C64XX_GPL(0),
1102 .ngpio
= S3C64XX_GPIO_L_NR
,
1104 .to_irq
= s3c64xx_gpiolib_lbank_to_irq
,
1110 static struct samsung_gpio_chip s3c64xx_gpios_2bit
[] = {
1111 #ifdef CONFIG_ARCH_S3C64XX
1113 .base
= S3C64XX_GPF_BASE
,
1114 .config
= &samsung_gpio_cfgs
[6],
1116 .base
= S3C64XX_GPF(0),
1117 .ngpio
= S3C64XX_GPIO_F_NR
,
1121 .config
= &samsung_gpio_cfgs
[7],
1123 .base
= S3C64XX_GPI(0),
1124 .ngpio
= S3C64XX_GPIO_I_NR
,
1128 .config
= &samsung_gpio_cfgs
[7],
1130 .base
= S3C64XX_GPJ(0),
1131 .ngpio
= S3C64XX_GPIO_J_NR
,
1135 .config
= &samsung_gpio_cfgs
[6],
1137 .base
= S3C64XX_GPO(0),
1138 .ngpio
= S3C64XX_GPIO_O_NR
,
1142 .config
= &samsung_gpio_cfgs
[6],
1144 .base
= S3C64XX_GPP(0),
1145 .ngpio
= S3C64XX_GPIO_P_NR
,
1149 .config
= &samsung_gpio_cfgs
[6],
1151 .base
= S3C64XX_GPQ(0),
1152 .ngpio
= S3C64XX_GPIO_Q_NR
,
1156 .base
= S3C64XX_GPN_BASE
,
1157 .irq_base
= IRQ_EINT(0),
1158 .config
= &samsung_gpio_cfgs
[5],
1160 .base
= S3C64XX_GPN(0),
1161 .ngpio
= S3C64XX_GPIO_N_NR
,
1163 .to_irq
= samsung_gpiolib_to_irq
,
1169 /* TODO: cleanup soc_is_* */
1170 static __init
int samsung_gpiolib_init(void)
1173 * Currently there are two drivers that can provide GPIO support for
1174 * Samsung SoCs. For device tree enabled platforms, the new
1175 * pinctrl-samsung driver is used, providing both GPIO and pin control
1176 * interfaces. For legacy (non-DT) platforms this driver is used.
1178 if (of_have_populated_dt())
1181 samsung_gpiolib_set_cfg(samsung_gpio_cfgs
, ARRAY_SIZE(samsung_gpio_cfgs
));
1183 if (soc_is_s3c24xx()) {
1184 s3c24xx_gpiolib_add_chips(s3c24xx_gpios
,
1185 ARRAY_SIZE(s3c24xx_gpios
), S3C24XX_VA_GPIO
);
1186 } else if (soc_is_s3c64xx()) {
1187 samsung_gpiolib_add_2bit_chips(s3c64xx_gpios_2bit
,
1188 ARRAY_SIZE(s3c64xx_gpios_2bit
),
1189 S3C64XX_VA_GPIO
+ 0xE0, 0x20);
1190 samsung_gpiolib_add_4bit_chips(s3c64xx_gpios_4bit
,
1191 ARRAY_SIZE(s3c64xx_gpios_4bit
),
1193 samsung_gpiolib_add_4bit2_chips(s3c64xx_gpios_4bit2
,
1194 ARRAY_SIZE(s3c64xx_gpios_4bit2
));
1196 WARN(1, "Unknown SoC in gpio-samsung, no GPIOs added\n");
1202 core_initcall(samsung_gpiolib_init
);
1204 int s3c_gpio_cfgpin(unsigned int pin
, unsigned int config
)
1206 struct samsung_gpio_chip
*chip
= samsung_gpiolib_getchip(pin
);
1207 unsigned long flags
;
1214 offset
= pin
- chip
->chip
.base
;
1216 samsung_gpio_lock(chip
, flags
);
1217 ret
= samsung_gpio_do_setcfg(chip
, offset
, config
);
1218 samsung_gpio_unlock(chip
, flags
);
1222 EXPORT_SYMBOL(s3c_gpio_cfgpin
);
1224 int s3c_gpio_cfgpin_range(unsigned int start
, unsigned int nr
,
1229 for (; nr
> 0; nr
--, start
++) {
1230 ret
= s3c_gpio_cfgpin(start
, cfg
);
1237 EXPORT_SYMBOL_GPL(s3c_gpio_cfgpin_range
);
1239 int s3c_gpio_cfgall_range(unsigned int start
, unsigned int nr
,
1240 unsigned int cfg
, samsung_gpio_pull_t pull
)
1244 for (; nr
> 0; nr
--, start
++) {
1245 s3c_gpio_setpull(start
, pull
);
1246 ret
= s3c_gpio_cfgpin(start
, cfg
);
1253 EXPORT_SYMBOL_GPL(s3c_gpio_cfgall_range
);
1255 unsigned s3c_gpio_getcfg(unsigned int pin
)
1257 struct samsung_gpio_chip
*chip
= samsung_gpiolib_getchip(pin
);
1258 unsigned long flags
;
1263 offset
= pin
- chip
->chip
.base
;
1265 samsung_gpio_lock(chip
, flags
);
1266 ret
= samsung_gpio_do_getcfg(chip
, offset
);
1267 samsung_gpio_unlock(chip
, flags
);
1272 EXPORT_SYMBOL(s3c_gpio_getcfg
);
1274 int s3c_gpio_setpull(unsigned int pin
, samsung_gpio_pull_t pull
)
1276 struct samsung_gpio_chip
*chip
= samsung_gpiolib_getchip(pin
);
1277 unsigned long flags
;
1283 offset
= pin
- chip
->chip
.base
;
1285 samsung_gpio_lock(chip
, flags
);
1286 ret
= samsung_gpio_do_setpull(chip
, offset
, pull
);
1287 samsung_gpio_unlock(chip
, flags
);
1291 EXPORT_SYMBOL(s3c_gpio_setpull
);
1293 samsung_gpio_pull_t
s3c_gpio_getpull(unsigned int pin
)
1295 struct samsung_gpio_chip
*chip
= samsung_gpiolib_getchip(pin
);
1296 unsigned long flags
;
1301 offset
= pin
- chip
->chip
.base
;
1303 samsung_gpio_lock(chip
, flags
);
1304 pup
= samsung_gpio_do_getpull(chip
, offset
);
1305 samsung_gpio_unlock(chip
, flags
);
1308 return (__force samsung_gpio_pull_t
)pup
;
1310 EXPORT_SYMBOL(s3c_gpio_getpull
);
1312 #ifdef CONFIG_PLAT_S3C24XX
1313 unsigned int s3c2410_modify_misccr(unsigned int clear
, unsigned int change
)
1315 unsigned long flags
;
1316 unsigned long misccr
;
1318 local_irq_save(flags
);
1319 misccr
= __raw_readl(S3C24XX_MISCCR
);
1322 __raw_writel(misccr
, S3C24XX_MISCCR
);
1323 local_irq_restore(flags
);
1327 EXPORT_SYMBOL(s3c2410_modify_misccr
);