blob: a9c470ee5e6cd1643906dda95a328b25226464c1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8d30fcd2012-02-15 15:51:13 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
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>
Patrick Delaunayff526652020-01-13 11:35:14 +010011#include <dm/lists.h>
Simon Glass3a571232017-05-18 20:09:18 -060012#include <dm/of.h>
Patrick Delaunayff526652020-01-13 11:35:14 +010013#include <dm/pinctrl.h>
Simon Glass3669e0e2015-01-05 20:05:29 -070014#include <dt-bindings/gpio/gpio.h>
Patrick Delaunay2c0f7822020-01-13 11:35:13 +010015#include <dt-bindings/gpio/sandbox-gpio.h>
Simon Glass8d30fcd2012-02-15 15:51:13 -080016
Simon Glass8d30fcd2012-02-15 15:51:13 -080017
18struct gpio_state {
19 const char *label; /* label given by requester */
Patrick Delaunayff526652020-01-13 11:35:14 +010020 ulong dir_flags; /* dir_flags (GPIOD_...) */
Simon Glass8d30fcd2012-02-15 15:51:13 -080021};
22
Patrick Delaunayff526652020-01-13 11:35:14 +010023/* Access routines for GPIO dir flags */
24static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080025{
Simon Glasse564f052015-03-05 12:25:20 -070026 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -070027 struct gpio_state *state = dev_get_priv(dev);
28
29 if (offset >= uc_priv->gpio_count) {
Patrick Delaunayff526652020-01-13 11:35:14 +010030 static ulong invalid_dir_flags;
Simon Glasse2d8a712014-02-26 15:59:25 -070031 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Patrick Delaunayff526652020-01-13 11:35:14 +010032 return &invalid_dir_flags;
Simon Glass8d30fcd2012-02-15 15:51:13 -080033 }
34
Patrick Delaunayff526652020-01-13 11:35:14 +010035 return &state[offset].dir_flags;
36
Simon Glass8d30fcd2012-02-15 15:51:13 -080037}
38
Patrick Delaunayff526652020-01-13 11:35:14 +010039static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
Simon Glass8d30fcd2012-02-15 15:51:13 -080040{
Patrick Delaunayff526652020-01-13 11:35:14 +010041 return (*get_gpio_dir_flags(dev, offset) & flag) != 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -080042}
43
Patrick Delaunayff526652020-01-13 11:35:14 +010044static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
Simon Glasse2d8a712014-02-26 15:59:25 -070045 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080046{
Patrick Delaunayff526652020-01-13 11:35:14 +010047 ulong *gpio = get_gpio_dir_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 Glass8d30fcd2012-02-15 15:51:13 -080057/*
58 * Back-channel sandbox-internal-only access to GPIO state
59 */
60
Heiko Schocher54c5d082014-05-22 12:43:05 +020061int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080062{
Patrick Delaunayff526652020-01-13 11:35:14 +010063 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glasse2d8a712014-02-26 15:59:25 -070064 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
Patrick Delaunayff526652020-01-13 11:35:14 +010065 return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE);
Simon Glass8d30fcd2012-02-15 15:51:13 -080066}
67
Heiko Schocher54c5d082014-05-22 12:43:05 +020068int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -080069{
Patrick Delaunayff526652020-01-13 11:35:14 +010070 return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -080071}
72
Heiko Schocher54c5d082014-05-22 12:43:05 +020073int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -080074{
Patrick Delaunayff526652020-01-13 11:35:14 +010075 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
Simon Glass8d30fcd2012-02-15 15:51:13 -080076}
77
Heiko Schocher54c5d082014-05-22 12:43:05 +020078int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glass8d30fcd2012-02-15 15:51:13 -080079{
Patrick Delaunayff526652020-01-13 11:35:14 +010080 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
81 set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output));
82
83 return 0;
84}
85
86ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset)
87{
88 return *get_gpio_dir_flags(dev, offset);
89}
90
91int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
92 ulong flags)
93{
94 *get_gpio_dir_flags(dev, offset) = flags;
95
96 return 0;
Simon Glass8d30fcd2012-02-15 15:51:13 -080097}
98
99/*
100 * These functions implement the public interface within U-Boot
101 */
102
Simon Glasse2d8a712014-02-26 15:59:25 -0700103/* set GPIO port 'offset' as an input */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200104static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800105{
Simon Glasse2d8a712014-02-26 15:59:25 -0700106 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800107
Simon Glasse2d8a712014-02-26 15:59:25 -0700108 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800109}
110
Simon Glasse2d8a712014-02-26 15:59:25 -0700111/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200112static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glasse2d8a712014-02-26 15:59:25 -0700113 int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800114{
Simon Glasse2d8a712014-02-26 15:59:25 -0700115 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800116
Simon Glasse2d8a712014-02-26 15:59:25 -0700117 return sandbox_gpio_set_direction(dev, offset, 1) |
118 sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800119}
120
Simon Glasse2d8a712014-02-26 15:59:25 -0700121/* read GPIO IN value of port 'offset' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200122static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800123{
Simon Glasse2d8a712014-02-26 15:59:25 -0700124 debug("%s: offset:%u\n", __func__, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800125
Simon Glasse2d8a712014-02-26 15:59:25 -0700126 return sandbox_gpio_get_value(dev, offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800127}
128
Simon Glasse2d8a712014-02-26 15:59:25 -0700129/* write GPIO OUT value to port 'offset' */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200130static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glass8d30fcd2012-02-15 15:51:13 -0800131{
Simon Glasse2d8a712014-02-26 15:59:25 -0700132 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800133
Simon Glasse2d8a712014-02-26 15:59:25 -0700134 if (!sandbox_gpio_get_direction(dev, offset)) {
135 printf("sandbox_gpio: error: set_value on input gpio %u\n",
136 offset);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800137 return -1;
138 }
139
Simon Glasse2d8a712014-02-26 15:59:25 -0700140 return sandbox_gpio_set_value(dev, offset, value);
Simon Glass8d30fcd2012-02-15 15:51:13 -0800141}
142
Simon Glass699ea962014-10-04 11:29:45 -0600143static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
144{
Patrick Delaunayff526652020-01-13 11:35:14 +0100145 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glass699ea962014-10-04 11:29:45 -0600146 return GPIOF_OUTPUT;
Patrick Delaunayff526652020-01-13 11:35:14 +0100147 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
148 return GPIOF_INPUT;
149
150 return GPIOF_INPUT; /*GPIO is not configurated */
Simon Glass699ea962014-10-04 11:29:45 -0600151}
152
Simon Glass3669e0e2015-01-05 20:05:29 -0700153static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600154 struct ofnode_phandle_args *args)
Simon Glass3669e0e2015-01-05 20:05:29 -0700155{
156 desc->offset = args->args[0];
157 if (args->args_count < 2)
158 return 0;
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100159 /* treat generic binding with gpio uclass */
160 gpio_xlate_offs_flags(dev, desc, args);
161
162 /* sandbox test specific, not defined in gpio.h */
163 if (args->args[1] & GPIO_IN)
Simon Glass3669e0e2015-01-05 20:05:29 -0700164 desc->flags |= GPIOD_IS_IN;
Patrick Delaunayff526652020-01-13 11:35:14 +0100165
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100166 if (args->args[1] & GPIO_OUT)
Simon Glass3669e0e2015-01-05 20:05:29 -0700167 desc->flags |= GPIOD_IS_OUT;
Patrick Delaunayff526652020-01-13 11:35:14 +0100168
Patrick Delaunay2c0f7822020-01-13 11:35:13 +0100169 if (args->args[1] & GPIO_OUT_ACTIVE)
Simon Glass3669e0e2015-01-05 20:05:29 -0700170 desc->flags |= GPIOD_IS_OUT_ACTIVE;
171
172 return 0;
173}
174
Patrick Delaunayff526652020-01-13 11:35:14 +0100175static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
176 ulong flags)
177{
178 ulong *dir_flags;
179
180 debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags);
181
182 dir_flags = get_gpio_dir_flags(dev, offset);
183
184 *dir_flags = flags;
185
186 return 0;
187}
188
189static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
190 ulong *flags)
191{
192 debug("%s: offset:%u\n", __func__, offset);
193 *flags = *get_gpio_dir_flags(dev, offset);
194
195 return 0;
196}
197
Simon Glasse2d8a712014-02-26 15:59:25 -0700198static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glasse2d8a712014-02-26 15:59:25 -0700199 .direction_input = sb_gpio_direction_input,
200 .direction_output = sb_gpio_direction_output,
201 .get_value = sb_gpio_get_value,
202 .set_value = sb_gpio_set_value,
Simon Glass699ea962014-10-04 11:29:45 -0600203 .get_function = sb_gpio_get_function,
Simon Glass3669e0e2015-01-05 20:05:29 -0700204 .xlate = sb_gpio_xlate,
Patrick Delaunayff526652020-01-13 11:35:14 +0100205 .set_dir_flags = sb_gpio_set_dir_flags,
206 .get_dir_flags = sb_gpio_get_dir_flags,
Simon Glasse2d8a712014-02-26 15:59:25 -0700207};
208
Heiko Schocher54c5d082014-05-22 12:43:05 +0200209static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glasse2d8a712014-02-26 15:59:25 -0700210{
Simon Glasse564f052015-03-05 12:25:20 -0700211 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -0700212
Simon Glass995b60b2018-02-03 10:36:59 -0700213 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
214 0);
Simon Glass95795ad2017-05-18 20:09:20 -0600215 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
Simon Glasse2d8a712014-02-26 15:59:25 -0700216
217 return 0;
218}
219
Heiko Schocher54c5d082014-05-22 12:43:05 +0200220static int gpio_sandbox_probe(struct udevice *dev)
Simon Glasse2d8a712014-02-26 15:59:25 -0700221{
Simon Glasse564f052015-03-05 12:25:20 -0700222 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse2d8a712014-02-26 15:59:25 -0700223
Simon Glass95795ad2017-05-18 20:09:20 -0600224 if (!dev_of_valid(dev))
Simon Glasse2d8a712014-02-26 15:59:25 -0700225 /* Tell the uclass how many GPIOs we have */
226 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glasse2d8a712014-02-26 15:59:25 -0700227
228 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
229
230 return 0;
231}
232
Simon Glass62cb89d2014-10-04 11:29:46 -0600233static int gpio_sandbox_remove(struct udevice *dev)
234{
235 free(dev->priv);
236
237 return 0;
238}
239
Simon Glassae7f4512014-06-11 23:29:45 -0600240static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glasse2d8a712014-02-26 15:59:25 -0700241 { .compatible = "sandbox,gpio" },
242 { }
243};
244
245U_BOOT_DRIVER(gpio_sandbox) = {
246 .name = "gpio_sandbox",
247 .id = UCLASS_GPIO,
248 .of_match = sandbox_gpio_ids,
249 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
250 .probe = gpio_sandbox_probe,
Simon Glass62cb89d2014-10-04 11:29:46 -0600251 .remove = gpio_sandbox_remove,
Simon Glasse2d8a712014-02-26 15:59:25 -0700252 .ops = &gpio_sandbox_ops,
253};