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 | /* |
| 3 | * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* #define DEBUG */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 10 | #include <dm/pinctrl.h> |
| 11 | |
| 12 | static const char * const sandbox_pins[] = { |
| 13 | "SCL", |
| 14 | "SDA", |
| 15 | "TX", |
| 16 | "RX", |
Patrick Delaunay | 939cbe9 | 2020-01-13 11:35:12 +0100 | [diff] [blame] | 17 | "W1", |
| 18 | "GPIO0", |
| 19 | "GPIO1", |
| 20 | "GPIO2", |
| 21 | "GPIO3", |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 22 | }; |
| 23 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 24 | static const char * const sandbox_pins_muxing[] = { |
| 25 | "I2C SCL", |
| 26 | "I2C SDA", |
| 27 | "Uart TX", |
| 28 | "Uart RX", |
| 29 | "1-wire gpio", |
Patrick Delaunay | 939cbe9 | 2020-01-13 11:35:12 +0100 | [diff] [blame] | 30 | "gpio", |
| 31 | "gpio", |
| 32 | "gpio", |
| 33 | "gpio", |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 36 | static const char * const sandbox_groups[] = { |
| 37 | "i2c", |
| 38 | "serial_a", |
| 39 | "serial_b", |
| 40 | "spi", |
Eugen Hristev | 200e35e | 2018-09-18 10:35:34 +0300 | [diff] [blame] | 41 | "w1", |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static const char * const sandbox_functions[] = { |
| 45 | "i2c", |
| 46 | "serial", |
| 47 | "spi", |
Eugen Hristev | 200e35e | 2018-09-18 10:35:34 +0300 | [diff] [blame] | 48 | "w1", |
Patrick Delaunay | 939cbe9 | 2020-01-13 11:35:12 +0100 | [diff] [blame] | 49 | "gpio", |
| 50 | "gpio", |
| 51 | "gpio", |
| 52 | "gpio", |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static const struct pinconf_param sandbox_conf_params[] = { |
| 56 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 57 | { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 }, |
| 58 | { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 }, |
| 59 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 60 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 61 | { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 }, |
| 62 | { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 }, |
| 63 | { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 }, |
| 64 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 65 | { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, |
| 66 | { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 }, |
| 67 | }; |
| 68 | |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 69 | /* bitfield used to save param and value of each pin/selector */ |
| 70 | static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)]; |
| 71 | static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)]; |
| 72 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 73 | static int sandbox_get_pins_count(struct udevice *dev) |
| 74 | { |
| 75 | return ARRAY_SIZE(sandbox_pins); |
| 76 | } |
| 77 | |
| 78 | static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector) |
| 79 | { |
| 80 | return sandbox_pins[selector]; |
| 81 | } |
| 82 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 83 | static int sandbox_get_pin_muxing(struct udevice *dev, |
| 84 | unsigned int selector, |
| 85 | char *buf, int size) |
| 86 | { |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 87 | const struct pinconf_param *p; |
| 88 | int i; |
| 89 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 90 | snprintf(buf, size, "%s", sandbox_pins_muxing[selector]); |
| 91 | |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 92 | if (sandbox_pins_param[selector]) { |
| 93 | for (i = 0, p = sandbox_conf_params; |
| 94 | i < ARRAY_SIZE(sandbox_conf_params); |
| 95 | i++, p++) { |
| 96 | if ((sandbox_pins_param[selector] & BIT(p->param)) && |
| 97 | (!!(sandbox_pins_value[selector] & BIT(p->param)) == |
| 98 | p->default_value)) { |
| 99 | strncat(buf, " ", size); |
| 100 | strncat(buf, p->property, size); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | strncat(buf, ".", size); |
| 105 | |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 109 | static int sandbox_get_groups_count(struct udevice *dev) |
| 110 | { |
| 111 | return ARRAY_SIZE(sandbox_groups); |
| 112 | } |
| 113 | |
| 114 | static const char *sandbox_get_group_name(struct udevice *dev, |
| 115 | unsigned selector) |
| 116 | { |
| 117 | return sandbox_groups[selector]; |
| 118 | } |
| 119 | |
| 120 | static int sandbox_get_functions_count(struct udevice *dev) |
| 121 | { |
| 122 | return ARRAY_SIZE(sandbox_functions); |
| 123 | } |
| 124 | |
| 125 | static const char *sandbox_get_function_name(struct udevice *dev, |
| 126 | unsigned selector) |
| 127 | { |
| 128 | return sandbox_functions[selector]; |
| 129 | } |
| 130 | |
| 131 | static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector, |
| 132 | unsigned func_selector) |
| 133 | { |
| 134 | debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n", |
| 135 | pin_selector, sandbox_get_pin_name(dev, pin_selector), |
| 136 | func_selector, sandbox_get_function_name(dev, func_selector)); |
| 137 | |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 138 | sandbox_pins_param[pin_selector] = 0; |
| 139 | sandbox_pins_value[pin_selector] = 0; |
| 140 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int sandbox_pinmux_group_set(struct udevice *dev, |
| 145 | unsigned group_selector, |
| 146 | unsigned func_selector) |
| 147 | { |
| 148 | debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n", |
| 149 | group_selector, sandbox_get_group_name(dev, group_selector), |
| 150 | func_selector, sandbox_get_function_name(dev, func_selector)); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector, |
| 156 | unsigned param, unsigned argument) |
| 157 | { |
| 158 | debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n", |
| 159 | pin_selector, sandbox_get_pin_name(dev, pin_selector), |
| 160 | param, argument); |
| 161 | |
Patrick Delaunay | 4382692 | 2020-01-13 11:35:11 +0100 | [diff] [blame] | 162 | sandbox_pins_param[pin_selector] |= BIT(param); |
| 163 | if (argument) |
| 164 | sandbox_pins_value[pin_selector] |= BIT(param); |
| 165 | else |
| 166 | sandbox_pins_value[pin_selector] &= ~BIT(param); |
| 167 | |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int sandbox_pinconf_group_set(struct udevice *dev, |
| 172 | unsigned group_selector, |
| 173 | unsigned param, unsigned argument) |
| 174 | { |
| 175 | debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n", |
| 176 | group_selector, sandbox_get_group_name(dev, group_selector), |
| 177 | param, argument); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | const struct pinctrl_ops sandbox_pinctrl_ops = { |
| 183 | .get_pins_count = sandbox_get_pins_count, |
| 184 | .get_pin_name = sandbox_get_pin_name, |
Patrice Chotard | 3cd1f33 | 2018-10-24 14:10:22 +0200 | [diff] [blame] | 185 | .get_pin_muxing = sandbox_get_pin_muxing, |
Masahiro Yamada | 0b53a75 | 2015-08-27 12:44:30 +0900 | [diff] [blame] | 186 | .get_groups_count = sandbox_get_groups_count, |
| 187 | .get_group_name = sandbox_get_group_name, |
| 188 | .get_functions_count = sandbox_get_functions_count, |
| 189 | .get_function_name = sandbox_get_function_name, |
| 190 | .pinmux_set = sandbox_pinmux_set, |
| 191 | .pinmux_group_set = sandbox_pinmux_group_set, |
| 192 | .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params), |
| 193 | .pinconf_params = sandbox_conf_params, |
| 194 | .pinconf_set = sandbox_pinconf_set, |
| 195 | .pinconf_group_set = sandbox_pinconf_group_set, |
| 196 | .set_state = pinctrl_generic_set_state, |
| 197 | }; |
| 198 | |
| 199 | static const struct udevice_id sandbox_pinctrl_match[] = { |
| 200 | { .compatible = "sandbox,pinctrl" }, |
| 201 | { /* sentinel */ } |
| 202 | }; |
| 203 | |
| 204 | U_BOOT_DRIVER(sandbox_pinctrl) = { |
| 205 | .name = "sandbox_pinctrl", |
| 206 | .id = UCLASS_PINCTRL, |
| 207 | .of_match = sandbox_pinctrl_match, |
| 208 | .ops = &sandbox_pinctrl_ops, |
| 209 | }; |