Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Vikram Narayananan |
| 3 | * <vikram186@gmail.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/gpio.h> |
| 10 | #include <asm/io.h> |
| 11 | |
| 12 | inline int gpio_is_valid(unsigned gpio) |
| 13 | { |
| 14 | return (gpio < BCM2835_GPIO_COUNT); |
| 15 | } |
| 16 | |
| 17 | int gpio_request(unsigned gpio, const char *label) |
| 18 | { |
| 19 | return !gpio_is_valid(gpio); |
| 20 | } |
| 21 | |
| 22 | int gpio_free(unsigned gpio) |
| 23 | { |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int gpio_direction_input(unsigned gpio) |
| 28 | { |
| 29 | struct bcm2835_gpio_regs *reg = |
| 30 | (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE; |
| 31 | unsigned val; |
| 32 | |
| 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)]); |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int gpio_direction_output(unsigned gpio, int value) |
| 42 | { |
| 43 | struct bcm2835_gpio_regs *reg = |
| 44 | (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE; |
| 45 | unsigned val; |
| 46 | |
| 47 | gpio_set_value(gpio, value); |
| 48 | |
| 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)]); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int gpio_get_value(unsigned gpio) |
| 58 | { |
| 59 | struct bcm2835_gpio_regs *reg = |
| 60 | (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE; |
| 61 | unsigned val; |
| 62 | |
| 63 | val = readl(®->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 64 | |
| 65 | return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; |
| 66 | } |
| 67 | |
| 68 | int gpio_set_value(unsigned gpio, int value) |
| 69 | { |
| 70 | struct bcm2835_gpio_regs *reg = |
| 71 | (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE; |
| 72 | u32 *output_reg = value ? reg->gpset : reg->gpclr; |
| 73 | |
| 74 | writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), |
| 75 | &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 76 | |
| 77 | return 0; |
| 78 | } |