2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
14 struct bcm2835_gpios {
15 struct bcm2835_gpio_regs *reg;
18 static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
20 struct bcm2835_gpios *gpios = dev_get_priv(dev);
23 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
24 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
25 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
26 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
31 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
34 struct bcm2835_gpios *gpios = dev_get_priv(dev);
37 gpio_set_value(gpio, value);
39 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
40 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
41 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
42 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
47 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
51 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
53 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
56 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
58 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
60 return bcm2835_get_value(gpios, gpio);
63 static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
66 struct bcm2835_gpios *gpios = dev_get_priv(dev);
67 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
69 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
70 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
75 int bcm2835_gpio_get_func_id(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)]);
82 return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
85 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
87 int funcid = bcm2835_gpio_get_func_id(dev, offset);
90 case BCM2835_GPIO_OUTPUT:
92 case BCM2835_GPIO_INPUT:
100 static const struct dm_gpio_ops gpio_bcm2835_ops = {
101 .direction_input = bcm2835_gpio_direction_input,
102 .direction_output = bcm2835_gpio_direction_output,
103 .get_value = bcm2835_gpio_get_value,
104 .set_value = bcm2835_gpio_set_value,
105 .get_function = bcm2835_gpio_get_function,
108 static int bcm2835_gpio_probe(struct udevice *dev)
110 struct bcm2835_gpios *gpios = dev_get_priv(dev);
111 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
112 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
114 uc_priv->bank_name = "GPIO";
115 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
116 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
121 U_BOOT_DRIVER(gpio_bcm2835) = {
122 .name = "gpio_bcm2835",
124 .ops = &gpio_bcm2835_ops,
125 .probe = bcm2835_gpio_probe,
126 .flags = DM_FLAG_PRE_RELOC,
127 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),