2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 inline int gpio_is_valid(unsigned gpio)
14 return (gpio < BCM2835_GPIO_COUNT);
17 int gpio_request(unsigned gpio, const char *label)
19 return !gpio_is_valid(gpio);
22 int gpio_free(unsigned gpio)
27 int gpio_direction_input(unsigned gpio)
29 struct bcm2835_gpio_regs *reg =
30 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
33 val = readl(®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
34 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
35 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
36 writel(val, ®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
41 int gpio_direction_output(unsigned gpio, int value)
43 struct bcm2835_gpio_regs *reg =
44 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
47 gpio_set_value(gpio, value);
49 val = readl(®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
50 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
51 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
52 writel(val, ®->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
57 int gpio_get_value(unsigned gpio)
59 struct bcm2835_gpio_regs *reg =
60 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
63 val = readl(®->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
65 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
68 int gpio_set_value(unsigned gpio, int value)
70 struct bcm2835_gpio_regs *reg =
71 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
72 u32 *output_reg = value ? reg->gpset : reg->gpclr;
74 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
75 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);