1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Loongson-2F/3A/3B GPIO Support
5 * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com>
6 * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
7 * Copyright (c) 2013 Hongbing Hu <huhb@lemote.com>
8 * Copyright (c) 2014 Huacai Chen <chenhc@lemote.com>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/err.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/platform_device.h>
18 #include <linux/bitops.h>
19 #include <asm/types.h>
22 #define STLS2F_N_GPIO 4
23 #define STLS3A_N_GPIO 16
25 #ifdef CONFIG_CPU_LOONGSON64
26 #define LOONGSON_N_GPIO STLS3A_N_GPIO
28 #define LOONGSON_N_GPIO STLS2F_N_GPIO
32 * Offset into the register where we read lines, we write them from offset 0.
33 * This offset is the only thing that stand between us and using
36 #define LOONGSON_GPIO_IN_OFFSET 16
38 static DEFINE_SPINLOCK(gpio_lock);
40 static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
44 spin_lock(&gpio_lock);
45 val = LOONGSON_GPIODATA;
46 spin_unlock(&gpio_lock);
48 return !!(val & BIT(gpio + LOONGSON_GPIO_IN_OFFSET));
51 static void loongson_gpio_set_value(struct gpio_chip *chip,
52 unsigned gpio, int value)
56 spin_lock(&gpio_lock);
57 val = LOONGSON_GPIODATA;
62 LOONGSON_GPIODATA = val;
63 spin_unlock(&gpio_lock);
66 static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
70 spin_lock(&gpio_lock);
71 temp = LOONGSON_GPIOIE;
73 LOONGSON_GPIOIE = temp;
74 spin_unlock(&gpio_lock);
79 static int loongson_gpio_direction_output(struct gpio_chip *chip,
80 unsigned gpio, int level)
84 loongson_gpio_set_value(chip, gpio, level);
85 spin_lock(&gpio_lock);
86 temp = LOONGSON_GPIOIE;
88 LOONGSON_GPIOIE = temp;
89 spin_unlock(&gpio_lock);
94 static int loongson_gpio_probe(struct platform_device *pdev)
97 struct device *dev = &pdev->dev;
99 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
103 gc->label = "loongson-gpio-chip";
105 gc->ngpio = LOONGSON_N_GPIO;
106 gc->get = loongson_gpio_get_value;
107 gc->set = loongson_gpio_set_value;
108 gc->direction_input = loongson_gpio_direction_input;
109 gc->direction_output = loongson_gpio_direction_output;
111 return gpiochip_add_data(gc, NULL);
114 static struct platform_driver loongson_gpio_driver = {
116 .name = "loongson-gpio",
118 .probe = loongson_gpio_probe,
121 static int __init loongson_gpio_setup(void)
123 struct platform_device *pdev;
126 ret = platform_driver_register(&loongson_gpio_driver);
128 pr_err("error registering loongson GPIO driver\n");
132 pdev = platform_device_register_simple("loongson-gpio", -1, NULL, 0);
133 return PTR_ERR_OR_ZERO(pdev);
135 postcore_initcall(loongson_gpio_setup);