1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Vikram Narayananan
4 * <vikram186@gmail.com>
8 #include <dm/pinctrl.h>
14 struct bcm2835_gpios {
15 struct bcm2835_gpio_regs *reg;
16 struct udevice *pinctrl;
19 static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
21 struct bcm2835_gpios *gpios = dev_get_priv(dev);
24 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
25 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
26 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
27 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
32 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
35 struct bcm2835_gpios *gpios = dev_get_priv(dev);
38 gpio_set_value(gpio, value);
40 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
41 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
42 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
43 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
48 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
52 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
54 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
57 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
59 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
61 return bcm2835_get_value(gpios, gpio);
64 static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
67 struct bcm2835_gpios *gpios = dev_get_priv(dev);
68 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
70 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
71 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
76 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
78 struct bcm2835_gpios *priv = dev_get_priv(dev);
81 funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
84 case BCM2835_GPIO_OUTPUT:
86 case BCM2835_GPIO_INPUT:
93 static const struct dm_gpio_ops gpio_bcm2835_ops = {
94 .direction_input = bcm2835_gpio_direction_input,
95 .direction_output = bcm2835_gpio_direction_output,
96 .get_value = bcm2835_gpio_get_value,
97 .set_value = bcm2835_gpio_set_value,
98 .get_function = bcm2835_gpio_get_function,
101 static int bcm2835_gpio_probe(struct udevice *dev)
103 struct bcm2835_gpios *gpios = dev_get_priv(dev);
104 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
105 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
107 uc_priv->bank_name = "GPIO";
108 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
109 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
111 /* We know we're spawned by the pinctrl driver */
112 gpios->pinctrl = dev->parent;
117 #if CONFIG_IS_ENABLED(OF_CONTROL)
118 static int bcm2835_gpio_of_to_plat(struct udevice *dev)
120 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
123 addr = dev_read_addr(dev);
124 if (addr == FDT_ADDR_T_NONE)
132 U_BOOT_DRIVER(gpio_bcm2835) = {
133 .name = "gpio_bcm2835",
135 .of_to_plat = of_match_ptr(bcm2835_gpio_of_to_plat),
136 .plat_auto = sizeof(struct bcm2835_gpio_plat),
137 .ops = &gpio_bcm2835_ops,
138 .probe = bcm2835_gpio_probe,
139 .flags = DM_FLAG_PRE_RELOC,
140 .priv_auto = sizeof(struct bcm2835_gpios),