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