blob: ae6d93013f7c79a4bc84ab6be9acd5fd052c736e [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>
Simon Glass16e10402015-01-05 20:05:29 -070011#include <dt-bindings/gpio/gpio.h>
Simon Glassb43d0442012-02-15 15:51:13 -080012
Simon Glassb4d70702014-02-26 15:59:25 -070013DECLARE_GLOBAL_DATA_PTR;
14
Simon Glassb43d0442012-02-15 15:51:13 -080015/* Flags for each GPIO */
16#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
17#define GPIOF_HIGH (1 << 1) /* Currently set high */
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020018#define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
Simon Glassb43d0442012-02-15 15:51:13 -080019
20struct gpio_state {
21 const char *label; /* label given by requester */
22 u8 flags; /* flags (GPIOF_...) */
23};
24
Simon Glassb43d0442012-02-15 15:51:13 -080025/* Access routines for GPIO state */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020026static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080027{
Simon Glassde0977b2015-03-05 12:25:20 -070028 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -070029 struct gpio_state *state = dev_get_priv(dev);
30
31 if (offset >= uc_priv->gpio_count) {
Simon Glassb43d0442012-02-15 15:51:13 -080032 static u8 invalid_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070033 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -080034 return &invalid_flags;
35 }
36
Simon Glassb4d70702014-02-26 15:59:25 -070037 return &state[offset].flags;
Simon Glassb43d0442012-02-15 15:51:13 -080038}
39
Heiko Schocherb74fcb42014-05-22 12:43:05 +020040static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
Simon Glassb43d0442012-02-15 15:51:13 -080041{
Simon Glassb4d70702014-02-26 15:59:25 -070042 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080043}
44
Heiko Schocherb74fcb42014-05-22 12:43:05 +020045static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
Simon Glassb4d70702014-02-26 15:59:25 -070046 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080047{
Simon Glassb4d70702014-02-26 15:59:25 -070048 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080049
50 if (value)
51 *gpio |= flag;
52 else
53 *gpio &= ~flag;
54
55 return 0;
56}
57
Simon Glassb43d0442012-02-15 15:51:13 -080058/*
59 * Back-channel sandbox-internal-only access to GPIO state
60 */
61
Heiko Schocherb74fcb42014-05-22 12:43:05 +020062int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080063{
Simon Glassb4d70702014-02-26 15:59:25 -070064 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
65 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
66 return get_gpio_flag(dev, offset, GPIOF_HIGH);
Simon Glassb43d0442012-02-15 15:51:13 -080067}
68
Heiko Schocherb74fcb42014-05-22 12:43:05 +020069int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080070{
Simon Glassb4d70702014-02-26 15:59:25 -070071 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glassb43d0442012-02-15 15:51:13 -080072}
73
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020074int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
75{
76 return get_gpio_flag(dev, offset, GPIOF_ODR);
77}
78
79int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
80{
81 return set_gpio_flag(dev, offset, GPIOF_ODR, value);
82}
83
Heiko Schocherb74fcb42014-05-22 12:43:05 +020084int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080085{
Simon Glassb4d70702014-02-26 15:59:25 -070086 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080087}
88
Heiko Schocherb74fcb42014-05-22 12:43:05 +020089int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080090{
Simon Glassb4d70702014-02-26 15:59:25 -070091 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080092}
93
94/*
95 * These functions implement the public interface within U-Boot
96 */
97
Simon Glassb4d70702014-02-26 15:59:25 -070098/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020099static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800100{
Simon Glassb4d70702014-02-26 15:59:25 -0700101 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800102
Simon Glassb4d70702014-02-26 15:59:25 -0700103 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800104}
105
Simon Glassb4d70702014-02-26 15:59:25 -0700106/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200107static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700108 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800109{
Simon Glassb4d70702014-02-26 15:59:25 -0700110 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800111
Simon Glassb4d70702014-02-26 15:59:25 -0700112 return sandbox_gpio_set_direction(dev, offset, 1) |
113 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800114}
115
Simon Glassb4d70702014-02-26 15:59:25 -0700116/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200117static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800118{
Simon Glassb4d70702014-02-26 15:59:25 -0700119 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800120
Simon Glassb4d70702014-02-26 15:59:25 -0700121 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800122}
123
Simon Glassb4d70702014-02-26 15:59:25 -0700124/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200125static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800126{
Simon Glassb4d70702014-02-26 15:59:25 -0700127 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800128
Simon Glassb4d70702014-02-26 15:59:25 -0700129 if (!sandbox_gpio_get_direction(dev, offset)) {
130 printf("sandbox_gpio: error: set_value on input gpio %u\n",
131 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800132 return -1;
133 }
134
Simon Glassb4d70702014-02-26 15:59:25 -0700135 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800136}
137
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200138/* read GPIO ODR value of port 'offset' */
139static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
140{
141 debug("%s: offset:%u\n", __func__, offset);
142
143 return sandbox_gpio_get_open_drain(dev, offset);
144}
145
146/* write GPIO ODR value to port 'offset' */
147static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
148{
149 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
150
151 if (!sandbox_gpio_get_direction(dev, offset)) {
152 printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
153 offset);
154 return -1;
155 }
156
157 return sandbox_gpio_set_open_drain(dev, offset, value);
158}
159
Simon Glass32048632014-10-04 11:29:45 -0600160static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
161{
162 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
163 return GPIOF_OUTPUT;
164 return GPIOF_INPUT;
165}
166
Simon Glass16e10402015-01-05 20:05:29 -0700167static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
168 struct fdtdec_phandle_args *args)
169{
170 desc->offset = args->args[0];
171 if (args->args_count < 2)
172 return 0;
173 if (args->args[1] & GPIO_ACTIVE_LOW)
174 desc->flags |= GPIOD_ACTIVE_LOW;
175 if (args->args[1] & 2)
176 desc->flags |= GPIOD_IS_IN;
177 if (args->args[1] & 4)
178 desc->flags |= GPIOD_IS_OUT;
179 if (args->args[1] & 8)
180 desc->flags |= GPIOD_IS_OUT_ACTIVE;
181
182 return 0;
183}
184
Simon Glassb4d70702014-02-26 15:59:25 -0700185static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700186 .direction_input = sb_gpio_direction_input,
187 .direction_output = sb_gpio_direction_output,
188 .get_value = sb_gpio_get_value,
189 .set_value = sb_gpio_set_value,
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200190 .get_open_drain = sb_gpio_get_open_drain,
191 .set_open_drain = sb_gpio_set_open_drain,
Simon Glass32048632014-10-04 11:29:45 -0600192 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700193 .xlate = sb_gpio_xlate,
Simon Glassb4d70702014-02-26 15:59:25 -0700194};
195
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200196static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800197{
Simon Glassde0977b2015-03-05 12:25:20 -0700198 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800199
Simon Glassdd79d6e2017-01-17 16:52:55 -0700200 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb4d70702014-02-26 15:59:25 -0700201 "num-gpios", 0);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700202 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb4d70702014-02-26 15:59:25 -0700203 "gpio-bank-name", NULL);
Simon Glassb43d0442012-02-15 15:51:13 -0800204
Simon Glassb4d70702014-02-26 15:59:25 -0700205 return 0;
206}
Simon Glassb43d0442012-02-15 15:51:13 -0800207
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200208static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700209{
Simon Glassde0977b2015-03-05 12:25:20 -0700210 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700211
Simon Glassdd79d6e2017-01-17 16:52:55 -0700212 if (dev_of_offset(dev) == -1) {
Simon Glassb4d70702014-02-26 15:59:25 -0700213 /* Tell the uclass how many GPIOs we have */
214 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb43d0442012-02-15 15:51:13 -0800215 }
Simon Glassb4d70702014-02-26 15:59:25 -0700216
217 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
218
219 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800220}
Simon Glassb4d70702014-02-26 15:59:25 -0700221
Simon Glassa367f1d2014-10-04 11:29:46 -0600222static int gpio_sandbox_remove(struct udevice *dev)
223{
224 free(dev->priv);
225
226 return 0;
227}
228
Simon Glass767827a2014-06-11 23:29:45 -0600229static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700230 { .compatible = "sandbox,gpio" },
231 { }
232};
233
234U_BOOT_DRIVER(gpio_sandbox) = {
235 .name = "gpio_sandbox",
236 .id = UCLASS_GPIO,
237 .of_match = sandbox_gpio_ids,
238 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
239 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600240 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700241 .ops = &gpio_sandbox_ops,
242};