]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/gpio/sandbox.c
Merge branch 'master' of git://www.denx.de/git/u-boot-dm
[people/ms/u-boot.git] / drivers / gpio / sandbox.c
CommitLineData
8d30fcd9
SG
1/*
2 * Copyright (c) 2011 The Chromium OS Authors.
1a459660 3 * SPDX-License-Identifier: GPL-2.0+
8d30fcd9
SG
4 */
5
6#include <common.h>
e2d8a714
SG
7#include <dm.h>
8#include <fdtdec.h>
9#include <malloc.h>
8d30fcd9 10#include <asm/gpio.h>
3669e0e7 11#include <dt-bindings/gpio/gpio.h>
8d30fcd9 12
e2d8a714
SG
13DECLARE_GLOBAL_DATA_PTR;
14
8d30fcd9
SG
15/* Flags for each GPIO */
16#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
17#define GPIOF_HIGH (1 << 1) /* Currently set high */
8d30fcd9
SG
18
19struct gpio_state {
20 const char *label; /* label given by requester */
21 u8 flags; /* flags (GPIOF_...) */
22};
23
8d30fcd9 24/* Access routines for GPIO state */
54c5d08a 25static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
8d30fcd9 26{
e564f054 27 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
e2d8a714
SG
28 struct gpio_state *state = dev_get_priv(dev);
29
30 if (offset >= uc_priv->gpio_count) {
8d30fcd9 31 static u8 invalid_flags;
e2d8a714 32 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
8d30fcd9
SG
33 return &invalid_flags;
34 }
35
e2d8a714 36 return &state[offset].flags;
8d30fcd9
SG
37}
38
54c5d08a 39static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
8d30fcd9 40{
e2d8a714 41 return (*get_gpio_flags(dev, offset) & flag) != 0;
8d30fcd9
SG
42}
43
54c5d08a 44static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
e2d8a714 45 int value)
8d30fcd9 46{
e2d8a714 47 u8 *gpio = get_gpio_flags(dev, offset);
8d30fcd9
SG
48
49 if (value)
50 *gpio |= flag;
51 else
52 *gpio &= ~flag;
53
54 return 0;
55}
56
8d30fcd9
SG
57/*
58 * Back-channel sandbox-internal-only access to GPIO state
59 */
60
54c5d08a 61int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
8d30fcd9 62{
e2d8a714
SG
63 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
64 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
65 return get_gpio_flag(dev, offset, GPIOF_HIGH);
8d30fcd9
SG
66}
67
54c5d08a 68int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
8d30fcd9 69{
e2d8a714 70 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
8d30fcd9
SG
71}
72
54c5d08a 73int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
8d30fcd9 74{
e2d8a714 75 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
8d30fcd9
SG
76}
77
54c5d08a 78int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
8d30fcd9 79{
e2d8a714 80 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
8d30fcd9
SG
81}
82
83/*
84 * These functions implement the public interface within U-Boot
85 */
86
e2d8a714 87/* set GPIO port 'offset' as an input */
54c5d08a 88static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
8d30fcd9 89{
e2d8a714 90 debug("%s: offset:%u\n", __func__, offset);
8d30fcd9 91
e2d8a714 92 return sandbox_gpio_set_direction(dev, offset, 0);
8d30fcd9
SG
93}
94
e2d8a714 95/* set GPIO port 'offset' as an output, with polarity 'value' */
54c5d08a 96static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
e2d8a714 97 int value)
8d30fcd9 98{
e2d8a714 99 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
8d30fcd9 100
e2d8a714
SG
101 return sandbox_gpio_set_direction(dev, offset, 1) |
102 sandbox_gpio_set_value(dev, offset, value);
8d30fcd9
SG
103}
104
e2d8a714 105/* read GPIO IN value of port 'offset' */
54c5d08a 106static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
8d30fcd9 107{
e2d8a714 108 debug("%s: offset:%u\n", __func__, offset);
8d30fcd9 109
e2d8a714 110 return sandbox_gpio_get_value(dev, offset);
8d30fcd9
SG
111}
112
e2d8a714 113/* write GPIO OUT value to port 'offset' */
54c5d08a 114static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
8d30fcd9 115{
e2d8a714 116 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
8d30fcd9 117
e2d8a714
SG
118 if (!sandbox_gpio_get_direction(dev, offset)) {
119 printf("sandbox_gpio: error: set_value on input gpio %u\n",
120 offset);
8d30fcd9
SG
121 return -1;
122 }
123
e2d8a714 124 return sandbox_gpio_set_value(dev, offset, value);
8d30fcd9
SG
125}
126
699ea960
SG
127static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
128{
129 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
130 return GPIOF_OUTPUT;
131 return GPIOF_INPUT;
132}
133
3669e0e7
SG
134static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
135 struct fdtdec_phandle_args *args)
136{
137 desc->offset = args->args[0];
138 if (args->args_count < 2)
139 return 0;
140 if (args->args[1] & GPIO_ACTIVE_LOW)
141 desc->flags |= GPIOD_ACTIVE_LOW;
142 if (args->args[1] & 2)
143 desc->flags |= GPIOD_IS_IN;
144 if (args->args[1] & 4)
145 desc->flags |= GPIOD_IS_OUT;
146 if (args->args[1] & 8)
147 desc->flags |= GPIOD_IS_OUT_ACTIVE;
148
149 return 0;
150}
151
e2d8a714 152static const struct dm_gpio_ops gpio_sandbox_ops = {
e2d8a714
SG
153 .direction_input = sb_gpio_direction_input,
154 .direction_output = sb_gpio_direction_output,
155 .get_value = sb_gpio_get_value,
156 .set_value = sb_gpio_set_value,
699ea960 157 .get_function = sb_gpio_get_function,
3669e0e7 158 .xlate = sb_gpio_xlate,
e2d8a714
SG
159};
160
54c5d08a 161static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
e2d8a714 162{
e564f054 163 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
8d30fcd9 164
e2d8a714
SG
165 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
166 "num-gpios", 0);
167 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
168 "gpio-bank-name", NULL);
8d30fcd9 169
e2d8a714
SG
170 return 0;
171}
172
54c5d08a 173static int gpio_sandbox_probe(struct udevice *dev)
e2d8a714 174{
e564f054 175 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
e2d8a714
SG
176
177 if (dev->of_offset == -1) {
178 /* Tell the uclass how many GPIOs we have */
179 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
8d30fcd9 180 }
e2d8a714
SG
181
182 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
183
184 return 0;
8d30fcd9 185}
e2d8a714 186
62cb89d5
SG
187static int gpio_sandbox_remove(struct udevice *dev)
188{
189 free(dev->priv);
190
191 return 0;
192}
193
ae7f4513 194static const struct udevice_id sandbox_gpio_ids[] = {
e2d8a714
SG
195 { .compatible = "sandbox,gpio" },
196 { }
197};
198
199U_BOOT_DRIVER(gpio_sandbox) = {
200 .name = "gpio_sandbox",
201 .id = UCLASS_GPIO,
202 .of_match = sandbox_gpio_ids,
203 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
204 .probe = gpio_sandbox_probe,
62cb89d5 205 .remove = gpio_sandbox_remove,
e2d8a714
SG
206 .ops = &gpio_sandbox_ops,
207};