gpio.c 3.4 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
F
Florian Fainelli 已提交
4
 * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/module.h>
F
Florian Fainelli 已提交
22
#include <linux/gpio.h>
23 24 25

#include <asm/mach-ar7/gpio.h>

F
Florian Fainelli 已提交
26
struct ar7_gpio_chip {
27 28
	void __iomem		*regs;
	struct gpio_chip	chip;
F
Florian Fainelli 已提交
29
};
30

F
Florian Fainelli 已提交
31
static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
32
{
F
Florian Fainelli 已提交
33 34 35
	struct ar7_gpio_chip *gpch =
				container_of(chip, struct ar7_gpio_chip, chip);
	void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT;
36

F
Florian Fainelli 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	return readl(gpio_in) & (1 << gpio);
}

static void ar7_gpio_set_value(struct gpio_chip *chip,
				unsigned gpio, int value)
{
	struct ar7_gpio_chip *gpch =
				container_of(chip, struct ar7_gpio_chip, chip);
	void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT;
	unsigned tmp;

	tmp = readl(gpio_out) & ~(1 << gpio);
	if (value)
		tmp |= 1 << gpio;
	writel(tmp, gpio_out);
}

static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
{
	struct ar7_gpio_chip *gpch =
				container_of(chip, struct ar7_gpio_chip, chip);
	void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
59

F
Florian Fainelli 已提交
60
	writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
61 62 63 64

	return 0;
}

F
Florian Fainelli 已提交
65 66
static int ar7_gpio_direction_output(struct gpio_chip *chip,
					unsigned gpio, int value)
67
{
F
Florian Fainelli 已提交
68 69 70 71 72 73 74 75 76 77 78 79
	struct ar7_gpio_chip *gpch =
				container_of(chip, struct ar7_gpio_chip, chip);
	void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;

	ar7_gpio_set_value(chip, gpio, value);
	writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);

	return 0;
}

static struct ar7_gpio_chip ar7_gpio_chip = {
	.chip = {
80
		.label			= "ar7-gpio",
F
Florian Fainelli 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		.direction_input	= ar7_gpio_direction_input,
		.direction_output	= ar7_gpio_direction_output,
		.set			= ar7_gpio_set_value,
		.get			= ar7_gpio_get_value,
		.base			= 0,
		.ngpio			= AR7_GPIO_MAX,
	}
};

int ar7_gpio_enable(unsigned gpio)
{
	void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;

	writel(readl(gpio_en) | (1 << gpio), gpio_en);

	return 0;
}
EXPORT_SYMBOL(ar7_gpio_enable);

int ar7_gpio_disable(unsigned gpio)
{
	void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;

	writel(readl(gpio_en) & ~(1 << gpio), gpio_en);

	return 0;
}
EXPORT_SYMBOL(ar7_gpio_disable);

static int __init ar7_gpio_init(void)
{
	int ret;

	ar7_gpio_chip.regs = ioremap_nocache(AR7_REGS_GPIO,
					AR7_REGS_GPIO + 0x10);

	if (!ar7_gpio_chip.regs) {
		printk(KERN_ERR "ar7-gpio: failed to ioremap regs\n");
		return -ENOMEM;
	}

	ret = gpiochip_add(&ar7_gpio_chip.chip);
	if (ret) {
		printk(KERN_ERR "ar7-gpio: failed to add gpiochip\n");
		return ret;
	}
	printk(KERN_INFO "ar7-gpio: registered %d GPIOs\n",
				ar7_gpio_chip.chip.ngpio);
	return ret;
130
}
F
Florian Fainelli 已提交
131
arch_initcall(ar7_gpio_init);