3 * Bhuvanchandra DV, Toradex, Inc.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/imx-common/iomux-v3.h>
17 DECLARE_GLOBAL_DATA_PTR;
21 struct vybrid_gpio_regs *reg;
24 static int vybrid_gpio_direction_input(struct udevice *dev, unsigned gpio)
26 const struct vybrid_gpios *gpios = dev_get_priv(dev);
28 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
29 imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_IN);
34 static int vybrid_gpio_direction_output(struct udevice *dev, unsigned gpio,
37 const struct vybrid_gpios *gpios = dev_get_priv(dev);
39 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
40 gpio_set_value(gpio, value);
41 imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_OUT);
46 static int vybrid_gpio_get_value(struct udevice *dev, unsigned gpio)
48 const struct vybrid_gpios *gpios = dev_get_priv(dev);
50 return ((readl(&gpios->reg->gpio_pdir) & (1 << gpio))) ? 1 : 0;
53 static int vybrid_gpio_set_value(struct udevice *dev, unsigned gpio,
56 const struct vybrid_gpios *gpios = dev_get_priv(dev);
58 writel((1 << gpio), &gpios->reg->gpio_psor);
60 writel((1 << gpio), &gpios->reg->gpio_pcor);
65 static int vybrid_gpio_get_function(struct udevice *dev, unsigned gpio)
67 const struct vybrid_gpios *gpios = dev_get_priv(dev);
70 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
72 imx_iomux_gpio_get_function(gpio, &g_state);
74 if (((g_state & (0x07 << PAD_MUX_MODE_SHIFT)) >> PAD_MUX_MODE_SHIFT) > 0)
76 if (g_state & PAD_CTL_OBE_ENABLE)
78 if (g_state & PAD_CTL_IBE_ENABLE)
80 if (!(g_state & PAD_CTL_OBE_IBE_ENABLE))
86 static const struct dm_gpio_ops gpio_vybrid_ops = {
87 .direction_input = vybrid_gpio_direction_input,
88 .direction_output = vybrid_gpio_direction_output,
89 .get_value = vybrid_gpio_get_value,
90 .set_value = vybrid_gpio_set_value,
91 .get_function = vybrid_gpio_get_function,
94 static int vybrid_gpio_probe(struct udevice *dev)
96 struct vybrid_gpios *gpios = dev_get_priv(dev);
97 struct vybrid_gpio_platdata *plat = dev_get_platdata(dev);
98 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
100 uc_priv->bank_name = plat->port_name;
101 uc_priv->gpio_count = VYBRID_GPIO_COUNT;
102 gpios->reg = (struct vybrid_gpio_regs *)plat->base;
103 gpios->chip = plat->chip;
108 static int vybrid_gpio_bind(struct udevice *dev)
110 struct vybrid_gpio_platdata *plat = dev->platdata;
111 fdt_addr_t base_addr;
116 base_addr = dev_get_addr(dev);
117 if (base_addr == FDT_ADDR_T_NONE)
122 * When every board is converted to driver model and DT is
123 * supported, this can be done by auto-alloc feature, but
124 * not using calloc to alloc memory for platdata.
126 plat = calloc(1, sizeof(*plat));
130 plat->base = base_addr;
131 plat->chip = dev->req_seq;
132 plat->port_name = fdt_get_name(gd->fdt_blob, dev->of_offset, NULL);
133 dev->platdata = plat;
138 #ifndef CONFIG_OF_CONTROL
139 static const struct vybrid_gpio_platdata vybrid_gpio[] = {
140 {0, GPIO0_BASE_ADDR, "GPIO0 "},
141 {1, GPIO1_BASE_ADDR, "GPIO1 "},
142 {2, GPIO2_BASE_ADDR, "GPIO2 "},
143 {3, GPIO3_BASE_ADDR, "GPIO3 "},
144 {4, GPIO4_BASE_ADDR, "GPIO4 "},
147 U_BOOT_DEVICES(vybrid_gpio) = {
148 { "gpio_vybrid", &vybrid_gpio[0] },
149 { "gpio_vybrid", &vybrid_gpio[1] },
150 { "gpio_vybrid", &vybrid_gpio[2] },
151 { "gpio_vybrid", &vybrid_gpio[3] },
152 { "gpio_vybrid", &vybrid_gpio[4] },
156 static const struct udevice_id vybrid_gpio_ids[] = {
157 { .compatible = "fsl,vf610-gpio" },
161 U_BOOT_DRIVER(gpio_vybrid) = {
162 .name = "gpio_vybrid",
164 .ops = &gpio_vybrid_ops,
165 .probe = vybrid_gpio_probe,
166 .priv_auto_alloc_size = sizeof(struct vybrid_gpios),
167 .of_match = vybrid_gpio_ids,
168 .bind = vybrid_gpio_bind,