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> |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
| 13 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 14 | #define GPIO_NAME_SIZE 20 |
| 15 | |
| 16 | struct bcm2835_gpios { |
| 17 | char label[BCM2835_GPIO_COUNT][GPIO_NAME_SIZE]; |
| 18 | struct bcm2835_gpio_regs *reg; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * gpio_is_requested() - check if a GPIO has been requested |
| 23 | * |
| 24 | * @bank: Bank to check |
| 25 | * @offset: GPIO offset within bank to check |
| 26 | * @return true if marked as requested, false if not |
| 27 | */ |
| 28 | static inline bool gpio_is_requested(struct bcm2835_gpios *gpios, int offset) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 29 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 30 | return *gpios->label[offset] != '\0'; |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 33 | static int check_requested(struct udevice *dev, unsigned offset, |
| 34 | const char *func) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 35 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 36 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 37 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 38 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 39 | if (!gpio_is_requested(gpios, offset)) { |
| 40 | printf("omap_gpio: %s: error: gpio %s%d not requested\n", |
| 41 | func, uc_priv->bank_name, offset); |
| 42 | return -EPERM; |
| 43 | } |
| 44 | |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 48 | static int bcm2835_gpio_request(struct udevice *dev, unsigned offset, |
| 49 | const char *label) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 50 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 51 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 52 | |
| 53 | if (gpio_is_requested(gpios, offset)) |
| 54 | return -EBUSY; |
| 55 | |
| 56 | strncpy(gpios->label[offset], label, GPIO_NAME_SIZE); |
| 57 | gpios->label[offset][GPIO_NAME_SIZE - 1] = '\0'; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int bcm2835_gpio_free(struct udevice *dev, unsigned offset) |
| 63 | { |
| 64 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 65 | int ret; |
| 66 | |
| 67 | ret = check_requested(dev, offset, __func__); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | gpios->label[offset][0] = '\0'; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 76 | { |
| 77 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 78 | unsigned val; |
| 79 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 80 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 81 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 82 | val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 83 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 88 | static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 89 | int value) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 90 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 91 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 92 | unsigned val; |
| 93 | |
| 94 | gpio_set_value(gpio, value); |
| 95 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 96 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 97 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 98 | val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 99 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 104 | static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 105 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 106 | u32 val; |
| 107 | |
| 108 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
| 109 | val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio); |
| 110 | return val ? true : false; |
| 111 | } |
| 112 | |
| 113 | static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio) |
| 114 | { |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 115 | unsigned val; |
| 116 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 117 | val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 118 | |
| 119 | return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; |
| 120 | } |
| 121 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 122 | static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 123 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 124 | const struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 125 | |
| 126 | return bcm2835_get_value(gpios, gpio); |
| 127 | } |
| 128 | |
| 129 | static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 130 | int value) |
| 131 | { |
| 132 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 133 | u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr; |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 134 | |
| 135 | writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), |
| 136 | &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 137 | |
| 138 | return 0; |
| 139 | } |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 140 | |
| 141 | static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset) |
| 142 | { |
| 143 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 144 | |
| 145 | if (!gpio_is_requested(gpios, offset)) |
| 146 | return GPIOF_UNUSED; |
| 147 | |
| 148 | /* GPIOF_FUNC is not implemented yet */ |
| 149 | if (bcm2835_gpio_is_output(gpios, offset)) |
| 150 | return GPIOF_OUTPUT; |
| 151 | else |
| 152 | return GPIOF_INPUT; |
| 153 | } |
| 154 | |
| 155 | static int bcm2835_gpio_get_state(struct udevice *dev, unsigned int offset, |
| 156 | char *buf, int bufsize) |
| 157 | { |
| 158 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 159 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 160 | const char *label; |
| 161 | bool requested; |
| 162 | bool is_output; |
| 163 | int size; |
| 164 | |
| 165 | label = gpios->label[offset]; |
| 166 | is_output = bcm2835_gpio_is_output(gpios, offset); |
| 167 | size = snprintf(buf, bufsize, "%s%d: ", |
| 168 | uc_priv->bank_name ? uc_priv->bank_name : "", offset); |
| 169 | buf += size; |
| 170 | bufsize -= size; |
| 171 | requested = gpio_is_requested(gpios, offset); |
| 172 | snprintf(buf, bufsize, "%s: %d [%c]%s%s", |
| 173 | is_output ? "out" : " in", |
| 174 | bcm2835_get_value(gpios, offset), |
| 175 | requested ? 'x' : ' ', |
| 176 | requested ? " " : "", |
| 177 | label); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static const struct dm_gpio_ops gpio_bcm2835_ops = { |
| 183 | .request = bcm2835_gpio_request, |
| 184 | .free = bcm2835_gpio_free, |
| 185 | .direction_input = bcm2835_gpio_direction_input, |
| 186 | .direction_output = bcm2835_gpio_direction_output, |
| 187 | .get_value = bcm2835_gpio_get_value, |
| 188 | .set_value = bcm2835_gpio_set_value, |
| 189 | .get_function = bcm2835_gpio_get_function, |
| 190 | .get_state = bcm2835_gpio_get_state, |
| 191 | }; |
| 192 | |
| 193 | static int bcm2835_gpio_probe(struct udevice *dev) |
| 194 | { |
| 195 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 196 | struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev); |
| 197 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 198 | |
| 199 | uc_priv->bank_name = "GPIO"; |
| 200 | uc_priv->gpio_count = BCM2835_GPIO_COUNT; |
| 201 | gpios->reg = (struct bcm2835_gpio_regs *)plat->base; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | U_BOOT_DRIVER(gpio_bcm2835) = { |
| 207 | .name = "gpio_bcm2835", |
| 208 | .id = UCLASS_GPIO, |
| 209 | .ops = &gpio_bcm2835_ops, |
| 210 | .probe = bcm2835_gpio_probe, |
| 211 | .priv_auto_alloc_size = sizeof(struct bcm2835_gpios), |
| 212 | }; |