MIPS: Cleanup Loongson-2F's gpio driver
[platform/kernel/linux-rpi.git] / drivers / gpio / gpio-loongson.c
1 /*
2  *  STLS2F GPIO Support
3  *
4  *  Copyright (c) 2008 Richard Liu,  STMicroelectronics  <richard.liu@st.com>
5  *  Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
6  *
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.
11  */
12
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>
19 #include <loongson.h>
20 #include <linux/gpio.h>
21
22 #define STLS2F_N_GPIO           4
23 #define STLS2F_GPIO_IN_OFFSET   16
24
25 static DEFINE_SPINLOCK(gpio_lock);
26
27 static int ls2f_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
28 {
29         u32 temp;
30         u32 mask;
31
32         spin_lock(&gpio_lock);
33         mask = 1 << gpio;
34         temp = LOONGSON_GPIOIE;
35         temp |= mask;
36         LOONGSON_GPIOIE = temp;
37         spin_unlock(&gpio_lock);
38
39         return 0;
40 }
41
42 static int ls2f_gpio_direction_output(struct gpio_chip *chip,
43                 unsigned gpio, int level)
44 {
45         u32 temp;
46         u32 mask;
47
48         gpio_set_value(gpio, level);
49         spin_lock(&gpio_lock);
50         mask = 1 << gpio;
51         temp = LOONGSON_GPIOIE;
52         temp &= (~mask);
53         LOONGSON_GPIOIE = temp;
54         spin_unlock(&gpio_lock);
55
56         return 0;
57 }
58
59 static int ls2f_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
60 {
61         u32 val;
62         u32 mask;
63
64         mask = 1 << (gpio + STLS2F_GPIO_IN_OFFSET);
65         spin_lock(&gpio_lock);
66         val = LOONGSON_GPIODATA;
67         spin_unlock(&gpio_lock);
68
69         return (val & mask) != 0;
70 }
71
72 static void ls2f_gpio_set_value(struct gpio_chip *chip,
73                 unsigned gpio, int value)
74 {
75         u32 val;
76         u32 mask;
77
78         mask = 1 << gpio;
79
80         spin_lock(&gpio_lock);
81         val = LOONGSON_GPIODATA;
82         if (value)
83                 val |= mask;
84         else
85                 val &= (~mask);
86         LOONGSON_GPIODATA = val;
87         spin_unlock(&gpio_lock);
88 }
89
90 static struct gpio_chip ls2f_chip = {
91         .label                  = "ls2f",
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,
96         .base                   = 0,
97         .ngpio                  = STLS2F_N_GPIO,
98         .can_sleep              = false,
99 };
100
101 static int __init ls2f_gpio_setup(void)
102 {
103         return gpiochip_add(&ls2f_chip);
104 }
105 arch_initcall(ls2f_gpio_setup);