blob: 3c9d4f3ed5508dd9b4e12cc69b610d9e2e496be2 [file] [log] [blame]
Arnaud Patardc197da92010-04-29 11:58:54 +02001/*
Huacai Chencbfb3ea2015-04-01 10:20:09 +08002 * Loongson-2F/3A/3B GPIO Support
Arnaud Patardc197da92010-04-29 11:58:54 +02003 *
Ralf Baechle70342282013-01-22 12:59:30 +01004 * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com>
Arnaud Patardc197da92010-04-29 11:58:54 +02005 * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
Huacai Chencbfb3ea2015-04-01 10:20:09 +08006 * Copyright (c) 2013 Hongbing Hu <huhb@lemote.com>
7 * Copyright (c) 2014 Huacai Chen <chenhc@lemote.com>
Arnaud Patardc197da92010-04-29 11:58:54 +02008 *
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.
13 */
14
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>
Linus Walleijf105edf2018-04-13 10:28:21 +020020#include <linux/gpio/driver.h>
Linus Walleij70e703e2018-04-13 12:05:36 +020021#include <linux/platform_device.h>
Arnaud Patardc197da92010-04-29 11:58:54 +020022#include <asm/types.h>
23#include <loongson.h>
Arnaud Patardc197da92010-04-29 11:58:54 +020024
25#define STLS2F_N_GPIO 4
Huacai Chencbfb3ea2015-04-01 10:20:09 +080026#define STLS3A_N_GPIO 16
27
28#ifdef CONFIG_CPU_LOONGSON3
29#define LOONGSON_N_GPIO STLS3A_N_GPIO
30#else
31#define LOONGSON_N_GPIO STLS2F_N_GPIO
32#endif
33
34#define LOONGSON_GPIO_IN_OFFSET 16
Arnaud Patardc197da92010-04-29 11:58:54 +020035
36static DEFINE_SPINLOCK(gpio_lock);
37
Huacai Chencbfb3ea2015-04-01 10:20:09 +080038static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
Arnaud Patardc197da92010-04-29 11:58:54 +020039{
Huacai Chendf5dade2015-04-01 10:20:07 +080040 u32 val;
41 u32 mask;
42
Huacai Chencbfb3ea2015-04-01 10:20:09 +080043 mask = 1 << (gpio + LOONGSON_GPIO_IN_OFFSET);
Huacai Chendf5dade2015-04-01 10:20:07 +080044 spin_lock(&gpio_lock);
45 val = LOONGSON_GPIODATA;
46 spin_unlock(&gpio_lock);
47
48 return (val & mask) != 0;
Arnaud Patardc197da92010-04-29 11:58:54 +020049}
50
Huacai Chencbfb3ea2015-04-01 10:20:09 +080051static void loongson_gpio_set_value(struct gpio_chip *chip,
Arnaud Patardc197da92010-04-29 11:58:54 +020052 unsigned gpio, int value)
53{
Huacai Chendf5dade2015-04-01 10:20:07 +080054 u32 val;
55 u32 mask;
56
57 mask = 1 << gpio;
58
59 spin_lock(&gpio_lock);
60 val = LOONGSON_GPIODATA;
61 if (value)
62 val |= mask;
63 else
64 val &= (~mask);
65 LOONGSON_GPIODATA = val;
66 spin_unlock(&gpio_lock);
Arnaud Patardc197da92010-04-29 11:58:54 +020067}
68
Linus Walleijf105edf2018-04-13 10:28:21 +020069static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
70{
71 u32 temp;
72 u32 mask;
73
74 spin_lock(&gpio_lock);
75 mask = 1 << gpio;
76 temp = LOONGSON_GPIOIE;
77 temp |= mask;
78 LOONGSON_GPIOIE = temp;
79 spin_unlock(&gpio_lock);
80
81 return 0;
82}
83
84static int loongson_gpio_direction_output(struct gpio_chip *chip,
85 unsigned gpio, int level)
86{
87 u32 temp;
88 u32 mask;
89
90 loongson_gpio_set_value(chip, gpio, level);
91 spin_lock(&gpio_lock);
92 mask = 1 << gpio;
93 temp = LOONGSON_GPIOIE;
94 temp &= (~mask);
95 LOONGSON_GPIOIE = temp;
96 spin_unlock(&gpio_lock);
97
98 return 0;
99}
100
Linus Walleij70e703e2018-04-13 12:05:36 +0200101static int loongson_gpio_probe(struct platform_device *pdev)
102{
103 struct gpio_chip *gc;
104 struct device *dev = &pdev->dev;
105
106 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
107 if (!gc)
108 return -ENOMEM;
109
110 gc->label = "loongson-gpio-chip";
111 gc->base = 0;
112 gc->ngpio = LOONGSON_N_GPIO;
113 gc->get = loongson_gpio_get_value;
114 gc->set = loongson_gpio_set_value;
115 gc->direction_input = loongson_gpio_direction_input;
116 gc->direction_output = loongson_gpio_direction_output;
117
118 return gpiochip_add_data(gc, NULL);
119}
120
121static struct platform_driver loongson_gpio_driver = {
122 .driver = {
123 .name = "loongson-gpio",
124 },
125 .probe = loongson_gpio_probe,
Arnaud Patardc197da92010-04-29 11:58:54 +0200126};
127
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800128static int __init loongson_gpio_setup(void)
Arnaud Patardc197da92010-04-29 11:58:54 +0200129{
Linus Walleij70e703e2018-04-13 12:05:36 +0200130 struct platform_device *pdev;
131 int ret;
132
133 ret = platform_driver_register(&loongson_gpio_driver);
134 if (ret) {
135 pr_err("error registering loongson GPIO driver\n");
136 return ret;
137 }
138
139 pdev = platform_device_register_simple("loongson-gpio", -1, NULL, 0);
140 return PTR_ERR_OR_ZERO(pdev);
Arnaud Patardc197da92010-04-29 11:58:54 +0200141}
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800142postcore_initcall(loongson_gpio_setup);