blob: c2716e71763293529c65062cf34092c99ad7a502 [file] [log] [blame]
Stephan Gerholddf839d52021-07-02 17:06:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2019 Stephan Gerhold */
3
Stephan Gerholddf839d52021-07-02 17:06:18 +02004#include <dm.h>
5#include <asm/gpio.h>
6#include <asm/io.h>
7
8struct nmk_gpio_regs {
9 u32 dat; /* data */
10 u32 dats; /* data set */
11 u32 datc; /* data clear */
12 u32 pdis; /* pull disable */
13 u32 dir; /* direction */
14 u32 dirs; /* direction set */
15 u32 dirc; /* direction clear */
16 u32 slpm; /* sleep mode */
17 u32 afsla; /* alternate function select A */
18 u32 afslb; /* alternate function select B */
19 u32 lowemi; /* low EMI mode */
20};
21
22struct nmk_gpio {
23 struct nmk_gpio_regs *regs;
24};
25
26static int nmk_gpio_get_value(struct udevice *dev, unsigned offset)
27{
28 struct nmk_gpio *priv = dev_get_priv(dev);
29
30 return !!(readl(&priv->regs->dat) & BIT(offset));
31}
32
33static int nmk_gpio_set_value(struct udevice *dev, unsigned offset, int value)
34{
35 struct nmk_gpio *priv = dev_get_priv(dev);
36
37 if (value)
38 writel(BIT(offset), &priv->regs->dats);
39 else
40 writel(BIT(offset), &priv->regs->datc);
41
42 return 0;
43}
44
45static int nmk_gpio_get_function(struct udevice *dev, unsigned offset)
46{
47 struct nmk_gpio *priv = dev_get_priv(dev);
48
49 if (readl(&priv->regs->afsla) & BIT(offset) ||
50 readl(&priv->regs->afslb) & BIT(offset))
51 return GPIOF_FUNC;
52
53 if (readl(&priv->regs->dir) & BIT(offset))
54 return GPIOF_OUTPUT;
55 else
56 return GPIOF_INPUT;
57}
58
59static int nmk_gpio_direction_input(struct udevice *dev, unsigned offset)
60{
61 struct nmk_gpio *priv = dev_get_priv(dev);
62
63 writel(BIT(offset), &priv->regs->dirc);
64 return 0;
65}
66
67static int nmk_gpio_direction_output(struct udevice *dev, unsigned offset,
68 int value)
69{
70 struct nmk_gpio *priv = dev_get_priv(dev);
71
72 writel(BIT(offset), &priv->regs->dirs);
73 return nmk_gpio_set_value(dev, offset, value);
74}
75
76static const struct dm_gpio_ops nmk_gpio_ops = {
77 .get_value = nmk_gpio_get_value,
78 .set_value = nmk_gpio_set_value,
79 .get_function = nmk_gpio_get_function,
80 .direction_input = nmk_gpio_direction_input,
81 .direction_output = nmk_gpio_direction_output,
82};
83
84static int nmk_gpio_probe(struct udevice *dev)
85{
86 struct nmk_gpio *priv = dev_get_priv(dev);
87 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
88 char buf[20];
89 u32 bank;
90 int ret;
91
92 priv->regs = dev_read_addr_ptr(dev);
93 if (!priv->regs)
94 return -EINVAL;
95
96 ret = dev_read_u32(dev, "gpio-bank", &bank);
97 if (ret < 0) {
98 printf("nmk_gpio(%s): Failed to read gpio-bank\n", dev->name);
99 return ret;
100 }
101
102 sprintf(buf, "nmk%u-gpio", bank);
103 uc_priv->bank_name = strdup(buf);
104 if (!uc_priv->bank_name)
105 return -ENOMEM;
106
107 uc_priv->gpio_count = sizeof(priv->regs->dat) * BITS_PER_BYTE;
108
109 return 0;
110}
111
112static const struct udevice_id nmk_gpio_ids[] = {
113 { .compatible = "st,nomadik-gpio" },
114 { }
115};
116
117U_BOOT_DRIVER(gpio_nmk) = {
118 .name = "nmk_gpio",
119 .id = UCLASS_GPIO,
120 .of_match = nmk_gpio_ids,
121 .probe = nmk_gpio_probe,
122 .ops = &nmk_gpio_ops,
123 .priv_auto = sizeof(struct nmk_gpio),
124};