1 /* linux/arch/arm/plat-s3c/gpio.c
3 * Copyright 2008 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 * http://armlinux.simtec.co.uk/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
17 #include <linux/gpio.h>
19 #include <mach/gpio-core.h>
21 #ifdef CONFIG_S3C_GPIO_TRACK
22 struct s3c_gpio_chip
*s3c_gpios
[S3C_GPIO_END
];
24 static __init
void s3c_gpiolib_track(struct s3c_gpio_chip
*chip
)
29 gpn
= chip
->chip
.base
;
30 for (i
= 0; i
< chip
->chip
.ngpio
; i
++, gpn
++) {
31 BUG_ON(gpn
>= ARRAY_SIZE(s3c_gpios
));
32 s3c_gpios
[gpn
] = chip
;
35 #endif /* CONFIG_S3C_GPIO_TRACK */
37 /* Default routines for controlling GPIO, based on the original S3C24XX
38 * GPIO functions which deal with the case where each gpio bank of the
39 * chip is as following:
41 * base + 0x00: Control register, 2 bits per gpio
42 * gpio n: 2 bits starting at (2*n)
43 * 00 = input, 01 = output, others mean special-function
44 * base + 0x04: Data register, 1 bit per gpio
48 static int s3c_gpiolib_input(struct gpio_chip
*chip
, unsigned offset
)
50 struct s3c_gpio_chip
*ourchip
= to_s3c_gpio(chip
);
51 void __iomem
*base
= ourchip
->base
;
55 local_irq_save(flags
);
57 con
= __raw_readl(base
+ 0x00);
58 con
&= ~(3 << (offset
* 2));
60 __raw_writel(con
, base
+ 0x00);
62 local_irq_restore(flags
);
66 static int s3c_gpiolib_output(struct gpio_chip
*chip
,
67 unsigned offset
, int value
)
69 struct s3c_gpio_chip
*ourchip
= to_s3c_gpio(chip
);
70 void __iomem
*base
= ourchip
->base
;
75 local_irq_save(flags
);
77 dat
= __raw_readl(base
+ 0x04);
78 dat
&= ~(1 << offset
);
81 __raw_writel(dat
, base
+ 0x04);
83 con
= __raw_readl(base
+ 0x00);
84 con
&= ~(3 << (offset
* 2));
85 con
|= 1 << (offset
* 2);
87 __raw_writel(con
, base
+ 0x00);
88 __raw_writel(dat
, base
+ 0x04);
90 local_irq_restore(flags
);
94 static void s3c_gpiolib_set(struct gpio_chip
*chip
,
95 unsigned offset
, int value
)
97 struct s3c_gpio_chip
*ourchip
= to_s3c_gpio(chip
);
98 void __iomem
*base
= ourchip
->base
;
102 local_irq_save(flags
);
104 dat
= __raw_readl(base
+ 0x04);
105 dat
&= ~(1 << offset
);
108 __raw_writel(dat
, base
+ 0x04);
110 local_irq_restore(flags
);
113 static int s3c_gpiolib_get(struct gpio_chip
*chip
, unsigned offset
)
115 struct s3c_gpio_chip
*ourchip
= to_s3c_gpio(chip
);
118 val
= __raw_readl(ourchip
->base
+ 0x04);
125 __init
void s3c_gpiolib_add(struct s3c_gpio_chip
*chip
)
127 struct gpio_chip
*gc
= &chip
->chip
;
134 if (!gc
->direction_input
)
135 gc
->direction_input
= s3c_gpiolib_input
;
136 if (!gc
->direction_output
)
137 gc
->direction_output
= s3c_gpiolib_output
;
139 gc
->set
= s3c_gpiolib_set
;
141 gc
->get
= s3c_gpiolib_get
;
144 if (chip
->pm
!= NULL
) {
145 if (!chip
->pm
->save
|| !chip
->pm
->resume
)
146 printk(KERN_ERR
"gpio: %s has missing PM functions\n",
149 printk(KERN_ERR
"gpio: %s has no PM function\n", gc
->label
);
152 /* gpiochip_add() prints own failure message on error. */
153 ret
= gpiochip_add(gc
);
155 s3c_gpiolib_track(chip
);