Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018 Alexander Graf <agraf@suse.de> |
| 4 | * |
| 5 | * Based on drivers/pinctrl/mvebu/pinctrl-mvebu.c and |
| 6 | * drivers/gpio/bcm2835_gpio.c |
| 7 | * |
| 8 | * This driver gets instantiated by the GPIO driver, because both devices |
| 9 | * share the same device node. |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 10 | * https://spdx.org/licenses |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <config.h> |
| 15 | #include <errno.h> |
| 16 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 18 | #include <dm/pinctrl.h> |
| 19 | #include <dm/root.h> |
| 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/lists.h> |
| 22 | #include <asm/system.h> |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/gpio.h> |
| 25 | |
| 26 | struct bcm283x_pinctrl_priv { |
| 27 | u32 *base_reg; |
| 28 | }; |
| 29 | |
| 30 | #define MAX_PINS_PER_BANK 16 |
| 31 | |
| 32 | static void bcm2835_gpio_set_func_id(struct udevice *dev, unsigned int gpio, |
| 33 | int func) |
| 34 | { |
| 35 | struct bcm283x_pinctrl_priv *priv = dev_get_priv(dev); |
| 36 | int reg_offset; |
| 37 | int field_offset; |
| 38 | |
| 39 | reg_offset = BCM2835_GPIO_FSEL_BANK(gpio); |
| 40 | field_offset = BCM2835_GPIO_FSEL_SHIFT(gpio); |
| 41 | |
| 42 | clrsetbits_le32(&priv->base_reg[reg_offset], |
| 43 | BCM2835_GPIO_FSEL_MASK << field_offset, |
| 44 | (func & BCM2835_GPIO_FSEL_MASK) << field_offset); |
| 45 | } |
| 46 | |
| 47 | static int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned int gpio) |
| 48 | { |
| 49 | struct bcm283x_pinctrl_priv *priv = dev_get_priv(dev); |
| 50 | u32 val; |
| 51 | |
| 52 | val = readl(&priv->base_reg[BCM2835_GPIO_FSEL_BANK(gpio)]); |
| 53 | |
| 54 | return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * bcm283x_pinctrl_set_state: configure pin functions. |
| 59 | * @dev: the pinctrl device to be configured. |
| 60 | * @config: the state to be configured. |
| 61 | * @return: 0 in success |
| 62 | */ |
| 63 | int bcm283x_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 64 | { |
| 65 | u32 pin_arr[MAX_PINS_PER_BANK]; |
Ovidiu Panait | 0db912d | 2020-09-29 20:27:13 +0300 | [diff] [blame] | 66 | int function; |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 67 | int i, len, pin_count = 0; |
| 68 | |
| 69 | if (!dev_read_prop(config, "brcm,pins", &len) || !len || |
| 70 | len & 0x3 || dev_read_u32_array(config, "brcm,pins", pin_arr, |
| 71 | len / sizeof(u32))) { |
| 72 | debug("Failed reading pins array for pinconfig %s (%d)\n", |
| 73 | config->name, len); |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | pin_count = len / sizeof(u32); |
| 78 | |
| 79 | function = dev_read_u32_default(config, "brcm,function", -1); |
| 80 | if (function < 0) { |
| 81 | debug("Failed reading function for pinconfig %s (%d)\n", |
| 82 | config->name, function); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | for (i = 0; i < pin_count; i++) |
| 87 | bcm2835_gpio_set_func_id(dev, pin_arr[i], function); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int bcm283x_pinctrl_get_gpio_mux(struct udevice *dev, int banknum, |
| 93 | int index) |
| 94 | { |
| 95 | if (banknum != 0) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | return bcm2835_gpio_get_func_id(dev, index); |
| 99 | } |
| 100 | |
| 101 | static const struct udevice_id bcm2835_pinctrl_id[] = { |
| 102 | {.compatible = "brcm,bcm2835-gpio"}, |
Matthias Brugger | d2086d6 | 2019-11-06 15:28:25 +0100 | [diff] [blame] | 103 | {.compatible = "brcm,bcm2711-gpio"}, |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 104 | {} |
| 105 | }; |
| 106 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 107 | int bcm283x_pinctl_of_to_plat(struct udevice *dev) |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 108 | { |
| 109 | struct bcm283x_pinctrl_priv *priv; |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 110 | |
| 111 | priv = dev_get_priv(dev); |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 112 | |
| 113 | priv->base_reg = dev_read_addr_ptr(dev); |
Sean Anderson | 082faeb | 2020-06-24 06:41:13 -0400 | [diff] [blame] | 114 | if (!priv->base_reg) { |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 115 | debug("%s: Failed to get base address\n", __func__); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
Ovidiu Panait | e020c07 | 2020-09-29 20:27:12 +0300 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | int bcm283x_pinctl_probe(struct udevice *dev) |
| 123 | { |
| 124 | int ret; |
| 125 | struct udevice *pdev; |
| 126 | |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 127 | /* Create GPIO device as well */ |
Simon Glass | a2703ce | 2020-11-28 17:50:03 -0700 | [diff] [blame] | 128 | ret = device_bind(dev, lists_driver_lookup_name("gpio_bcm2835"), |
| 129 | "gpio_bcm2835", NULL, dev_ofnode(dev), &pdev); |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 130 | if (ret) { |
| 131 | /* |
| 132 | * While we really want the pinctrl driver to work to make |
| 133 | * devices go where they should go, the GPIO controller is |
| 134 | * not quite as crucial as it's only rarely used, so don't |
| 135 | * fail here. |
| 136 | */ |
| 137 | printf("Failed to bind GPIO driver\n"); |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static struct pinctrl_ops bcm283x_pinctrl_ops = { |
| 144 | .set_state = bcm283x_pinctrl_set_state, |
| 145 | .get_gpio_mux = bcm283x_pinctrl_get_gpio_mux, |
| 146 | }; |
| 147 | |
| 148 | U_BOOT_DRIVER(pinctrl_bcm283x) = { |
| 149 | .name = "bcm283x_pinctrl", |
| 150 | .id = UCLASS_PINCTRL, |
| 151 | .of_match = of_match_ptr(bcm2835_pinctrl_id), |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 152 | .of_to_plat = bcm283x_pinctl_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 153 | .priv_auto = sizeof(struct bcm283x_pinctrl_priv), |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 154 | .ops = &bcm283x_pinctrl_ops, |
Alexander Graf | 9821636 | 2018-01-25 12:05:56 +0100 | [diff] [blame] | 155 | .probe = bcm283x_pinctl_probe, |
Simon Glass | 68be8d9 | 2023-02-05 17:54:42 -0700 | [diff] [blame] | 156 | #if IS_ENABLED(CONFIG_OF_BOARD) |
Alexander Graf | 9821636 | 2018-01-25 12:05:56 +0100 | [diff] [blame] | 157 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | ad0e804 | 2018-10-24 06:36:33 -0700 | [diff] [blame] | 158 | #endif |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame] | 159 | }; |