1 // SPDX-License-Identifier: GPL-2.0-or-later
8 #include <linux/init.h>
9 #include <linux/export.h>
10 #include <linux/gpio.h>
12 #include <asm/mach-ar7/ar7.h>
14 #define AR7_GPIO_MAX 32
15 #define TITAN_GPIO_MAX 51
17 struct ar7_gpio_chip {
19 struct gpio_chip chip;
22 static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
24 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
25 void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT;
27 return !!(readl(gpio_in) & (1 << gpio));
30 static int titan_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
32 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
33 void __iomem *gpio_in0 = gpch->regs + TITAN_GPIO_INPUT_0;
34 void __iomem *gpio_in1 = gpch->regs + TITAN_GPIO_INPUT_1;
36 return readl(gpio >> 5 ? gpio_in1 : gpio_in0) & (1 << (gpio & 0x1f));
39 static void ar7_gpio_set_value(struct gpio_chip *chip,
40 unsigned gpio, int value)
42 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
43 void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT;
46 tmp = readl(gpio_out) & ~(1 << gpio);
49 writel(tmp, gpio_out);
52 static void titan_gpio_set_value(struct gpio_chip *chip,
53 unsigned gpio, int value)
55 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
56 void __iomem *gpio_out0 = gpch->regs + TITAN_GPIO_OUTPUT_0;
57 void __iomem *gpio_out1 = gpch->regs + TITAN_GPIO_OUTPUT_1;
60 tmp = readl(gpio >> 5 ? gpio_out1 : gpio_out0) & ~(1 << (gpio & 0x1f));
62 tmp |= 1 << (gpio & 0x1f);
63 writel(tmp, gpio >> 5 ? gpio_out1 : gpio_out0);
66 static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
68 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
69 void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
71 writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
76 static int titan_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
78 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
79 void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0;
80 void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1;
82 if (gpio >= TITAN_GPIO_MAX)
85 writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) | (1 << (gpio & 0x1f)),
86 gpio >> 5 ? gpio_dir1 : gpio_dir0);
90 static int ar7_gpio_direction_output(struct gpio_chip *chip,
91 unsigned gpio, int value)
93 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
94 void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
96 ar7_gpio_set_value(chip, gpio, value);
97 writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
102 static int titan_gpio_direction_output(struct gpio_chip *chip,
103 unsigned gpio, int value)
105 struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
106 void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0;
107 void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1;
109 if (gpio >= TITAN_GPIO_MAX)
112 titan_gpio_set_value(chip, gpio, value);
113 writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) & ~(1 <<
114 (gpio & 0x1f)), gpio >> 5 ? gpio_dir1 : gpio_dir0);
119 static struct ar7_gpio_chip ar7_gpio_chip = {
122 .direction_input = ar7_gpio_direction_input,
123 .direction_output = ar7_gpio_direction_output,
124 .set = ar7_gpio_set_value,
125 .get = ar7_gpio_get_value,
127 .ngpio = AR7_GPIO_MAX,
131 static struct ar7_gpio_chip titan_gpio_chip = {
133 .label = "titan-gpio",
134 .direction_input = titan_gpio_direction_input,
135 .direction_output = titan_gpio_direction_output,
136 .set = titan_gpio_set_value,
137 .get = titan_gpio_get_value,
139 .ngpio = TITAN_GPIO_MAX,
143 static inline int ar7_gpio_enable_ar7(unsigned gpio)
145 void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
147 writel(readl(gpio_en) | (1 << gpio), gpio_en);
152 static inline int ar7_gpio_enable_titan(unsigned gpio)
154 void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0;
155 void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1;
157 writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) | (1 << (gpio & 0x1f)),
158 gpio >> 5 ? gpio_en1 : gpio_en0);
163 int ar7_gpio_enable(unsigned gpio)
165 return ar7_is_titan() ? ar7_gpio_enable_titan(gpio) :
166 ar7_gpio_enable_ar7(gpio);
168 EXPORT_SYMBOL(ar7_gpio_enable);
170 static inline int ar7_gpio_disable_ar7(unsigned gpio)
172 void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
174 writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
179 static inline int ar7_gpio_disable_titan(unsigned gpio)
181 void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0;
182 void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1;
184 writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) & ~(1 << (gpio & 0x1f)),
185 gpio >> 5 ? gpio_en1 : gpio_en0);
190 int ar7_gpio_disable(unsigned gpio)
192 return ar7_is_titan() ? ar7_gpio_disable_titan(gpio) :
193 ar7_gpio_disable_ar7(gpio);
195 EXPORT_SYMBOL(ar7_gpio_disable);
197 struct titan_gpio_cfg {
203 static const struct titan_gpio_cfg titan_gpio_table[] = {
204 /* reg, start bit, mux value */
259 static int titan_gpio_pinsel(unsigned gpio)
261 struct titan_gpio_cfg gpio_cfg;
262 u32 mux_status, pin_sel_reg, tmp;
263 void __iomem *pin_sel = (void __iomem *)KSEG1ADDR(AR7_REGS_PINSEL);
265 if (gpio >= ARRAY_SIZE(titan_gpio_table))
268 gpio_cfg = titan_gpio_table[gpio];
269 pin_sel_reg = gpio_cfg.reg - 1;
271 mux_status = (readl(pin_sel + pin_sel_reg) >> gpio_cfg.shift) & 0x3;
273 /* Check the mux status */
274 if (!((mux_status == 0) || (mux_status == gpio_cfg.func)))
277 /* Set the pin sel value */
278 tmp = readl(pin_sel + pin_sel_reg);
279 tmp |= ((gpio_cfg.func & 0x3) << gpio_cfg.shift);
280 writel(tmp, pin_sel + pin_sel_reg);
285 /* Perform minimal Titan GPIO configuration */
286 static void titan_gpio_init(void)
290 for (i = 44; i < 48; i++) {
291 titan_gpio_pinsel(i);
292 ar7_gpio_enable_titan(i);
293 titan_gpio_direction_input(&titan_gpio_chip.chip, i);
297 int __init ar7_gpio_init(void)
300 struct ar7_gpio_chip *gpch;
303 if (!ar7_is_titan()) {
304 gpch = &ar7_gpio_chip;
307 gpch = &titan_gpio_chip;
311 gpch->regs = ioremap(AR7_REGS_GPIO, size);
313 printk(KERN_ERR "%s: failed to ioremap regs\n",
318 ret = gpiochip_add_data(&gpch->chip, gpch);
320 printk(KERN_ERR "%s: failed to add gpiochip\n",
325 printk(KERN_INFO "%s: registered %d GPIOs\n",
326 gpch->chip.label, gpch->chip.ngpio);