Merge branch '2020-03-31-master-imports'
[oweals/u-boot.git] / drivers / gpio / cortina_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Cortina-Access
4  *
5  * GPIO Driver for Cortina Access CAxxxx Line of SoCs
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <asm/io.h>
11 #include <asm/gpio.h>
12 #include <linux/compat.h>
13 #include <linux/compiler.h>
14
15 /* GPIO Register Map */
16 #define CORTINA_GPIO_CFG        0x00
17 #define CORTINA_GPIO_OUT        0x04
18 #define CORTINA_GPIO_IN         0x08
19 #define CORTINA_GPIO_LVL        0x0C
20 #define CORTINA_GPIO_EDGE       0x10
21 #define CORTINA_GPIO_BOTHEDGE   0x14
22 #define CORTINA_GPIO_IE         0x18
23 #define CORTINA_GPIO_INT        0x1C
24 #define CORTINA_GPIO_STAT       0x20
25
26 struct cortina_gpio_bank {
27         void __iomem *base;
28 };
29
30 #ifdef CONFIG_DM_GPIO
31 static int ca_gpio_direction_input(struct udevice *dev, unsigned int offset)
32 {
33         struct cortina_gpio_bank *priv = dev_get_priv(dev);
34
35         setbits_32(priv->base, BIT(offset));
36         return 0;
37 }
38
39 static int
40 ca_gpio_direction_output(struct udevice *dev, unsigned int offset, int value)
41 {
42         struct cortina_gpio_bank *priv = dev_get_priv(dev);
43
44         clrbits_32(priv->base, BIT(offset));
45         return 0;
46 }
47
48 static int ca_gpio_get_value(struct udevice *dev, unsigned int offset)
49 {
50         struct cortina_gpio_bank *priv = dev_get_priv(dev);
51
52         return readl(priv->base + CORTINA_GPIO_IN) & BIT(offset);
53 }
54
55 static int ca_gpio_set_value(struct udevice *dev, unsigned int offset,
56                              int value)
57 {
58         struct cortina_gpio_bank *priv = dev_get_priv(dev);
59
60         setbits_32(priv->base + CORTINA_GPIO_OUT, BIT(offset));
61         return 0;
62 }
63
64 static int ca_gpio_get_function(struct udevice *dev, unsigned int offset)
65 {
66         struct cortina_gpio_bank *priv = dev_get_priv(dev);
67
68         if (readl(priv->base) & BIT(offset))
69                 return GPIOF_INPUT;
70         else
71                 return GPIOF_OUTPUT;
72 }
73
74 static const struct dm_gpio_ops gpio_cortina_ops = {
75         .direction_input = ca_gpio_direction_input,
76         .direction_output = ca_gpio_direction_output,
77         .get_value = ca_gpio_get_value,
78         .set_value = ca_gpio_set_value,
79         .get_function = ca_gpio_get_function,
80 };
81
82 static int ca_gpio_probe(struct udevice *dev)
83 {
84         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
85         struct cortina_gpio_bank *priv = dev_get_priv(dev);
86
87         priv->base = dev_remap_addr_index(dev, 0);
88         if (!priv->base)
89                 return -EINVAL;
90
91         uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", 32);
92         uc_priv->bank_name = dev->name;
93
94         debug("Done Cortina GPIO init\n");
95         return 0;
96 }
97
98 static const struct udevice_id ca_gpio_ids[] = {
99         {.compatible = "cortina,ca-gpio"},
100         {}
101 };
102
103 U_BOOT_DRIVER(cortina_gpio) = {
104         .name = "cortina-gpio",
105         .id = UCLASS_GPIO,
106         .ops = &gpio_cortina_ops,
107         .probe = ca_gpio_probe,
108         .priv_auto_alloc_size = sizeof(struct cortina_gpio_bank),
109         .of_match = ca_gpio_ids,
110 };
111 #endif /* CONFIG_DM_GPIO */