]> git.sur5r.net Git - u-boot/blob - drivers/gpio/bcm2835_gpio.c
Merge git://git.denx.de/u-boot-sunxi
[u-boot] / drivers / gpio / bcm2835_gpio.c
1 /*
2  * Copyright (C) 2012 Vikram Narayananan
3  * <vikram186@gmail.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <asm/gpio.h>
12 #include <asm/io.h>
13 #include <fdtdec.h>
14
15 struct bcm2835_gpios {
16         struct bcm2835_gpio_regs *reg;
17 };
18
19 static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
20 {
21         struct bcm2835_gpios *gpios = dev_get_priv(dev);
22         unsigned val;
23
24         val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
25         val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
26         val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
27         writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
28
29         return 0;
30 }
31
32 static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
33                                          int value)
34 {
35         struct bcm2835_gpios *gpios = dev_get_priv(dev);
36         unsigned val;
37
38         gpio_set_value(gpio, value);
39
40         val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
41         val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
42         val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
43         writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
44
45         return 0;
46 }
47
48 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
49 {
50         unsigned val;
51
52         val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
53
54         return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
55 }
56
57 static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
58 {
59         const struct bcm2835_gpios *gpios = dev_get_priv(dev);
60
61         return bcm2835_get_value(gpios, gpio);
62 }
63
64 static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
65                                   int value)
66 {
67         struct bcm2835_gpios *gpios = dev_get_priv(dev);
68         u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
69
70         writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
71                                 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
72
73         return 0;
74 }
75
76 int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
77 {
78         struct bcm2835_gpios *gpios = dev_get_priv(dev);
79         u32 val;
80
81         val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
82
83         return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
84 }
85
86 static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
87 {
88         int funcid = bcm2835_gpio_get_func_id(dev, offset);
89
90         switch (funcid) {
91         case BCM2835_GPIO_OUTPUT:
92                 return GPIOF_OUTPUT;
93         case BCM2835_GPIO_INPUT:
94                 return GPIOF_INPUT;
95         default:
96                 return GPIOF_FUNC;
97         }
98 }
99
100
101 static const struct dm_gpio_ops gpio_bcm2835_ops = {
102         .direction_input        = bcm2835_gpio_direction_input,
103         .direction_output       = bcm2835_gpio_direction_output,
104         .get_value              = bcm2835_gpio_get_value,
105         .set_value              = bcm2835_gpio_set_value,
106         .get_function           = bcm2835_gpio_get_function,
107 };
108
109 static int bcm2835_gpio_probe(struct udevice *dev)
110 {
111         struct bcm2835_gpios *gpios = dev_get_priv(dev);
112         struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
113         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
114
115         uc_priv->bank_name = "GPIO";
116         uc_priv->gpio_count = BCM2835_GPIO_COUNT;
117         gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
118
119         return 0;
120 }
121
122 #if CONFIG_IS_ENABLED(OF_CONTROL)
123 static const struct udevice_id bcm2835_gpio_id[] = {
124         {.compatible = "brcm,bcm2835-gpio"},
125         {}
126 };
127
128 static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
129 {
130         struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
131         fdt_addr_t addr;
132
133         addr = devfdt_get_addr(dev);
134         if (addr == FDT_ADDR_T_NONE)
135                 return -EINVAL;
136
137         plat->base = addr;
138         return 0;
139 }
140 #endif
141
142 U_BOOT_DRIVER(gpio_bcm2835) = {
143         .name   = "gpio_bcm2835",
144         .id     = UCLASS_GPIO,
145         .of_match = of_match_ptr(bcm2835_gpio_id),
146         .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
147         .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
148         .ops    = &gpio_bcm2835_ops,
149         .probe  = bcm2835_gpio_probe,
150         .flags  = DM_FLAG_PRE_RELOC,
151         .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
152 };