blob: 332cfc2b231835ca7c4ffd9f0ead44b2a8351730 [file] [log] [blame]
Stephen Warrenefad6cf2012-08-05 16:07:21 +00001/*
2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stephen Warrenefad6cf2012-08-05 16:07:21 +00006 */
7
8#include <common.h>
Simon Glass41e98e02014-09-22 17:30:56 -06009#include <dm.h>
10#include <errno.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000011#include <asm/gpio.h>
12#include <asm/io.h>
13
Simon Glass41e98e02014-09-22 17:30:56 -060014#define GPIO_NAME_SIZE 20
15
16struct bcm2835_gpios {
17 char label[BCM2835_GPIO_COUNT][GPIO_NAME_SIZE];
18 struct bcm2835_gpio_regs *reg;
19};
20
21/**
22 * gpio_is_requested() - check if a GPIO has been requested
23 *
24 * @bank: Bank to check
25 * @offset: GPIO offset within bank to check
26 * @return true if marked as requested, false if not
27 */
28static inline bool gpio_is_requested(struct bcm2835_gpios *gpios, int offset)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000029{
Simon Glass41e98e02014-09-22 17:30:56 -060030 return *gpios->label[offset] != '\0';
Stephen Warrenefad6cf2012-08-05 16:07:21 +000031}
32
Simon Glass41e98e02014-09-22 17:30:56 -060033static int check_requested(struct udevice *dev, unsigned offset,
34 const char *func)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000035{
Simon Glass41e98e02014-09-22 17:30:56 -060036 struct bcm2835_gpios *gpios = dev_get_priv(dev);
37 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
Stephen Warrenefad6cf2012-08-05 16:07:21 +000038
Simon Glass41e98e02014-09-22 17:30:56 -060039 if (!gpio_is_requested(gpios, offset)) {
40 printf("omap_gpio: %s: error: gpio %s%d not requested\n",
41 func, uc_priv->bank_name, offset);
42 return -EPERM;
43 }
44
Stephen Warrenefad6cf2012-08-05 16:07:21 +000045 return 0;
46}
47
Simon Glass41e98e02014-09-22 17:30:56 -060048static int bcm2835_gpio_request(struct udevice *dev, unsigned offset,
49 const char *label)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000050{
Simon Glass41e98e02014-09-22 17:30:56 -060051 struct bcm2835_gpios *gpios = dev_get_priv(dev);
52
53 if (gpio_is_requested(gpios, offset))
54 return -EBUSY;
55
56 strncpy(gpios->label[offset], label, GPIO_NAME_SIZE);
57 gpios->label[offset][GPIO_NAME_SIZE - 1] = '\0';
58
59 return 0;
60}
61
62static int bcm2835_gpio_free(struct udevice *dev, unsigned offset)
63{
64 struct bcm2835_gpios *gpios = dev_get_priv(dev);
65 int ret;
66
67 ret = check_requested(dev, offset, __func__);
68 if (ret)
69 return ret;
70 gpios->label[offset][0] = '\0';
71
72 return 0;
73}
74
75static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
76{
77 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000078 unsigned val;
79
Simon Glass41e98e02014-09-22 17:30:56 -060080 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000081 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
82 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060083 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000084
85 return 0;
86}
87
Simon Glass41e98e02014-09-22 17:30:56 -060088static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
89 int value)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000090{
Simon Glass41e98e02014-09-22 17:30:56 -060091 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000092 unsigned val;
93
94 gpio_set_value(gpio, value);
95
Simon Glass41e98e02014-09-22 17:30:56 -060096 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000097 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
98 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060099 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000100
101 return 0;
102}
103
Simon Glass41e98e02014-09-22 17:30:56 -0600104static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000105{
Simon Glass41e98e02014-09-22 17:30:56 -0600106 u32 val;
107
108 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
109 val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio);
110 return val ? true : false;
111}
112
113static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
114{
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000115 unsigned val;
116
Simon Glass41e98e02014-09-22 17:30:56 -0600117 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000118
119 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
120}
121
Simon Glass41e98e02014-09-22 17:30:56 -0600122static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000123{
Simon Glass41e98e02014-09-22 17:30:56 -0600124 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
125
126 return bcm2835_get_value(gpios, gpio);
127}
128
129static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
130 int value)
131{
132 struct bcm2835_gpios *gpios = dev_get_priv(dev);
133 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
Stephen Warrenefad6cf2012-08-05 16:07:21 +0000134
135 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
136 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
137
138 return 0;
139}
Simon Glass41e98e02014-09-22 17:30:56 -0600140
141static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
142{
143 struct bcm2835_gpios *gpios = dev_get_priv(dev);
144
145 if (!gpio_is_requested(gpios, offset))
146 return GPIOF_UNUSED;
147
148 /* GPIOF_FUNC is not implemented yet */
149 if (bcm2835_gpio_is_output(gpios, offset))
150 return GPIOF_OUTPUT;
151 else
152 return GPIOF_INPUT;
153}
154
155static int bcm2835_gpio_get_state(struct udevice *dev, unsigned int offset,
156 char *buf, int bufsize)
157{
158 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
159 struct bcm2835_gpios *gpios = dev_get_priv(dev);
160 const char *label;
161 bool requested;
162 bool is_output;
163 int size;
164
165 label = gpios->label[offset];
166 is_output = bcm2835_gpio_is_output(gpios, offset);
167 size = snprintf(buf, bufsize, "%s%d: ",
168 uc_priv->bank_name ? uc_priv->bank_name : "", offset);
169 buf += size;
170 bufsize -= size;
171 requested = gpio_is_requested(gpios, offset);
172 snprintf(buf, bufsize, "%s: %d [%c]%s%s",
173 is_output ? "out" : " in",
174 bcm2835_get_value(gpios, offset),
175 requested ? 'x' : ' ',
176 requested ? " " : "",
177 label);
178
179 return 0;
180}
181
182static const struct dm_gpio_ops gpio_bcm2835_ops = {
183 .request = bcm2835_gpio_request,
184 .free = bcm2835_gpio_free,
185 .direction_input = bcm2835_gpio_direction_input,
186 .direction_output = bcm2835_gpio_direction_output,
187 .get_value = bcm2835_gpio_get_value,
188 .set_value = bcm2835_gpio_set_value,
189 .get_function = bcm2835_gpio_get_function,
190 .get_state = bcm2835_gpio_get_state,
191};
192
193static int bcm2835_gpio_probe(struct udevice *dev)
194{
195 struct bcm2835_gpios *gpios = dev_get_priv(dev);
196 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
197 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
198
199 uc_priv->bank_name = "GPIO";
200 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
201 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
202
203 return 0;
204}
205
206U_BOOT_DRIVER(gpio_bcm2835) = {
207 .name = "gpio_bcm2835",
208 .id = UCLASS_GPIO,
209 .ops = &gpio_bcm2835_ops,
210 .probe = bcm2835_gpio_probe,
211 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
212};