Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <malloc.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 11 | #include <dm/of.h> |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 12 | #include <dt-bindings/gpio/gpio.h> |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame^] | 13 | #include <dt-bindings/gpio/sandbox-gpio.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 14 | |
| 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 */ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 18 | |
| 19 | struct gpio_state { |
| 20 | const char *label; /* label given by requester */ |
| 21 | u8 flags; /* flags (GPIOF_...) */ |
| 22 | }; |
| 23 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 24 | /* Access routines for GPIO state */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 25 | static u8 *get_gpio_flags(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 26 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 27 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 28 | struct gpio_state *state = dev_get_priv(dev); |
| 29 | |
| 30 | if (offset >= uc_priv->gpio_count) { |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 31 | static u8 invalid_flags; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 32 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 33 | return &invalid_flags; |
| 34 | } |
| 35 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 36 | return &state[offset].flags; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 39 | static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 40 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 41 | return (*get_gpio_flags(dev, offset) & flag) != 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 44 | static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 45 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 46 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 47 | u8 *gpio = get_gpio_flags(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 48 | |
| 49 | if (value) |
| 50 | *gpio |= flag; |
| 51 | else |
| 52 | *gpio &= ~flag; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 57 | /* |
| 58 | * Back-channel sandbox-internal-only access to GPIO state |
| 59 | */ |
| 60 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 61 | int sandbox_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 62 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 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); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 68 | int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 69 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 70 | return set_gpio_flag(dev, offset, GPIOF_HIGH, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 73 | int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 74 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 75 | return get_gpio_flag(dev, offset, GPIOF_OUTPUT); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 78 | int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 79 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 80 | return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * These functions implement the public interface within U-Boot |
| 85 | */ |
| 86 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 87 | /* set GPIO port 'offset' as an input */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 88 | static int sb_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 89 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 90 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 91 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 92 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 95 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 96 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 97 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 98 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 99 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 100 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 101 | return sandbox_gpio_set_direction(dev, offset, 1) | |
| 102 | sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 105 | /* read GPIO IN value of port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 106 | static int sb_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 107 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 108 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 109 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 110 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 113 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 114 | static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 115 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 116 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 117 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 118 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 119 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 120 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 121 | return -1; |
| 122 | } |
| 123 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 124 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 127 | static 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 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 134 | static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 135 | struct ofnode_phandle_args *args) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 136 | { |
| 137 | desc->offset = args->args[0]; |
| 138 | if (args->args_count < 2) |
| 139 | return 0; |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame^] | 140 | /* treat generic binding with gpio uclass */ |
| 141 | gpio_xlate_offs_flags(dev, desc, args); |
| 142 | |
| 143 | /* sandbox test specific, not defined in gpio.h */ |
| 144 | if (args->args[1] & GPIO_IN) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 145 | desc->flags |= GPIOD_IS_IN; |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame^] | 146 | if (args->args[1] & GPIO_OUT) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 147 | desc->flags |= GPIOD_IS_OUT; |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame^] | 148 | if (args->args[1] & GPIO_OUT_ACTIVE) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 149 | desc->flags |= GPIOD_IS_OUT_ACTIVE; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 154 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 155 | .direction_input = sb_gpio_direction_input, |
| 156 | .direction_output = sb_gpio_direction_output, |
| 157 | .get_value = sb_gpio_get_value, |
| 158 | .set_value = sb_gpio_set_value, |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 159 | .get_function = sb_gpio_get_function, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 160 | .xlate = sb_gpio_xlate, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 163 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 164 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 165 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 166 | |
Simon Glass | 9e7ab23 | 2018-02-03 10:36:59 -0700 | [diff] [blame] | 167 | uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count", |
| 168 | 0); |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 169 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 170 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 173 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 174 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 175 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 176 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 177 | |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 178 | if (!dev_of_valid(dev)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 179 | /* Tell the uclass how many GPIOs we have */ |
| 180 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 181 | |
| 182 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 183 | |
| 184 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 185 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 186 | |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 187 | static int gpio_sandbox_remove(struct udevice *dev) |
| 188 | { |
| 189 | free(dev->priv); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 194 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 195 | { .compatible = "sandbox,gpio" }, |
| 196 | { } |
| 197 | }; |
| 198 | |
| 199 | U_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, |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 205 | .remove = gpio_sandbox_remove, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 206 | .ops = &gpio_sandbox_ops, |
| 207 | }; |