1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Vikram Narayananan
4 * <vikram186@gmail.com>
9 #include <dm/pinctrl.h>
15 struct bcm2835_gpios {
16 struct bcm2835_gpio_regs *reg;
17 struct udevice *pinctrl;
20 static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
22 struct bcm2835_gpios *gpios = dev_get_priv(dev);
25 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
26 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
27 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
28 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
33 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
36 struct bcm2835_gpios *gpios = dev_get_priv(dev);
39 gpio_set_value(gpio, value);
41 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
42 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
43 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
44 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
49 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
53 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
55 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
58 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
60 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
62 return bcm2835_get_value(gpios, gpio);
65 static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
68 struct bcm2835_gpios *gpios = dev_get_priv(dev);
69 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
71 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
72 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
77 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
79 struct bcm2835_gpios *priv = dev_get_priv(dev);
82 funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
85 case BCM2835_GPIO_OUTPUT:
87 case BCM2835_GPIO_INPUT:
94 static const struct dm_gpio_ops gpio_bcm2835_ops = {
95 .direction_input = bcm2835_gpio_direction_input,
96 .direction_output = bcm2835_gpio_direction_output,
97 .get_value = bcm2835_gpio_get_value,
98 .set_value = bcm2835_gpio_set_value,
99 .get_function = bcm2835_gpio_get_function,
102 static int bcm2835_gpio_probe(struct udevice *dev)
104 struct bcm2835_gpios *gpios = dev_get_priv(dev);
105 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
106 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
108 uc_priv->bank_name = "GPIO";
109 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
110 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
112 /* We know we're spawned by the pinctrl driver */
113 gpios->pinctrl = dev->parent;
118 #if CONFIG_IS_ENABLED(OF_CONTROL)
119 static int bcm2835_gpio_of_to_plat(struct udevice *dev)
121 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
124 addr = dev_read_addr(dev);
125 if (addr == FDT_ADDR_T_NONE)
133 U_BOOT_DRIVER(gpio_bcm2835) = {
134 .name = "gpio_bcm2835",
136 .of_to_plat = of_match_ptr(bcm2835_gpio_of_to_plat),
137 .plat_auto = sizeof(struct bcm2835_gpio_plat),
138 .ops = &gpio_bcm2835_ops,
139 .probe = bcm2835_gpio_probe,
140 .flags = DM_FLAG_PRE_RELOC,
141 .priv_auto = sizeof(struct bcm2835_gpios),