Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 2 | /* |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 3 | * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com> |
| 4 | * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 8 | #include <dm/pinctrl.h> |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 9 | #include <dt-bindings/pinctrl/sandbox-pinmux.h> |
| 10 | #include <log.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 12 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 13 | /* |
| 14 | * This driver emulates a pin controller with the following rules: |
| 15 | * - The pinctrl config for each pin must be set individually |
| 16 | * - The first three pins (P0-P2) must be muxed as a group |
| 17 | * - The next two pins (P3-P4) must be muxed as a group |
| 18 | * - The last four pins (P5-P8) must be muxed individually |
| 19 | */ |
| 20 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 21 | static const char * const sandbox_pins[] = { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 22 | #define PIN(x) \ |
| 23 | [x] = "P" #x |
| 24 | PIN(0), |
| 25 | PIN(1), |
| 26 | PIN(2), |
| 27 | PIN(3), |
| 28 | PIN(4), |
| 29 | PIN(5), |
| 30 | PIN(6), |
| 31 | PIN(7), |
| 32 | PIN(8), |
| 33 | #undef PIN |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 34 | }; |
| 35 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 36 | static const char * const sandbox_pins_muxing[][2] = { |
| 37 | { "UART TX", "I2C SCL" }, |
| 38 | { "UART RX", "I2C SDA" }, |
| 39 | { "SPI SCLK", "I2S SCK" }, |
| 40 | { "SPI MOSI", "I2S SD" }, |
| 41 | { "SPI MISO", "I2S WS" }, |
| 42 | { "GPIO0", "SPI CS0" }, |
| 43 | { "GPIO1", "SPI CS1" }, |
| 44 | { "GPIO2", "PWM0" }, |
| 45 | { "GPIO3", "PWM1" }, |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 48 | #define SANDBOX_GROUP_I2C_UART 0 |
| 49 | #define SANDBOX_GROUP_SPI_I2S 1 |
| 50 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 51 | static const char * const sandbox_groups[] = { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 52 | [SANDBOX_GROUP_I2C_UART] = "I2C_UART", |
| 53 | [SANDBOX_GROUP_SPI_I2S] = "SPI_I2S", |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static const char * const sandbox_functions[] = { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 57 | #define FUNC(id) \ |
| 58 | [SANDBOX_PINMUX_##id] = #id |
| 59 | FUNC(UART), |
| 60 | FUNC(I2C), |
| 61 | FUNC(SPI), |
| 62 | FUNC(I2S), |
| 63 | FUNC(GPIO), |
| 64 | FUNC(CS), |
| 65 | FUNC(PWM), |
| 66 | #undef FUNC |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static const struct pinconf_param sandbox_conf_params[] = { |
| 70 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 71 | { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 }, |
| 72 | { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 }, |
| 73 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 74 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 75 | { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 }, |
| 76 | { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 }, |
| 77 | { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 }, |
| 78 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 79 | { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, |
| 80 | { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 }, |
| 81 | }; |
| 82 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 83 | /* Bitfield used to save param and value of each pin/selector */ |
| 84 | struct sandbox_pinctrl_priv { |
| 85 | unsigned int mux; |
| 86 | unsigned int pins_param[ARRAY_SIZE(sandbox_pins)]; |
| 87 | unsigned int pins_value[ARRAY_SIZE(sandbox_pins)]; |
| 88 | }; |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 89 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 90 | static int sandbox_get_pins_count(struct udevice *dev) |
| 91 | { |
| 92 | return ARRAY_SIZE(sandbox_pins); |
| 93 | } |
| 94 | |
| 95 | static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector) |
| 96 | { |
| 97 | return sandbox_pins[selector]; |
| 98 | } |
| 99 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 100 | static int sandbox_get_pin_muxing(struct udevice *dev, |
| 101 | unsigned int selector, |
| 102 | char *buf, int size) |
| 103 | { |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 104 | const struct pinconf_param *p; |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 105 | struct sandbox_pinctrl_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 106 | int i; |
| 107 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 108 | snprintf(buf, size, "%s", |
| 109 | sandbox_pins_muxing[selector][!!(priv->mux & BIT(selector))]); |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 110 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 111 | if (priv->pins_param[selector]) { |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 112 | for (i = 0, p = sandbox_conf_params; |
| 113 | i < ARRAY_SIZE(sandbox_conf_params); |
| 114 | i++, p++) { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 115 | if ((priv->pins_param[selector] & BIT(p->param)) && |
| 116 | (!!(priv->pins_value[selector] & BIT(p->param)) == |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 117 | p->default_value)) { |
| 118 | strncat(buf, " ", size); |
| 119 | strncat(buf, p->property, size); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | strncat(buf, ".", size); |
| 124 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 128 | static int sandbox_get_groups_count(struct udevice *dev) |
| 129 | { |
| 130 | return ARRAY_SIZE(sandbox_groups); |
| 131 | } |
| 132 | |
| 133 | static const char *sandbox_get_group_name(struct udevice *dev, |
| 134 | unsigned selector) |
| 135 | { |
| 136 | return sandbox_groups[selector]; |
| 137 | } |
| 138 | |
| 139 | static int sandbox_get_functions_count(struct udevice *dev) |
| 140 | { |
| 141 | return ARRAY_SIZE(sandbox_functions); |
| 142 | } |
| 143 | |
| 144 | static const char *sandbox_get_function_name(struct udevice *dev, |
| 145 | unsigned selector) |
| 146 | { |
| 147 | return sandbox_functions[selector]; |
| 148 | } |
| 149 | |
| 150 | static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector, |
| 151 | unsigned func_selector) |
| 152 | { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 153 | int mux; |
| 154 | struct sandbox_pinctrl_priv *priv = dev_get_priv(dev); |
| 155 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 156 | debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n", |
| 157 | pin_selector, sandbox_get_pin_name(dev, pin_selector), |
| 158 | func_selector, sandbox_get_function_name(dev, func_selector)); |
| 159 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 160 | if (pin_selector < 5) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | switch (func_selector) { |
| 164 | case SANDBOX_PINMUX_GPIO: |
| 165 | mux = 0; |
| 166 | break; |
| 167 | case SANDBOX_PINMUX_CS: |
| 168 | case SANDBOX_PINMUX_PWM: |
| 169 | mux = BIT(pin_selector); |
| 170 | break; |
| 171 | default: |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | priv->mux &= ~BIT(pin_selector); |
| 176 | priv->mux |= mux; |
| 177 | priv->pins_param[pin_selector] = 0; |
| 178 | priv->pins_value[pin_selector] = 0; |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 179 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int sandbox_pinmux_group_set(struct udevice *dev, |
| 184 | unsigned group_selector, |
| 185 | unsigned func_selector) |
| 186 | { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 187 | bool mux; |
| 188 | int i, group_start, group_end; |
| 189 | struct sandbox_pinctrl_priv *priv = dev_get_priv(dev); |
| 190 | unsigned int mask; |
| 191 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 192 | debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n", |
| 193 | group_selector, sandbox_get_group_name(dev, group_selector), |
| 194 | func_selector, sandbox_get_function_name(dev, func_selector)); |
| 195 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 196 | if (group_selector == SANDBOX_GROUP_I2C_UART) { |
| 197 | group_start = 0; |
| 198 | group_end = 1; |
| 199 | |
| 200 | if (func_selector == SANDBOX_PINMUX_UART) |
| 201 | mux = false; |
| 202 | else if (func_selector == SANDBOX_PINMUX_I2C) |
| 203 | mux = true; |
| 204 | else |
| 205 | return -EINVAL; |
| 206 | } else if (group_selector == SANDBOX_GROUP_SPI_I2S) { |
| 207 | group_start = 2; |
| 208 | group_end = 4; |
| 209 | |
| 210 | if (func_selector == SANDBOX_PINMUX_SPI) |
| 211 | mux = false; |
| 212 | else if (func_selector == SANDBOX_PINMUX_I2S) |
| 213 | mux = true; |
| 214 | else |
| 215 | return -EINVAL; |
| 216 | } else { |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | mask = GENMASK(group_end, group_start); |
| 221 | priv->mux &= ~mask; |
| 222 | priv->mux |= mux ? mask : 0; |
| 223 | |
| 224 | for (i = group_start; i < group_end; i++) { |
| 225 | priv->pins_param[i] = 0; |
| 226 | priv->pins_value[i] = 0; |
| 227 | } |
| 228 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 232 | static int sandbox_pinmux_property_set(struct udevice *dev, u32 pinmux_group) |
| 233 | { |
| 234 | int ret; |
| 235 | unsigned pin_selector = pinmux_group & 0xFFFF; |
| 236 | unsigned func_selector = pinmux_group >> 16; |
| 237 | |
| 238 | ret = sandbox_pinmux_set(dev, pin_selector, func_selector); |
| 239 | return ret ? ret : pin_selector; |
| 240 | } |
| 241 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 242 | static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector, |
| 243 | unsigned param, unsigned argument) |
| 244 | { |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 245 | struct sandbox_pinctrl_priv *priv = dev_get_priv(dev); |
| 246 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 247 | debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n", |
| 248 | pin_selector, sandbox_get_pin_name(dev, pin_selector), |
| 249 | param, argument); |
| 250 | |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 251 | priv->pins_param[pin_selector] |= BIT(param); |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 252 | if (argument) |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 253 | priv->pins_value[pin_selector] |= BIT(param); |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 254 | else |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 255 | priv->pins_value[pin_selector] &= ~BIT(param); |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 256 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int sandbox_pinconf_group_set(struct udevice *dev, |
| 261 | unsigned group_selector, |
| 262 | unsigned param, unsigned argument) |
| 263 | { |
| 264 | debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n", |
| 265 | group_selector, sandbox_get_group_name(dev, group_selector), |
| 266 | param, argument); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | const struct pinctrl_ops sandbox_pinctrl_ops = { |
| 272 | .get_pins_count = sandbox_get_pins_count, |
| 273 | .get_pin_name = sandbox_get_pin_name, |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 274 | .get_pin_muxing = sandbox_get_pin_muxing, |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 275 | .get_groups_count = sandbox_get_groups_count, |
| 276 | .get_group_name = sandbox_get_group_name, |
| 277 | .get_functions_count = sandbox_get_functions_count, |
| 278 | .get_function_name = sandbox_get_function_name, |
| 279 | .pinmux_set = sandbox_pinmux_set, |
| 280 | .pinmux_group_set = sandbox_pinmux_group_set, |
Sean Anderson | 3438e3b | 2020-09-14 11:01:57 -0400 | [diff] [blame] | 281 | .pinmux_property_set = sandbox_pinmux_property_set, |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 282 | .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params), |
| 283 | .pinconf_params = sandbox_conf_params, |
| 284 | .pinconf_set = sandbox_pinconf_set, |
| 285 | .pinconf_group_set = sandbox_pinconf_group_set, |
| 286 | .set_state = pinctrl_generic_set_state, |
| 287 | }; |
| 288 | |
| 289 | static const struct udevice_id sandbox_pinctrl_match[] = { |
| 290 | { .compatible = "sandbox,pinctrl" }, |
| 291 | { /* sentinel */ } |
| 292 | }; |
| 293 | |
| 294 | U_BOOT_DRIVER(sandbox_pinctrl) = { |
| 295 | .name = "sandbox_pinctrl", |
| 296 | .id = UCLASS_PINCTRL, |
| 297 | .of_match = sandbox_pinctrl_match, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 298 | .priv_auto = sizeof(struct sandbox_pinctrl_priv), |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 299 | .ops = &sandbox_pinctrl_ops, |
| 300 | }; |