Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Atmel PIO4 pinctrl driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Atmel Corporation |
| 6 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/lists.h> |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 13 | #include <dm/pinctrl.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/err.h> |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 18 | #include <mach/atmel_pio4.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /* |
| 23 | * Warning: |
| 24 | * In order to not introduce confusion between Atmel PIO groups and pinctrl |
| 25 | * framework groups, Atmel PIO groups will be called banks. |
| 26 | */ |
| 27 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 28 | struct atmel_pio4_plat { |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 29 | struct atmel_pio4_port *reg_base; |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 30 | unsigned int slew_rate_support; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 31 | }; |
| 32 | |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 33 | /* |
| 34 | * Table keeping track of the pinctrl driver's slew rate support and the |
| 35 | * corresponding index into the struct udevice_id of the gpio_atmel_pio4 GPIO |
| 36 | * driver. This has been done in order to align the DT of U-Boot with the DT of |
| 37 | * Linux. In Linux, a phandle from a '-gpio' DT property is linked to the |
| 38 | * pinctrl driver, unlike U-Boot which redirects this phandle to a corresponding |
| 39 | * UCLASS_GPIO driver. Thus, in order to link the two, a hook to the bind method |
| 40 | * of the pinctrl driver in U-Boot has been added. This bind method will attach |
| 41 | * the GPIO driver to the pinctrl DT node using this table. |
| 42 | * @slew_rate_support pinctrl driver's slew rate support |
| 43 | * @gdidx index into the GPIO driver's struct udevide_id |
| 44 | * (needed in order to properly bind with driver_data) |
| 45 | */ |
| 46 | |
| 47 | struct atmel_pinctrl_data { |
| 48 | unsigned int slew_rate_support; |
| 49 | int gdidx; |
| 50 | }; |
| 51 | |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 52 | static const struct pinconf_param conf_params[] = { |
| 53 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 54 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 55 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 56 | { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 }, |
| 57 | { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 }, |
| 58 | { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 }, |
| 59 | { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 }, |
Eugen Hristev | 90add2b | 2021-01-05 10:54:01 +0200 | [diff] [blame] | 60 | { "atmel,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 61 | { "slew-rate", PIN_CONFIG_SLEW_RATE, 0}, |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 62 | }; |
| 63 | |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 64 | static u32 atmel_pinctrl_get_pinconf(struct udevice *config, |
| 65 | struct atmel_pio4_plat *plat) |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 66 | { |
| 67 | const struct pinconf_param *params; |
| 68 | u32 param, arg, conf = 0; |
| 69 | u32 i; |
Eugen Hristev | 90add2b | 2021-01-05 10:54:01 +0200 | [diff] [blame] | 70 | u32 val; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 71 | |
| 72 | for (i = 0; i < ARRAY_SIZE(conf_params); i++) { |
| 73 | params = &conf_params[i]; |
Eugen Hristev | e58dc84 | 2021-01-05 10:51:53 +0200 | [diff] [blame] | 74 | if (!dev_read_prop(config, params->property, NULL)) |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 75 | continue; |
| 76 | |
| 77 | param = params->param; |
| 78 | arg = params->default_value; |
| 79 | |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 80 | /* Keep slew rate enabled by default. */ |
| 81 | if (plat->slew_rate_support) |
| 82 | conf |= ATMEL_PIO_SR; |
| 83 | |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 84 | switch (param) { |
| 85 | case PIN_CONFIG_BIAS_DISABLE: |
| 86 | conf &= (~ATMEL_PIO_PUEN_MASK); |
| 87 | conf &= (~ATMEL_PIO_PDEN_MASK); |
| 88 | break; |
| 89 | case PIN_CONFIG_BIAS_PULL_UP: |
| 90 | conf |= ATMEL_PIO_PUEN_MASK; |
| 91 | break; |
| 92 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 93 | conf |= ATMEL_PIO_PDEN_MASK; |
| 94 | break; |
| 95 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
| 96 | if (arg == 0) |
| 97 | conf &= (~ATMEL_PIO_OPD_MASK); |
| 98 | else |
| 99 | conf |= ATMEL_PIO_OPD_MASK; |
| 100 | break; |
| 101 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 102 | if (arg == 0) |
| 103 | conf |= ATMEL_PIO_SCHMITT_MASK; |
| 104 | else |
| 105 | conf &= (~ATMEL_PIO_SCHMITT_MASK); |
| 106 | break; |
| 107 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 108 | if (arg == 0) { |
| 109 | conf &= (~ATMEL_PIO_IFEN_MASK); |
| 110 | conf &= (~ATMEL_PIO_IFSCEN_MASK); |
| 111 | } else { |
| 112 | conf |= ATMEL_PIO_IFEN_MASK; |
| 113 | conf |= ATMEL_PIO_IFSCEN_MASK; |
| 114 | } |
| 115 | break; |
Eugen Hristev | 90add2b | 2021-01-05 10:54:01 +0200 | [diff] [blame] | 116 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 117 | dev_read_u32(config, params->property, &val); |
| 118 | conf &= (~ATMEL_PIO_DRVSTR_MASK); |
| 119 | conf |= (val << ATMEL_PIO_DRVSTR_OFFSET) |
| 120 | & ATMEL_PIO_DRVSTR_MASK; |
| 121 | break; |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 122 | case PIN_CONFIG_SLEW_RATE: |
| 123 | if (!plat->slew_rate_support) |
| 124 | break; |
| 125 | |
| 126 | dev_read_u32(config, params->property, &val); |
| 127 | /* And disable it if requested. */ |
| 128 | if (val == 0) |
| 129 | conf &= ~ATMEL_PIO_SR; |
| 130 | break; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 131 | default: |
| 132 | printf("%s: Unsupported configuration parameter: %u\n", |
| 133 | __func__, param); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return conf; |
| 139 | } |
| 140 | |
| 141 | static inline struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev, |
| 142 | u32 bank) |
| 143 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 144 | struct atmel_pio4_plat *plat = dev_get_plat(dev); |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 145 | struct atmel_pio4_port *bank_base = |
| 146 | (struct atmel_pio4_port *)((u32)plat->reg_base + |
| 147 | ATMEL_PIO_BANK_OFFSET * bank); |
| 148 | |
| 149 | return bank_base; |
| 150 | } |
| 151 | |
| 152 | #define MAX_PINMUX_ENTRIES 40 |
| 153 | |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 154 | static int atmel_process_config_dev(struct udevice *dev, struct udevice *config) |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 155 | { |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 156 | struct atmel_pio4_plat *plat = dev_get_plat(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 157 | int node = dev_of_offset(config); |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 158 | struct atmel_pio4_port *bank_base; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 159 | u32 offset, func, bank, line; |
| 160 | u32 cells[MAX_PINMUX_ENTRIES]; |
| 161 | u32 i, conf; |
| 162 | int count; |
| 163 | |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 164 | conf = atmel_pinctrl_get_pinconf(config, plat); |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 165 | |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 166 | /* |
| 167 | * The only case where this function returns a negative error value |
| 168 | * is when there is no "pinmux" property attached to this node |
| 169 | */ |
| 170 | count = fdtdec_get_int_array_count(gd->fdt_blob, node, "pinmux", |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 171 | cells, ARRAY_SIZE(cells)); |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 172 | if (count < 0) |
| 173 | return count; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 174 | |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 175 | if (count > MAX_PINMUX_ENTRIES) |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 176 | return -EINVAL; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 177 | |
| 178 | for (i = 0 ; i < count; i++) { |
| 179 | offset = ATMEL_GET_PIN_NO(cells[i]); |
| 180 | func = ATMEL_GET_PIN_FUNC(cells[i]); |
| 181 | |
| 182 | bank = ATMEL_PIO_BANK(offset); |
| 183 | line = ATMEL_PIO_LINE(offset); |
| 184 | |
| 185 | bank_base = atmel_pio4_bank_base(dev, bank); |
| 186 | |
| 187 | writel(BIT(line), &bank_base->mskr); |
| 188 | conf &= (~ATMEL_PIO_CFGR_FUNC_MASK); |
| 189 | conf |= (func & ATMEL_PIO_CFGR_FUNC_MASK); |
| 190 | writel(conf, &bank_base->cfgr); |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Sergiu Moga | 219a5fd | 2022-09-01 17:22:42 +0300 | [diff] [blame] | 196 | static int atmel_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 197 | { |
| 198 | int node = dev_of_offset(config); |
| 199 | struct udevice *subconfig; |
| 200 | int subnode, subnode_count = 0, ret; |
| 201 | |
| 202 | /* |
| 203 | * If this function returns a negative error code then that means |
| 204 | * that either the "pinmux" property of the node is missing, which is |
| 205 | * the case for pinctrl nodes that do not have all the pins with the |
| 206 | * same configuration and are split in multiple subnodes, or something |
| 207 | * else went wrong and we have to stop. For the latter case, it would |
| 208 | * mean that the node failed even though it has no subnodes. |
| 209 | */ |
| 210 | ret = atmel_process_config_dev(dev, config); |
| 211 | if (!ret) |
| 212 | return ret; |
| 213 | |
| 214 | /* |
| 215 | * If we reach here, it means that the subnode pinctrl's DT has multiple |
| 216 | * subnodes. If it does not, then something else went wrong in the |
| 217 | * previous call to atmel_process_config_dev. |
| 218 | */ |
| 219 | fdt_for_each_subnode(subnode, gd->fdt_blob, node) { |
| 220 | /* Get subnode as an udevice */ |
| 221 | ret = uclass_find_device_by_of_offset(UCLASS_PINCONFIG, subnode, |
| 222 | &subconfig); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | /* |
| 227 | * If this time the function returns an error code on a subnode |
| 228 | * then something is totally wrong so abort. |
| 229 | */ |
| 230 | ret = atmel_process_config_dev(dev, subconfig); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | |
| 234 | subnode_count++; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * If we somehow got here and we do not have any subnodes, abort. |
| 239 | */ |
| 240 | if (!subnode_count) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 246 | const struct pinctrl_ops atmel_pinctrl_ops = { |
| 247 | .set_state = atmel_pinctrl_set_state, |
| 248 | }; |
| 249 | |
| 250 | static int atmel_pinctrl_probe(struct udevice *dev) |
| 251 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 252 | struct atmel_pio4_plat *plat = dev_get_plat(dev); |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 253 | struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev); |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 254 | fdt_addr_t addr_base; |
| 255 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 256 | addr_base = dev_read_addr(dev); |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 257 | if (addr_base == FDT_ADDR_T_NONE) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | plat->reg_base = (struct atmel_pio4_port *)addr_base; |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 261 | plat->slew_rate_support = priv->slew_rate_support; |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 266 | static int atmel_pinctrl_bind(struct udevice *dev) |
| 267 | { |
| 268 | struct udevice *g; |
| 269 | struct driver *drv; |
| 270 | ofnode node = dev_ofnode(dev); |
| 271 | struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev); |
| 272 | |
Simon Glass | 0282a03 | 2023-02-05 15:36:17 -0700 | [diff] [blame] | 273 | if (!IS_ENABLED(CONFIG_ATMEL_PIO4)) |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 274 | return 0; |
| 275 | |
| 276 | /* Obtain a handle to the GPIO driver */ |
| 277 | drv = lists_driver_lookup_name("gpio_atmel_pio4"); |
| 278 | if (!drv) |
| 279 | return -ENOENT; |
| 280 | |
| 281 | /* |
| 282 | * Bind the GPIO driver to the pinctrl DT node, together |
| 283 | * with its corresponding driver_data. |
| 284 | */ |
| 285 | return device_bind_with_driver_data(dev, drv, drv->name, |
| 286 | drv->of_match[priv->gdidx].data, |
| 287 | node, &g); |
| 288 | } |
| 289 | |
| 290 | static const struct atmel_pinctrl_data atmel_sama5d2_pinctrl_data = { |
| 291 | .gdidx = 0, |
| 292 | }; |
| 293 | |
| 294 | static const struct atmel_pinctrl_data microchip_sama7g5_pinctrl_data = { |
| 295 | .slew_rate_support = 1, |
| 296 | .gdidx = 1, |
| 297 | }; |
| 298 | |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 299 | static const struct udevice_id atmel_pinctrl_match[] = { |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 300 | { .compatible = "atmel,sama5d2-pinctrl", |
| 301 | .data = (ulong)&atmel_sama5d2_pinctrl_data, }, |
Claudiu Beznea | 153a793 | 2021-01-27 15:00:30 +0200 | [diff] [blame] | 302 | { .compatible = "microchip,sama7g5-pinctrl", |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 303 | .data = (ulong)µchip_sama7g5_pinctrl_data, }, |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 304 | {} |
| 305 | }; |
| 306 | |
| 307 | U_BOOT_DRIVER(atmel_pinctrl) = { |
| 308 | .name = "pinctrl_atmel_pio4", |
| 309 | .id = UCLASS_PINCTRL, |
| 310 | .of_match = atmel_pinctrl_match, |
Sergiu Moga | f700954 | 2022-09-01 17:22:41 +0300 | [diff] [blame] | 311 | .bind = atmel_pinctrl_bind, |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 312 | .probe = atmel_pinctrl_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 313 | .plat_auto = sizeof(struct atmel_pio4_plat), |
Wenyou Yang | 309686c | 2016-07-20 17:16:27 +0800 | [diff] [blame] | 314 | .ops = &atmel_pinctrl_ops, |
| 315 | }; |