blob: 973d82a29442059267d6a7f5d3f026eee95804a8 [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>
Arnaud Patardc197da92010-04-29 11:58:54 +020021#include <asm/types.h>
22#include <loongson.h>
Arnaud Patardc197da92010-04-29 11:58:54 +020023
24#define STLS2F_N_GPIO 4
Huacai Chencbfb3ea2015-04-01 10:20:09 +080025#define STLS3A_N_GPIO 16
26
27#ifdef CONFIG_CPU_LOONGSON3
28#define LOONGSON_N_GPIO STLS3A_N_GPIO
29#else
30#define LOONGSON_N_GPIO STLS2F_N_GPIO
31#endif
32
33#define LOONGSON_GPIO_IN_OFFSET 16
Arnaud Patardc197da92010-04-29 11:58:54 +020034
35static DEFINE_SPINLOCK(gpio_lock);
36
Huacai Chencbfb3ea2015-04-01 10:20:09 +080037static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
Arnaud Patardc197da92010-04-29 11:58:54 +020038{
Huacai Chendf5dade2015-04-01 10:20:07 +080039 u32 val;
40 u32 mask;
41
Huacai Chencbfb3ea2015-04-01 10:20:09 +080042 mask = 1 << (gpio + LOONGSON_GPIO_IN_OFFSET);
Huacai Chendf5dade2015-04-01 10:20:07 +080043 spin_lock(&gpio_lock);
44 val = LOONGSON_GPIODATA;
45 spin_unlock(&gpio_lock);
46
47 return (val & mask) != 0;
Arnaud Patardc197da92010-04-29 11:58:54 +020048}
49
Huacai Chencbfb3ea2015-04-01 10:20:09 +080050static void loongson_gpio_set_value(struct gpio_chip *chip,
Arnaud Patardc197da92010-04-29 11:58:54 +020051 unsigned gpio, int value)
52{
Huacai Chendf5dade2015-04-01 10:20:07 +080053 u32 val;
54 u32 mask;
55
56 mask = 1 << gpio;
57
58 spin_lock(&gpio_lock);
59 val = LOONGSON_GPIODATA;
60 if (value)
61 val |= mask;
62 else
63 val &= (~mask);
64 LOONGSON_GPIODATA = val;
65 spin_unlock(&gpio_lock);
Arnaud Patardc197da92010-04-29 11:58:54 +020066}
67
Linus Walleijf105edf2018-04-13 10:28:21 +020068static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
69{
70 u32 temp;
71 u32 mask;
72
73 spin_lock(&gpio_lock);
74 mask = 1 << gpio;
75 temp = LOONGSON_GPIOIE;
76 temp |= mask;
77 LOONGSON_GPIOIE = temp;
78 spin_unlock(&gpio_lock);
79
80 return 0;
81}
82
83static int loongson_gpio_direction_output(struct gpio_chip *chip,
84 unsigned gpio, int level)
85{
86 u32 temp;
87 u32 mask;
88
89 loongson_gpio_set_value(chip, gpio, level);
90 spin_lock(&gpio_lock);
91 mask = 1 << gpio;
92 temp = LOONGSON_GPIOIE;
93 temp &= (~mask);
94 LOONGSON_GPIOIE = temp;
95 spin_unlock(&gpio_lock);
96
97 return 0;
98}
99
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800100static 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,
Ralf Baechle70342282013-01-22 12:59:30 +0100106 .base = 0,
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800107 .ngpio = LOONGSON_N_GPIO,
Huacai Chendf5dade2015-04-01 10:20:07 +0800108 .can_sleep = false,
Arnaud Patardc197da92010-04-29 11:58:54 +0200109};
110
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800111static int __init loongson_gpio_setup(void)
Arnaud Patardc197da92010-04-29 11:58:54 +0200112{
Linus Walleij4eab22e2015-12-08 10:41:44 +0100113 return gpiochip_add_data(&loongson_chip, NULL);
Arnaud Patardc197da92010-04-29 11:58:54 +0200114}
Huacai Chencbfb3ea2015-04-01 10:20:09 +0800115postcore_initcall(loongson_gpio_setup);