1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2019 Stephan Gerhold */
11 u32 dats; /* data set */
12 u32 datc; /* data clear */
13 u32 pdis; /* pull disable */
14 u32 dir; /* direction */
15 u32 dirs; /* direction set */
16 u32 dirc; /* direction clear */
17 u32 slpm; /* sleep mode */
18 u32 afsla; /* alternate function select A */
19 u32 afslb; /* alternate function select B */
20 u32 lowemi; /* low EMI mode */
24 struct nmk_gpio_regs *regs;
27 static int nmk_gpio_get_value(struct udevice *dev, unsigned offset)
29 struct nmk_gpio *priv = dev_get_priv(dev);
31 return !!(readl(&priv->regs->dat) & BIT(offset));
34 static int nmk_gpio_set_value(struct udevice *dev, unsigned offset, int value)
36 struct nmk_gpio *priv = dev_get_priv(dev);
39 writel(BIT(offset), &priv->regs->dats);
41 writel(BIT(offset), &priv->regs->datc);
46 static int nmk_gpio_get_function(struct udevice *dev, unsigned offset)
48 struct nmk_gpio *priv = dev_get_priv(dev);
50 if (readl(&priv->regs->afsla) & BIT(offset) ||
51 readl(&priv->regs->afslb) & BIT(offset))
54 if (readl(&priv->regs->dir) & BIT(offset))
60 static int nmk_gpio_direction_input(struct udevice *dev, unsigned offset)
62 struct nmk_gpio *priv = dev_get_priv(dev);
64 writel(BIT(offset), &priv->regs->dirc);
68 static int nmk_gpio_direction_output(struct udevice *dev, unsigned offset,
71 struct nmk_gpio *priv = dev_get_priv(dev);
73 writel(BIT(offset), &priv->regs->dirs);
74 return nmk_gpio_set_value(dev, offset, value);
77 static const struct dm_gpio_ops nmk_gpio_ops = {
78 .get_value = nmk_gpio_get_value,
79 .set_value = nmk_gpio_set_value,
80 .get_function = nmk_gpio_get_function,
81 .direction_input = nmk_gpio_direction_input,
82 .direction_output = nmk_gpio_direction_output,
85 static int nmk_gpio_probe(struct udevice *dev)
87 struct nmk_gpio *priv = dev_get_priv(dev);
88 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
93 priv->regs = dev_read_addr_ptr(dev);
97 ret = dev_read_u32(dev, "gpio-bank", &bank);
99 printf("nmk_gpio(%s): Failed to read gpio-bank\n", dev->name);
103 sprintf(buf, "nmk%u-gpio", bank);
104 uc_priv->bank_name = strdup(buf);
105 if (!uc_priv->bank_name)
108 uc_priv->gpio_count = sizeof(priv->regs->dat) * BITS_PER_BYTE;
113 static const struct udevice_id nmk_gpio_ids[] = {
114 { .compatible = "st,nomadik-gpio" },
118 U_BOOT_DRIVER(gpio_nmk) = {
121 .of_match = nmk_gpio_ids,
122 .probe = nmk_gpio_probe,
123 .ops = &nmk_gpio_ops,
124 .priv_auto = sizeof(struct nmk_gpio),