2 * Loongson-2F/3A/3B GPIO Support
4 * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com>
5 * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
6 * Copyright (c) 2013 Hongbing Hu <huhb@lemote.com>
7 * Copyright (c) 2014 Huacai Chen <chenhc@lemote.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/err.h>
20 #include <asm/types.h>
22 #include <linux/gpio.h>
24 #define STLS2F_N_GPIO 4
25 #define STLS3A_N_GPIO 16
27 #ifdef CONFIG_CPU_LOONGSON3
28 #define LOONGSON_N_GPIO STLS3A_N_GPIO
30 #define LOONGSON_N_GPIO STLS2F_N_GPIO
33 #define LOONGSON_GPIO_IN_OFFSET 16
35 static DEFINE_SPINLOCK(gpio_lock);
37 static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
42 spin_lock(&gpio_lock);
44 temp = LOONGSON_GPIOIE;
46 LOONGSON_GPIOIE = temp;
47 spin_unlock(&gpio_lock);
52 static int loongson_gpio_direction_output(struct gpio_chip *chip,
53 unsigned gpio, int level)
58 gpio_set_value(gpio, level);
59 spin_lock(&gpio_lock);
61 temp = LOONGSON_GPIOIE;
63 LOONGSON_GPIOIE = temp;
64 spin_unlock(&gpio_lock);
69 static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
74 mask = 1 << (gpio + LOONGSON_GPIO_IN_OFFSET);
75 spin_lock(&gpio_lock);
76 val = LOONGSON_GPIODATA;
77 spin_unlock(&gpio_lock);
79 return (val & mask) != 0;
82 static void loongson_gpio_set_value(struct gpio_chip *chip,
83 unsigned gpio, int value)
90 spin_lock(&gpio_lock);
91 val = LOONGSON_GPIODATA;
96 LOONGSON_GPIODATA = val;
97 spin_unlock(&gpio_lock);
100 static struct gpio_chip loongson_chip = {
101 .label = "Loongson-gpio-chip",
102 .direction_input = loongson_gpio_direction_input,
103 .get = loongson_gpio_get_value,
104 .direction_output = loongson_gpio_direction_output,
105 .set = loongson_gpio_set_value,
107 .ngpio = LOONGSON_N_GPIO,
111 static int __init loongson_gpio_setup(void)
113 return gpiochip_add_data(&loongson_chip, NULL);
115 postcore_initcall(loongson_gpio_setup);