2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
23 inline int gpio_is_valid(unsigned gpio)
25 return (gpio < BCM2835_GPIO_COUNT);
28 int gpio_request(unsigned gpio, const char *label)
30 return !gpio_is_valid(gpio);
33 int gpio_free(unsigned gpio)
38 int gpio_direction_input(unsigned gpio)
40 struct bcm2835_gpio_regs *reg =
41 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
44 val = readl(®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
45 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
46 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
47 writel(val, ®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
52 int gpio_direction_output(unsigned gpio, int value)
54 struct bcm2835_gpio_regs *reg =
55 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
58 gpio_set_value(gpio, value);
60 val = readl(®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
61 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
62 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
63 writel(val, ®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
68 int gpio_get_value(unsigned gpio)
70 struct bcm2835_gpio_regs *reg =
71 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
74 val = readl(®->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
76 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
79 int gpio_set_value(unsigned gpio, int value)
81 struct bcm2835_gpio_regs *reg =
82 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
83 u32 *output_reg = value ? reg->gpset : reg->gpclr;
85 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
86 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);