blob: 7aa8b1f0f8cfcf92c76f16da4eff1166622c92d3 [file] [log] [blame]
Simon Glassb43d0442012-02-15 15:51:13 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glassb43d0442012-02-15 15:51:13 -08004 */
5
6#include <common.h>
Simon Glassb4d70702014-02-26 15:59:25 -07007#include <dm.h>
8#include <fdtdec.h>
9#include <malloc.h>
Simon Glassb43d0442012-02-15 15:51:13 -080010#include <asm/gpio.h>
11
Simon Glassb4d70702014-02-26 15:59:25 -070012DECLARE_GLOBAL_DATA_PTR;
13
Simon Glassb43d0442012-02-15 15:51:13 -080014/* Flags for each GPIO */
15#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
16#define GPIOF_HIGH (1 << 1) /* Currently set high */
17#define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
18
19struct gpio_state {
20 const char *label; /* label given by requester */
21 u8 flags; /* flags (GPIOF_...) */
22};
23
Simon Glassb43d0442012-02-15 15:51:13 -080024/* Access routines for GPIO state */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020025static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080026{
Simon Glassb4d70702014-02-26 15:59:25 -070027 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
28 struct gpio_state *state = dev_get_priv(dev);
29
30 if (offset >= uc_priv->gpio_count) {
Simon Glassb43d0442012-02-15 15:51:13 -080031 static u8 invalid_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070032 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -080033 return &invalid_flags;
34 }
35
Simon Glassb4d70702014-02-26 15:59:25 -070036 return &state[offset].flags;
Simon Glassb43d0442012-02-15 15:51:13 -080037}
38
Heiko Schocherb74fcb42014-05-22 12:43:05 +020039static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
Simon Glassb43d0442012-02-15 15:51:13 -080040{
Simon Glassb4d70702014-02-26 15:59:25 -070041 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080042}
43
Heiko Schocherb74fcb42014-05-22 12:43:05 +020044static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
Simon Glassb4d70702014-02-26 15:59:25 -070045 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080046{
Simon Glassb4d70702014-02-26 15:59:25 -070047 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080048
49 if (value)
50 *gpio |= flag;
51 else
52 *gpio &= ~flag;
53
54 return 0;
55}
56
Heiko Schocherb74fcb42014-05-22 12:43:05 +020057static int check_reserved(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -070058 const char *func)
Simon Glassb43d0442012-02-15 15:51:13 -080059{
Simon Glassb4d70702014-02-26 15:59:25 -070060 if (!get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
61 printf("sandbox_gpio: %s: error: offset %u not reserved\n",
62 func, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080063 return -1;
64 }
65
66 return 0;
67}
68
69/*
70 * Back-channel sandbox-internal-only access to GPIO state
71 */
72
Heiko Schocherb74fcb42014-05-22 12:43:05 +020073int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080074{
Simon Glassb4d70702014-02-26 15:59:25 -070075 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
76 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
77 return get_gpio_flag(dev, offset, GPIOF_HIGH);
Simon Glassb43d0442012-02-15 15:51:13 -080078}
79
Heiko Schocherb74fcb42014-05-22 12:43:05 +020080int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080081{
Simon Glassb4d70702014-02-26 15:59:25 -070082 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glassb43d0442012-02-15 15:51:13 -080083}
84
Heiko Schocherb74fcb42014-05-22 12:43:05 +020085int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080086{
Simon Glassb4d70702014-02-26 15:59:25 -070087 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080088}
89
Heiko Schocherb74fcb42014-05-22 12:43:05 +020090int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080091{
Simon Glassb4d70702014-02-26 15:59:25 -070092 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080093}
94
95/*
96 * These functions implement the public interface within U-Boot
97 */
98
Simon Glassb4d70702014-02-26 15:59:25 -070099/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200100static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800101{
Simon Glassb4d70702014-02-26 15:59:25 -0700102 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800103
Simon Glassb4d70702014-02-26 15:59:25 -0700104 if (check_reserved(dev, offset, __func__))
Simon Glassb43d0442012-02-15 15:51:13 -0800105 return -1;
106
Simon Glassb4d70702014-02-26 15:59:25 -0700107 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800108}
109
Simon Glassb4d70702014-02-26 15:59:25 -0700110/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200111static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700112 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800113{
Simon Glassb4d70702014-02-26 15:59:25 -0700114 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800115
Simon Glassb4d70702014-02-26 15:59:25 -0700116 if (check_reserved(dev, offset, __func__))
Simon Glassb43d0442012-02-15 15:51:13 -0800117 return -1;
118
Simon Glassb4d70702014-02-26 15:59:25 -0700119 return sandbox_gpio_set_direction(dev, offset, 1) |
120 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800121}
122
Simon Glassb4d70702014-02-26 15:59:25 -0700123/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200124static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800125{
Simon Glassb4d70702014-02-26 15:59:25 -0700126 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800127
Simon Glassb4d70702014-02-26 15:59:25 -0700128 if (check_reserved(dev, offset, __func__))
Simon Glassb43d0442012-02-15 15:51:13 -0800129 return -1;
130
Simon Glassb4d70702014-02-26 15:59:25 -0700131 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800132}
133
Simon Glassb4d70702014-02-26 15:59:25 -0700134/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200135static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800136{
Simon Glassb4d70702014-02-26 15:59:25 -0700137 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800138
Simon Glassb4d70702014-02-26 15:59:25 -0700139 if (check_reserved(dev, offset, __func__))
Simon Glassb43d0442012-02-15 15:51:13 -0800140 return -1;
141
Simon Glassb4d70702014-02-26 15:59:25 -0700142 if (!sandbox_gpio_get_direction(dev, offset)) {
143 printf("sandbox_gpio: error: set_value on input gpio %u\n",
144 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800145 return -1;
146 }
147
Simon Glassb4d70702014-02-26 15:59:25 -0700148 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800149}
150
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200151static int sb_gpio_request(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700152 const char *label)
Simon Glassb43d0442012-02-15 15:51:13 -0800153{
Simon Glassb4d70702014-02-26 15:59:25 -0700154 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
155 struct gpio_state *state = dev_get_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800156
Simon Glassb4d70702014-02-26 15:59:25 -0700157 debug("%s: offset:%u, label:%s\n", __func__, offset, label);
158
159 if (offset >= uc_priv->gpio_count) {
160 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800161 return -1;
162 }
163
Simon Glassb4d70702014-02-26 15:59:25 -0700164 if (get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
165 printf("sandbox_gpio: error: gpio %u already reserved\n",
166 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800167 return -1;
168 }
169
Simon Glassb4d70702014-02-26 15:59:25 -0700170 state[offset].label = label;
171 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 1);
Simon Glassb43d0442012-02-15 15:51:13 -0800172}
173
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200174static int sb_gpio_free(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800175{
Simon Glassb4d70702014-02-26 15:59:25 -0700176 struct gpio_state *state = dev_get_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800177
Simon Glassb4d70702014-02-26 15:59:25 -0700178 debug("%s: offset:%u\n", __func__, offset);
179
180 if (check_reserved(dev, offset, __func__))
Simon Glassb43d0442012-02-15 15:51:13 -0800181 return -1;
182
Simon Glassb4d70702014-02-26 15:59:25 -0700183 state[offset].label = NULL;
184 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 0);
185}
186
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200187static int sb_gpio_get_state(struct udevice *dev, unsigned int offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700188 char *buf, int bufsize)
189{
190 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
191 struct gpio_state *state = dev_get_priv(dev);
192 const char *label;
193
194 label = state[offset].label;
195 snprintf(buf, bufsize, "%s%d: %s: %d [%c]%s%s",
196 uc_priv->bank_name ? uc_priv->bank_name : "", offset,
197 sandbox_gpio_get_direction(dev, offset) ? "out" : " in",
198 sandbox_gpio_get_value(dev, offset),
199 get_gpio_flag(dev, offset, GPIOF_RESERVED) ? 'x' : ' ',
200 label ? " " : "",
201 label ? label : "");
202
203 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800204}
205
Simon Glass32048632014-10-04 11:29:45 -0600206static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
207{
208 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
209 return GPIOF_OUTPUT;
210 return GPIOF_INPUT;
211}
212
Simon Glassb4d70702014-02-26 15:59:25 -0700213static const struct dm_gpio_ops gpio_sandbox_ops = {
214 .request = sb_gpio_request,
215 .free = sb_gpio_free,
216 .direction_input = sb_gpio_direction_input,
217 .direction_output = sb_gpio_direction_output,
218 .get_value = sb_gpio_get_value,
219 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600220 .get_function = sb_gpio_get_function,
Simon Glassb4d70702014-02-26 15:59:25 -0700221};
222
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200223static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800224{
Simon Glassb4d70702014-02-26 15:59:25 -0700225 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
Simon Glassb43d0442012-02-15 15:51:13 -0800226
Simon Glassb4d70702014-02-26 15:59:25 -0700227 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
228 "num-gpios", 0);
229 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
230 "gpio-bank-name", NULL);
Simon Glassb43d0442012-02-15 15:51:13 -0800231
Simon Glassb4d70702014-02-26 15:59:25 -0700232 return 0;
233}
Simon Glassb43d0442012-02-15 15:51:13 -0800234
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200235static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700236{
237 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
238
239 if (dev->of_offset == -1) {
240 /* Tell the uclass how many GPIOs we have */
241 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb43d0442012-02-15 15:51:13 -0800242 }
Simon Glassb4d70702014-02-26 15:59:25 -0700243
244 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
245
246 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800247}
Simon Glassb4d70702014-02-26 15:59:25 -0700248
Simon Glass767827a2014-06-11 23:29:45 -0600249static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700250 { .compatible = "sandbox,gpio" },
251 { }
252};
253
254U_BOOT_DRIVER(gpio_sandbox) = {
255 .name = "gpio_sandbox",
256 .id = UCLASS_GPIO,
257 .of_match = sandbox_gpio_ids,
258 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
259 .probe = gpio_sandbox_probe,
260 .ops = &gpio_sandbox_ops,
261};