blob: 22b6a5f7941cb1e8776f9f40e4628da7228c1017 [file] [log] [blame]
Simon Glass8d30fcd2012-02-15 15:51:13 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glass8d30fcd2012-02-15 15:51:13 -08004 */
5
6#include <common.h>
Simon Glasse2d8a712014-02-26 15:59:25 -07007#include <dm.h>
8#include <fdtdec.h>
9#include <malloc.h>
Simon Glass8d30fcd2012-02-15 15:51:13 -080010#include <asm/gpio.h>
11
Simon Glasse2d8a712014-02-26 15:59:25 -070012DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass8d30fcd2012-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 Glass8d30fcd2012-02-15 15:51:13 -080024/* Access routines for GPIO state */
Simon Glasse2d8a712014-02-26 15:59:25 -070025static u8 *get_gpio_flags(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080026{
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -080031 static u8 invalid_flags;
Simon Glasse2d8a712014-02-26 15:59:25 -070032 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -080033 return &invalid_flags;
34 }
35
Simon Glasse2d8a712014-02-26 15:59:25 -070036 return &state[offset].flags;
Simon Glass8d30fcd2012-02-15 15:51:13 -080037}
38
Simon Glasse2d8a712014-02-26 15:59:25 -070039static int get_gpio_flag(struct device *dev, unsigned offset, int flag)
Simon Glass8d30fcd2012-02-15 15:51:13 -080040{
Simon Glasse2d8a712014-02-26 15:59:25 -070041 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -080042}
43
Simon Glasse2d8a712014-02-26 15:59:25 -070044static int set_gpio_flag(struct device *dev, unsigned offset, int flag,
45 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080046{
Simon Glasse2d8a712014-02-26 15:59:25 -070047 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -080048
49 if (value)
50 *gpio |= flag;
51 else
52 *gpio &= ~flag;
53
54 return 0;
55}
56
Simon Glasse2d8a712014-02-26 15:59:25 -070057static int check_reserved(struct device *dev, unsigned offset,
58 const char *func)
Simon Glass8d30fcd2012-02-15 15:51:13 -080059{
Simon Glasse2d8a712014-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 Glass8d30fcd2012-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
Simon Glasse2d8a712014-02-26 15:59:25 -070073int sandbox_gpio_get_value(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080074{
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -080078}
79
Simon Glasse2d8a712014-02-26 15:59:25 -070080int sandbox_gpio_set_value(struct device *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080081{
Simon Glasse2d8a712014-02-26 15:59:25 -070082 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -080083}
84
Simon Glasse2d8a712014-02-26 15:59:25 -070085int sandbox_gpio_get_direction(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080086{
Simon Glasse2d8a712014-02-26 15:59:25 -070087 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glass8d30fcd2012-02-15 15:51:13 -080088}
89
Simon Glasse2d8a712014-02-26 15:59:25 -070090int sandbox_gpio_set_direction(struct device *dev, unsigned offset, int output)
Simon Glass8d30fcd2012-02-15 15:51:13 -080091{
Simon Glasse2d8a712014-02-26 15:59:25 -070092 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glass8d30fcd2012-02-15 15:51:13 -080093}
94
95/*
96 * These functions implement the public interface within U-Boot
97 */
98
Simon Glasse2d8a712014-02-26 15:59:25 -070099/* set GPIO port 'offset' as an input */
100static int sb_gpio_direction_input(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800101{
Simon Glasse2d8a712014-02-26 15:59:25 -0700102 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800103
Simon Glasse2d8a712014-02-26 15:59:25 -0700104 if (check_reserved(dev, offset, __func__))
Simon Glass8d30fcd2012-02-15 15:51:13 -0800105 return -1;
106
Simon Glasse2d8a712014-02-26 15:59:25 -0700107 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800108}
109
Simon Glasse2d8a712014-02-26 15:59:25 -0700110/* set GPIO port 'offset' as an output, with polarity 'value' */
111static int sb_gpio_direction_output(struct device *dev, unsigned offset,
112 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800113{
Simon Glasse2d8a712014-02-26 15:59:25 -0700114 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800115
Simon Glasse2d8a712014-02-26 15:59:25 -0700116 if (check_reserved(dev, offset, __func__))
Simon Glass8d30fcd2012-02-15 15:51:13 -0800117 return -1;
118
Simon Glasse2d8a712014-02-26 15:59:25 -0700119 return sandbox_gpio_set_direction(dev, offset, 1) |
120 sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800121}
122
Simon Glasse2d8a712014-02-26 15:59:25 -0700123/* read GPIO IN value of port 'offset' */
124static int sb_gpio_get_value(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800125{
Simon Glasse2d8a712014-02-26 15:59:25 -0700126 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800127
Simon Glasse2d8a712014-02-26 15:59:25 -0700128 if (check_reserved(dev, offset, __func__))
Simon Glass8d30fcd2012-02-15 15:51:13 -0800129 return -1;
130
Simon Glasse2d8a712014-02-26 15:59:25 -0700131 return sandbox_gpio_get_value(dev, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800132}
133
Simon Glasse2d8a712014-02-26 15:59:25 -0700134/* write GPIO OUT value to port 'offset' */
135static int sb_gpio_set_value(struct device *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800136{
Simon Glasse2d8a712014-02-26 15:59:25 -0700137 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800138
Simon Glasse2d8a712014-02-26 15:59:25 -0700139 if (check_reserved(dev, offset, __func__))
Simon Glass8d30fcd2012-02-15 15:51:13 -0800140 return -1;
141
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -0800145 return -1;
146 }
147
Simon Glasse2d8a712014-02-26 15:59:25 -0700148 return sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800149}
150
Simon Glasse2d8a712014-02-26 15:59:25 -0700151static int sb_gpio_request(struct device *dev, unsigned offset,
152 const char *label)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800153{
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -0800156
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -0800161 return -1;
162 }
163
Simon Glasse2d8a712014-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 Glass8d30fcd2012-02-15 15:51:13 -0800167 return -1;
168 }
169
Simon Glasse2d8a712014-02-26 15:59:25 -0700170 state[offset].label = label;
171 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 1);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800172}
173
Simon Glasse2d8a712014-02-26 15:59:25 -0700174static int sb_gpio_free(struct device *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800175{
Simon Glasse2d8a712014-02-26 15:59:25 -0700176 struct gpio_state *state = dev_get_priv(dev);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800177
Simon Glasse2d8a712014-02-26 15:59:25 -0700178 debug("%s: offset:%u\n", __func__, offset);
179
180 if (check_reserved(dev, offset, __func__))
Simon Glass8d30fcd2012-02-15 15:51:13 -0800181 return -1;
182
Simon Glasse2d8a712014-02-26 15:59:25 -0700183 state[offset].label = NULL;
184 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 0);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800185}
186
Simon Glasse2d8a712014-02-26 15:59:25 -0700187static int sb_gpio_get_state(struct device *dev, unsigned int offset,
188 char *buf, int bufsize)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800189{
Simon Glasse2d8a712014-02-26 15:59:25 -0700190 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
191 struct gpio_state *state = dev_get_priv(dev);
192 const char *label;
Simon Glass8d30fcd2012-02-15 15:51:13 -0800193
Simon Glasse2d8a712014-02-26 15:59:25 -0700194 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 : "");
Simon Glass8d30fcd2012-02-15 15:51:13 -0800202
Simon Glasse2d8a712014-02-26 15:59:25 -0700203 return 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -0800204}
Simon Glasse2d8a712014-02-26 15:59:25 -0700205
206static const struct dm_gpio_ops gpio_sandbox_ops = {
207 .request = sb_gpio_request,
208 .free = sb_gpio_free,
209 .direction_input = sb_gpio_direction_input,
210 .direction_output = sb_gpio_direction_output,
211 .get_value = sb_gpio_get_value,
212 .set_value = sb_gpio_set_value,
213 .get_state = sb_gpio_get_state,
214};
215
216static int sandbox_gpio_ofdata_to_platdata(struct device *dev)
217{
218 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
219
220 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
221 "num-gpios", 0);
222 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
223 "gpio-bank-name", NULL);
224
225 return 0;
226}
227
228static int gpio_sandbox_probe(struct device *dev)
229{
230 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
231
232 if (dev->of_offset == -1) {
233 /* Tell the uclass how many GPIOs we have */
234 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
235 }
236
237 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
238
239 return 0;
240}
241
242static const struct device_id sandbox_gpio_ids[] = {
243 { .compatible = "sandbox,gpio" },
244 { }
245};
246
247U_BOOT_DRIVER(gpio_sandbox) = {
248 .name = "gpio_sandbox",
249 .id = UCLASS_GPIO,
250 .of_match = sandbox_gpio_ids,
251 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
252 .probe = gpio_sandbox_probe,
253 .ops = &gpio_sandbox_ops,
254};