2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
15 struct bcm2835_gpios {
16 struct bcm2835_gpio_regs *reg;
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 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 int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
78 struct bcm2835_gpios *gpios = dev_get_priv(dev);
81 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
83 return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
86 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
88 int funcid = bcm2835_gpio_get_func_id(dev, offset);
91 case BCM2835_GPIO_OUTPUT:
93 case BCM2835_GPIO_INPUT:
101 static const struct dm_gpio_ops gpio_bcm2835_ops = {
102 .direction_input = bcm2835_gpio_direction_input,
103 .direction_output = bcm2835_gpio_direction_output,
104 .get_value = bcm2835_gpio_get_value,
105 .set_value = bcm2835_gpio_set_value,
106 .get_function = bcm2835_gpio_get_function,
109 static int bcm2835_gpio_probe(struct udevice *dev)
111 struct bcm2835_gpios *gpios = dev_get_priv(dev);
112 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
113 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
115 uc_priv->bank_name = "GPIO";
116 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
117 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
122 #if CONFIG_IS_ENABLED(OF_CONTROL)
123 static const struct udevice_id bcm2835_gpio_id[] = {
124 {.compatible = "brcm,bcm2835-gpio"},
128 static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
130 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
133 addr = dev_get_addr(dev);
134 if (addr == FDT_ADDR_T_NONE)
142 U_BOOT_DRIVER(gpio_bcm2835) = {
143 .name = "gpio_bcm2835",
145 .of_match = of_match_ptr(bcm2835_gpio_id),
146 .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
147 .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
148 .ops = &gpio_bcm2835_ops,
149 .probe = bcm2835_gpio_probe,
150 .flags = DM_FLAG_PRE_RELOC,
151 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),