2009-08-31 08:29:19 +00:00
|
|
|
/*
|
|
|
|
* Ralink SoC specific GPIO support
|
|
|
|
*
|
2011-03-27 19:19:48 +00:00
|
|
|
* Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
|
2009-08-31 08:29:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
|
2011-03-27 19:19:57 +00:00
|
|
|
#include <asm/mach-ralink/ramips_gpio.h>
|
2009-08-31 08:29:19 +00:00
|
|
|
|
|
|
|
static inline struct ramips_gpio_chip *to_ramips_gpio(struct gpio_chip *chip)
|
|
|
|
{
|
|
|
|
struct ramips_gpio_chip *rg;
|
|
|
|
|
|
|
|
rg = container_of(chip, struct ramips_gpio_chip, chip);
|
|
|
|
return rg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ramips_gpio_wr(struct ramips_gpio_chip *rg, u8 reg, u32 val)
|
|
|
|
{
|
2011-03-27 19:19:48 +00:00
|
|
|
__raw_writel(val, rg->regs_base + rg->regs[reg]);
|
2009-08-31 08:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 ramips_gpio_rr(struct ramips_gpio_chip *rg, u8 reg)
|
|
|
|
{
|
2011-03-27 19:19:48 +00:00
|
|
|
return __raw_readl(rg->regs_base + rg->regs[reg]);
|
2009-08-31 08:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ramips_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
|
|
|
struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
|
2010-07-13 16:52:04 +00:00
|
|
|
unsigned long flags;
|
2009-08-31 08:29:19 +00:00
|
|
|
u32 t;
|
|
|
|
|
2010-07-13 16:52:04 +00:00
|
|
|
spin_lock_irqsave(&rg->lock, flags);
|
2009-08-31 08:29:19 +00:00
|
|
|
t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DIR);
|
|
|
|
t &= ~(1 << offset);
|
|
|
|
ramips_gpio_wr(rg, RAMIPS_GPIO_REG_DIR, t);
|
2010-07-13 16:52:04 +00:00
|
|
|
spin_unlock_irqrestore(&rg->lock, flags);
|
2009-08-31 08:29:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ramips_gpio_direction_output(struct gpio_chip *chip,
|
|
|
|
unsigned offset, int value)
|
|
|
|
{
|
|
|
|
struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
|
2010-07-13 16:52:04 +00:00
|
|
|
unsigned long flags;
|
2009-08-31 08:29:19 +00:00
|
|
|
u32 reg;
|
|
|
|
u32 t;
|
|
|
|
|
|
|
|
reg = (value) ? RAMIPS_GPIO_REG_SET : RAMIPS_GPIO_REG_RESET;
|
2010-07-13 16:52:04 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&rg->lock, flags);
|
2009-08-31 08:29:19 +00:00
|
|
|
ramips_gpio_wr(rg, reg, 1 << offset);
|
|
|
|
|
|
|
|
t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DIR);
|
|
|
|
t |= 1 << offset;
|
|
|
|
ramips_gpio_wr(rg, RAMIPS_GPIO_REG_DIR, t);
|
2010-07-13 16:52:04 +00:00
|
|
|
spin_unlock_irqrestore(&rg->lock, flags);
|
2009-08-31 08:29:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ramips_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
|
|
|
|
{
|
|
|
|
struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
reg = (value) ? RAMIPS_GPIO_REG_SET : RAMIPS_GPIO_REG_RESET;
|
|
|
|
ramips_gpio_wr(rg, reg, 1 << offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ramips_gpio_get(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
|
|
|
struct ramips_gpio_chip *rg = to_ramips_gpio(chip);
|
|
|
|
u32 t;
|
|
|
|
|
|
|
|
t = ramips_gpio_rr(rg, RAMIPS_GPIO_REG_DATA);
|
2010-01-30 15:25:08 +00:00
|
|
|
return !!(t & (1 << offset));
|
2009-08-31 08:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __init void ramips_gpio_chip_add(struct ramips_gpio_chip *rg)
|
|
|
|
{
|
2010-07-13 16:52:04 +00:00
|
|
|
spin_lock_init(&rg->lock);
|
|
|
|
|
2011-03-27 19:19:48 +00:00
|
|
|
rg->regs_base = ioremap(rg->map_base, rg->map_size);
|
|
|
|
|
2011-03-27 19:19:51 +00:00
|
|
|
rg->chip.direction_input = ramips_gpio_direction_input;
|
|
|
|
rg->chip.direction_output = ramips_gpio_direction_output;
|
|
|
|
rg->chip.get = ramips_gpio_get;
|
|
|
|
rg->chip.set = ramips_gpio_set;
|
|
|
|
|
2009-09-02 15:31:03 +00:00
|
|
|
/* set polarity to low for all lines */
|
2009-08-31 08:29:19 +00:00
|
|
|
ramips_gpio_wr(rg, RAMIPS_GPIO_REG_POL, 0);
|
|
|
|
|
|
|
|
gpiochip_add(&rg->chip);
|
|
|
|
}
|
|
|
|
|
2011-03-27 19:19:59 +00:00
|
|
|
__init int ramips_gpio_init(struct ramips_gpio_data *data)
|
2009-08-31 08:29:19 +00:00
|
|
|
{
|
2011-03-27 19:19:59 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < data->num_chips; i++)
|
|
|
|
ramips_gpio_chip_add(&data->chips[i]);
|
2009-08-31 08:29:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|