2 * Ralink SoC specific GPIO support
4 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/init.h>
14 #include <asm/mach-ralink/ramips_gpio.h>
15 #include <ralink_soc.h>
17 static inline struct ramips_gpio_chip *to_ramips_gpio(struct gpio_chip *chip)
19 struct ramips_gpio_chip *rg;
21 rg = container_of(chip, struct ramips_gpio_chip, chip);
25 static inline void ramips_gpio_wr(struct ramips_gpio_chip *rg, u8 reg, u32 val)
27 __raw_writel(val, rg->regs_base + rg->regs[reg]);
30 static inline u32 ramips_gpio_rr(struct ramips_gpio_chip *rg, u8 reg)
32 return __raw_readl(rg->regs_base + rg->regs[reg]);
35 static int ramips_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
37 struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
41 spin_lock_irqsave(&rg->lock, flags);
42 t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DIR);
44 ramips_gpio_wr(rg, RAMIPS_GPIO_REG_DIR, t);
45 spin_unlock_irqrestore(&rg->lock, flags);
50 static int ramips_gpio_direction_output(struct gpio_chip *chip,
51 unsigned offset, int value)
53 struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
58 reg = (value) ? RAMIPS_GPIO_REG_SET : RAMIPS_GPIO_REG_RESET;
60 spin_lock_irqsave(&rg->lock, flags);
61 ramips_gpio_wr(rg, reg, 1 << offset);
63 t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DIR);
65 ramips_gpio_wr(rg, RAMIPS_GPIO_REG_DIR, t);
66 spin_unlock_irqrestore(&rg->lock, flags);
71 static void ramips_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
73 struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
76 reg = (value) ? RAMIPS_GPIO_REG_SET : RAMIPS_GPIO_REG_RESET;
77 ramips_gpio_wr(rg, reg, 1 << offset);
80 static int ramips_gpio_get(struct gpio_chip *chip, unsigned offset)
82 struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
85 t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DATA);
86 return !!(t & (1 << offset));
89 static __init void ramips_gpio_chip_add(struct ramips_gpio_chip *rg)
91 spin_lock_init(&rg->lock);
93 rg->regs_base = ioremap(rg->map_base, rg->map_size);
95 rg->chip.direction_input = ramips_gpio_direction_input;
96 rg->chip.direction_output = ramips_gpio_direction_output;
97 rg->chip.get = ramips_gpio_get;
98 rg->chip.set = ramips_gpio_set;
100 /* set polarity to low for all lines */
101 ramips_gpio_wr(rg, RAMIPS_GPIO_REG_POL, 0);
103 gpiochip_add(&rg->chip);
106 __init int ramips_gpio_init(struct ramips_gpio_data *data)
110 for (i = 0; i < data->num_chips; i++)
111 ramips_gpio_chip_add(&data->chips[i]);