3 * Vikas Manocha, <vikas.manocha@st.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/gpio.h>
13 #include <asm/arch/stm32.h>
16 #include <linux/errno.h>
19 #define MAX_SIZE_BANK_NAME 5
20 #define STM32_GPIOS_PER_BANK 16
21 #define MODE_BITS(gpio_pin) (gpio_pin * 2)
22 #define MODE_BITS_MASK 3
23 #define IN_OUT_BIT_INDEX(gpio_pin) (1UL << (gpio_pin))
25 DECLARE_GLOBAL_DATA_PTR;
27 static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset)
29 struct stm32_gpio_priv *priv = dev_get_priv(dev);
30 struct stm32_gpio_regs *regs = priv->regs;
31 int bits_index = MODE_BITS(offset);
32 int mask = MODE_BITS_MASK << bits_index;
34 clrsetbits_le32(®s->moder, mask, STM32_GPIO_MODE_IN << bits_index);
39 static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset,
42 struct stm32_gpio_priv *priv = dev_get_priv(dev);
43 struct stm32_gpio_regs *regs = priv->regs;
44 int bits_index = MODE_BITS(offset);
45 int mask = MODE_BITS_MASK << bits_index;
47 clrsetbits_le32(®s->moder, mask, STM32_GPIO_MODE_OUT << bits_index);
48 mask = IN_OUT_BIT_INDEX(offset);
49 clrsetbits_le32(®s->odr, mask, value ? mask : 0);
54 static int stm32_gpio_get_value(struct udevice *dev, unsigned offset)
56 struct stm32_gpio_priv *priv = dev_get_priv(dev);
57 struct stm32_gpio_regs *regs = priv->regs;
59 return readl(®s->idr) & IN_OUT_BIT_INDEX(offset) ? 1 : 0;
62 static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value)
64 struct stm32_gpio_priv *priv = dev_get_priv(dev);
65 struct stm32_gpio_regs *regs = priv->regs;
66 int mask = IN_OUT_BIT_INDEX(offset);
68 clrsetbits_le32(®s->odr, mask, value ? mask : 0);
73 static const struct dm_gpio_ops gpio_stm32_ops = {
74 .direction_input = stm32_gpio_direction_input,
75 .direction_output = stm32_gpio_direction_output,
76 .get_value = stm32_gpio_get_value,
77 .set_value = stm32_gpio_set_value,
80 static int gpio_stm32_probe(struct udevice *dev)
82 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
83 struct stm32_gpio_priv *priv = dev_get_priv(dev);
87 addr = devfdt_get_addr(dev);
88 if (addr == FDT_ADDR_T_NONE)
91 priv->regs = (struct stm32_gpio_regs *)addr;
92 name = (char *)fdtdec_locate_byte_array(gd->fdt_blob,
98 uc_priv->bank_name = name;
99 uc_priv->gpio_count = STM32_GPIOS_PER_BANK;
100 debug("%s, addr = 0x%p, bank_name = %s\n", __func__, (u32 *)priv->regs,
106 ret = clk_get_by_index(dev, 0, &clk);
110 ret = clk_enable(&clk);
113 dev_err(dev, "failed to enable clock\n");
116 debug("clock enabled for device %s\n", dev->name);
122 static const struct udevice_id stm32_gpio_ids[] = {
123 { .compatible = "st,stm32-gpio" },
127 U_BOOT_DRIVER(gpio_stm32) = {
128 .name = "gpio_stm32",
130 .of_match = stm32_gpio_ids,
131 .probe = gpio_stm32_probe,
132 .ops = &gpio_stm32_ops,
133 .flags = DM_FLAG_PRE_RELOC | DM_UC_FLAG_SEQ_ALIAS,
134 .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv),