1 /* 2 * Copyright (C) 2012 Vikram Narayananan 3 * <vikram186@gmail.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <dm.h> 10 #include <errno.h> 11 #include <asm/gpio.h> 12 #include <asm/io.h> 13 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) 29 { 30 return *gpios->label[offset] != '\0'; 31 } 32 33 static int check_requested(struct udevice *dev, unsigned offset, 34 const char *func) 35 { 36 struct bcm2835_gpios *gpios = dev_get_priv(dev); 37 struct gpio_dev_priv *uc_priv = dev->uclass_priv; 38 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 45 return 0; 46 } 47 48 static int bcm2835_gpio_request(struct udevice *dev, unsigned offset, 49 const char *label) 50 { 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); 78 unsigned val; 79 80 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); 81 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); 82 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); 83 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); 84 85 return 0; 86 } 87 88 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio, 89 int value) 90 { 91 struct bcm2835_gpios *gpios = dev_get_priv(dev); 92 unsigned val; 93 94 gpio_set_value(gpio, value); 95 96 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); 97 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); 98 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); 99 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); 100 101 return 0; 102 } 103 104 static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio) 105 { 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 { 115 unsigned val; 116 117 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); 118 119 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; 120 } 121 122 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio) 123 { 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; 134 135 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), 136 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); 137 138 return 0; 139 } 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 }; 213