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 static int ls2f_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
32 spin_lock(&gpio_lock);
34 temp = LOONGSON_GPIOIE;
36 LOONGSON_GPIOIE = temp;
37 spin_unlock(&gpio_lock);
42 static int ls2f_gpio_direction_output(struct gpio_chip *chip,
43 unsigned gpio, int level)
48 gpio_set_value(gpio, level);
49 spin_lock(&gpio_lock);
51 temp = LOONGSON_GPIOIE;
53 LOONGSON_GPIOIE = temp;
54 spin_unlock(&gpio_lock);
59 static int ls2f_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
64 mask = 1 << (gpio + STLS2F_GPIO_IN_OFFSET);
65 spin_lock(&gpio_lock);
66 val = LOONGSON_GPIODATA;
67 spin_unlock(&gpio_lock);
69 return (val & mask) != 0;
72 static void ls2f_gpio_set_value(struct gpio_chip *chip,
73 unsigned gpio, int value)
80 spin_lock(&gpio_lock);
81 val = LOONGSON_GPIODATA;
86 LOONGSON_GPIODATA = val;
87 spin_unlock(&gpio_lock);
90 static struct gpio_chip ls2f_chip = {
92 .direction_input = ls2f_gpio_direction_input,
93 .get = ls2f_gpio_get_value,
94 .direction_output = ls2f_gpio_direction_output,
95 .set = ls2f_gpio_set_value,
97 .ngpio = STLS2F_N_GPIO,
101 static int __init ls2f_gpio_setup(void)
103 return gpiochip_add(&ls2f_chip);
105 arch_initcall(ls2f_gpio_setup);