4 * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com>
5 * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/err.h>
18 #include <asm/types.h>
20 #include <linux/gpio.h>
22 #define STLS2F_N_GPIO 4
23 #define STLS2F_GPIO_IN_OFFSET 16
25 static DEFINE_SPINLOCK(gpio_lock
);
27 int gpio_get_value(unsigned gpio
)
32 if (gpio
>= STLS2F_N_GPIO
)
33 return __gpio_get_value(gpio
);
35 mask
= 1 << (gpio
+ STLS2F_GPIO_IN_OFFSET
);
36 spin_lock(&gpio_lock
);
37 val
= LOONGSON_GPIODATA
;
38 spin_unlock(&gpio_lock
);
40 return ((val
& mask
) != 0);
42 EXPORT_SYMBOL(gpio_get_value
);
44 void gpio_set_value(unsigned gpio
, int state
)
49 if (gpio
>= STLS2F_N_GPIO
) {
50 __gpio_set_value(gpio
, state
);
56 spin_lock(&gpio_lock
);
57 val
= LOONGSON_GPIODATA
;
62 LOONGSON_GPIODATA
= val
;
63 spin_unlock(&gpio_lock
);
65 EXPORT_SYMBOL(gpio_set_value
);
67 int gpio_cansleep(unsigned gpio
)
69 if (gpio
< STLS2F_N_GPIO
)
72 return __gpio_cansleep(gpio
);
74 EXPORT_SYMBOL(gpio_cansleep
);
76 static int ls2f_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
81 if (gpio
>= STLS2F_N_GPIO
)
84 spin_lock(&gpio_lock
);
86 temp
= LOONGSON_GPIOIE
;
88 LOONGSON_GPIOIE
= temp
;
89 spin_unlock(&gpio_lock
);
94 static int ls2f_gpio_direction_output(struct gpio_chip
*chip
,
95 unsigned gpio
, int level
)
100 if (gpio
>= STLS2F_N_GPIO
)
103 gpio_set_value(gpio
, level
);
104 spin_lock(&gpio_lock
);
106 temp
= LOONGSON_GPIOIE
;
108 LOONGSON_GPIOIE
= temp
;
109 spin_unlock(&gpio_lock
);
114 static int ls2f_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
116 return gpio_get_value(gpio
);
119 static void ls2f_gpio_set_value(struct gpio_chip
*chip
,
120 unsigned gpio
, int value
)
122 gpio_set_value(gpio
, value
);
125 static struct gpio_chip ls2f_chip
= {
127 .direction_input
= ls2f_gpio_direction_input
,
128 .get
= ls2f_gpio_get_value
,
129 .direction_output
= ls2f_gpio_direction_output
,
130 .set
= ls2f_gpio_set_value
,
132 .ngpio
= STLS2F_N_GPIO
,
135 static int __init
ls2f_gpio_setup(void)
137 return gpiochip_add(&ls2f_chip
);
139 arch_initcall(ls2f_gpio_setup
);