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