2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
14 #define GPIO_NAME_SIZE 20
16 struct bcm2835_gpios {
17 char label[BCM2835_GPIO_COUNT][GPIO_NAME_SIZE];
18 struct bcm2835_gpio_regs *reg;
22 * gpio_is_requested() - check if a GPIO has been requested
24 * @bank: Bank to check
25 * @offset: GPIO offset within bank to check
26 * @return true if marked as requested, false if not
28 static inline bool gpio_is_requested(struct bcm2835_gpios *gpios, int offset)
30 return *gpios->label[offset] != '\0';
33 static int check_requested(struct udevice *dev, unsigned offset,
36 struct bcm2835_gpios *gpios = dev_get_priv(dev);
37 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
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);
48 static int bcm2835_gpio_request(struct udevice *dev, unsigned offset,
51 struct bcm2835_gpios *gpios = dev_get_priv(dev);
53 if (gpio_is_requested(gpios, offset))
56 strncpy(gpios->label[offset], label, GPIO_NAME_SIZE);
57 gpios->label[offset][GPIO_NAME_SIZE - 1] = '\0';
62 static int bcm2835_gpio_free(struct udevice *dev, unsigned offset)
64 struct bcm2835_gpios *gpios = dev_get_priv(dev);
67 ret = check_requested(dev, offset, __func__);
70 gpios->label[offset][0] = '\0';
75 static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
77 struct bcm2835_gpios *gpios = dev_get_priv(dev);
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)]);
88 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
91 struct bcm2835_gpios *gpios = dev_get_priv(dev);
94 gpio_set_value(gpio, value);
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)]);
104 static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
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;
113 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
117 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
119 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
122 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
124 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
126 return bcm2835_get_value(gpios, gpio);
129 static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
132 struct bcm2835_gpios *gpios = dev_get_priv(dev);
133 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
135 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
136 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
141 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
143 struct bcm2835_gpios *gpios = dev_get_priv(dev);
145 if (!gpio_is_requested(gpios, offset))
148 /* GPIOF_FUNC is not implemented yet */
149 if (bcm2835_gpio_is_output(gpios, offset))
155 static int bcm2835_gpio_get_state(struct udevice *dev, unsigned int offset,
156 char *buf, int bufsize)
158 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
159 struct bcm2835_gpios *gpios = dev_get_priv(dev);
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);
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 ? " " : "",
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,
193 static int bcm2835_gpio_probe(struct udevice *dev)
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;
199 uc_priv->bank_name = "GPIO";
200 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
201 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
206 U_BOOT_DRIVER(gpio_bcm2835) = {
207 .name = "gpio_bcm2835",
209 .ops = &gpio_bcm2835_ops,
210 .probe = bcm2835_gpio_probe,
211 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),