]> Git Repo - J-u-boot.git/blob - drivers/gpio/npcm_gpio.c
net: mediatek: fix gmac2 usability for mt7629
[J-u-boot.git] / drivers / gpio / npcm_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2022 Nuvoton Technology Corp.
4  */
5
6 #include <dm.h>
7 #include <asm/gpio.h>
8 #include <linux/io.h>
9
10 #define NPCM_GPIOS_PER_BANK    32
11
12 /* Register offsets */
13 #define GPIO_DIN                0x4     /* RO - Data In */
14 #define GPIO_DOUT               0xC     /* RW - Data Out */
15 #define GPIO_OE                 0x10    /* RW - Output Enable */
16 #define GPIO_IEM                0x58    /* RW - Input Enable Mask */
17 #define GPIO_OES                0x70    /* WO - Output Enable Register Set */
18 #define GPIO_OEC                0x74    /* WO - Output Enable Register Clear */
19
20 struct npcm_gpio_priv {
21         void __iomem *base;
22 };
23
24 static int npcm_gpio_direction_input(struct udevice *dev, unsigned int offset)
25 {
26         struct npcm_gpio_priv *priv = dev_get_priv(dev);
27
28         writel(BIT(offset), priv->base + GPIO_OEC);
29         setbits_le32(priv->base + GPIO_IEM, BIT(offset));
30
31         return 0;
32 }
33
34 static int npcm_gpio_direction_output(struct udevice *dev, unsigned int offset,
35                                       int value)
36 {
37         struct npcm_gpio_priv *priv = dev_get_priv(dev);
38
39         if (value)
40                 setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
41         else
42                 clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
43
44         clrbits_le32(priv->base + GPIO_IEM, BIT(offset));
45         writel(BIT(offset), priv->base + GPIO_OES);
46
47         return 0;
48 }
49
50 static int npcm_gpio_get_value(struct udevice *dev, unsigned int offset)
51 {
52         struct npcm_gpio_priv *priv = dev_get_priv(dev);
53
54         if (readl(priv->base + GPIO_IEM) & BIT(offset))
55                 return !!(readl(priv->base + GPIO_DIN) & BIT(offset));
56
57         if (readl(priv->base + GPIO_OE) & BIT(offset))
58                 return !!(readl(priv->base + GPIO_DOUT) & BIT(offset));
59
60         return -EINVAL;
61 }
62
63 static int npcm_gpio_set_value(struct udevice *dev, unsigned int offset,
64                                int value)
65 {
66         struct npcm_gpio_priv *priv = dev_get_priv(dev);
67
68         if (value)
69                 setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
70         else
71                 clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
72
73         return 0;
74 }
75
76 static int npcm_gpio_get_function(struct udevice *dev, unsigned int offset)
77 {
78         struct npcm_gpio_priv *priv = dev_get_priv(dev);
79
80         if (readl(priv->base + GPIO_IEM) & BIT(offset))
81                 return GPIOF_INPUT;
82
83         if (readl(priv->base + GPIO_OE) & BIT(offset))
84                 return GPIOF_OUTPUT;
85
86         return GPIOF_FUNC;
87 }
88
89 static const struct dm_gpio_ops npcm_gpio_ops = {
90         .direction_input        = npcm_gpio_direction_input,
91         .direction_output       = npcm_gpio_direction_output,
92         .get_value              = npcm_gpio_get_value,
93         .set_value              = npcm_gpio_set_value,
94         .get_function           = npcm_gpio_get_function,
95 };
96
97 static int npcm_gpio_probe(struct udevice *dev)
98 {
99         struct npcm_gpio_priv *priv = dev_get_priv(dev);
100         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
101
102         priv->base = dev_read_addr_ptr(dev);
103         uc_priv->gpio_count = NPCM_GPIOS_PER_BANK;
104         uc_priv->bank_name = dev->name;
105
106         return 0;
107 }
108
109 static const struct udevice_id npcm_gpio_match[] = {
110         { .compatible = "nuvoton,npcm845-gpio" },
111         { .compatible = "nuvoton,npcm750-gpio" },
112         { }
113 };
114
115 U_BOOT_DRIVER(npcm_gpio) = {
116         .name   = "npcm_gpio",
117         .id     = UCLASS_GPIO,
118         .of_match = npcm_gpio_match,
119         .probe  = npcm_gpio_probe,
120         .priv_auto = sizeof(struct npcm_gpio_priv),
121         .ops    = &npcm_gpio_ops,
122 };
This page took 0.031419 seconds and 4 git commands to generate.