Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com> |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 8 | #include <dm/device-internal.h> |
| 9 | #include <dm/lists.h> |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 10 | #include <dm/pinctrl.h> |
| 11 | #include <fdt_support.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/sizes.h> |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 16 | |
| 17 | #include "pinctrl-meson.h" |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | static const char *meson_pinctrl_dummy_name = "_dummy"; |
| 22 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 23 | int meson_pinctrl_get_groups_count(struct udevice *dev) |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 24 | { |
| 25 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 26 | |
| 27 | return priv->data->num_groups; |
| 28 | } |
| 29 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 30 | const char *meson_pinctrl_get_group_name(struct udevice *dev, |
| 31 | unsigned int selector) |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 32 | { |
| 33 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 34 | |
| 35 | if (!priv->data->groups[selector].name) |
| 36 | return meson_pinctrl_dummy_name; |
| 37 | |
| 38 | return priv->data->groups[selector].name; |
| 39 | } |
| 40 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 41 | int meson_pinmux_get_functions_count(struct udevice *dev) |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 42 | { |
| 43 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 44 | |
| 45 | return priv->data->num_funcs; |
| 46 | } |
| 47 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 48 | const char *meson_pinmux_get_function_name(struct udevice *dev, |
| 49 | unsigned int selector) |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 50 | { |
| 51 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 52 | |
| 53 | return priv->data->funcs[selector].name; |
| 54 | } |
| 55 | |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 56 | static int meson_gpio_calc_reg_and_bit(struct udevice *dev, unsigned int offset, |
| 57 | enum meson_reg_type reg_type, |
| 58 | unsigned int *reg, unsigned int *bit) |
| 59 | { |
| 60 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 61 | struct meson_bank *bank = NULL; |
| 62 | struct meson_reg_desc *desc; |
| 63 | unsigned int pin; |
| 64 | int i; |
| 65 | |
| 66 | pin = priv->data->pin_base + offset; |
| 67 | |
| 68 | for (i = 0; i < priv->data->num_banks; i++) { |
| 69 | if (pin >= priv->data->banks[i].first && |
| 70 | pin <= priv->data->banks[i].last) { |
| 71 | bank = &priv->data->banks[i]; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (!bank) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | desc = &bank->regs[reg_type]; |
| 80 | *reg = desc->reg * 4; |
| 81 | *bit = desc->bit + pin - bank->first; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 86 | int meson_gpio_get(struct udevice *dev, unsigned int offset) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 87 | { |
| 88 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 89 | unsigned int reg, bit; |
| 90 | int ret; |
| 91 | |
| 92 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_IN, ®, &bit); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | |
| 96 | return !!(readl(priv->reg_gpio + reg) & BIT(bit)); |
| 97 | } |
| 98 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 99 | int meson_gpio_set(struct udevice *dev, unsigned int offset, int value) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 100 | { |
| 101 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 102 | unsigned int reg, bit; |
| 103 | int ret; |
| 104 | |
| 105 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_OUT, ®, &bit); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | |
| 109 | clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 114 | int meson_gpio_get_direction(struct udevice *dev, unsigned int offset) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 115 | { |
| 116 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 117 | unsigned int reg, bit, val; |
| 118 | int ret; |
| 119 | |
| 120 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, ®, &bit); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | val = readl(priv->reg_gpio + reg); |
| 125 | |
| 126 | return (val & BIT(bit)) ? GPIOF_INPUT : GPIOF_OUTPUT; |
| 127 | } |
| 128 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 129 | int meson_gpio_direction_input(struct udevice *dev, unsigned int offset) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 130 | { |
| 131 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 132 | unsigned int reg, bit; |
| 133 | int ret; |
| 134 | |
| 135 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, ®, &bit); |
| 136 | if (ret) |
| 137 | return ret; |
| 138 | |
Carlo Caione | fb19c7b | 2018-12-03 18:00:42 +0000 | [diff] [blame^] | 139 | setbits_le32(priv->reg_gpio + reg, BIT(bit)); |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 144 | int meson_gpio_direction_output(struct udevice *dev, |
| 145 | unsigned int offset, int value) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 146 | { |
| 147 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 148 | unsigned int reg, bit; |
| 149 | int ret; |
| 150 | |
| 151 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DIR, ®, &bit); |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | |
Carlo Caione | fb19c7b | 2018-12-03 18:00:42 +0000 | [diff] [blame^] | 155 | clrbits_le32(priv->reg_gpio + reg, BIT(bit)); |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 156 | |
| 157 | ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_OUT, ®, &bit); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
| 161 | clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 166 | int meson_gpio_probe(struct udevice *dev) |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 167 | { |
| 168 | struct meson_pinctrl *priv = dev_get_priv(dev->parent); |
| 169 | struct gpio_dev_priv *uc_priv; |
| 170 | |
| 171 | uc_priv = dev_get_uclass_priv(dev); |
| 172 | uc_priv->bank_name = priv->data->name; |
| 173 | uc_priv->gpio_count = priv->data->num_pins; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 178 | static fdt_addr_t parse_address(int offset, const char *name, int na, int ns) |
| 179 | { |
| 180 | int index, len = 0; |
| 181 | const fdt32_t *reg; |
| 182 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 183 | index = fdt_stringlist_search(gd->fdt_blob, offset, "reg-names", name); |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 184 | if (index < 0) |
| 185 | return FDT_ADDR_T_NONE; |
| 186 | |
| 187 | reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len); |
| 188 | if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) |
| 189 | return FDT_ADDR_T_NONE; |
| 190 | |
| 191 | reg += index * (na + ns); |
| 192 | |
| 193 | return fdt_translate_address((void *)gd->fdt_blob, offset, reg); |
| 194 | } |
| 195 | |
| 196 | int meson_pinctrl_probe(struct udevice *dev) |
| 197 | { |
| 198 | struct meson_pinctrl *priv = dev_get_priv(dev); |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 199 | struct uclass_driver *drv; |
| 200 | struct udevice *gpio_dev; |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 201 | fdt_addr_t addr; |
| 202 | int node, gpio = -1, len; |
| 203 | int na, ns; |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 204 | char *name; |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 205 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 206 | na = fdt_address_cells(gd->fdt_blob, dev_of_offset(dev->parent)); |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 207 | if (na < 1) { |
| 208 | debug("bad #address-cells\n"); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 212 | ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent)); |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 213 | if (ns < 1) { |
| 214 | debug("bad #size-cells\n"); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 218 | fdt_for_each_subnode(node, gd->fdt_blob, dev_of_offset(dev)) { |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 219 | if (fdt_getprop(gd->fdt_blob, node, "gpio-controller", &len)) { |
| 220 | gpio = node; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (!gpio) { |
| 226 | debug("gpio node not found\n"); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | addr = parse_address(gpio, "mux", na, ns); |
| 231 | if (addr == FDT_ADDR_T_NONE) { |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 232 | debug("mux address not found\n"); |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 233 | return -EINVAL; |
| 234 | } |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 235 | priv->reg_mux = (void __iomem *)addr; |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 236 | |
| 237 | addr = parse_address(gpio, "gpio", na, ns); |
| 238 | if (addr == FDT_ADDR_T_NONE) { |
| 239 | debug("gpio address not found\n"); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | priv->reg_gpio = (void __iomem *)addr; |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 243 | priv->data = (struct meson_pinctrl_data *)dev_get_driver_data(dev); |
| 244 | |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 245 | /* Lookup GPIO driver */ |
| 246 | drv = lists_uclass_lookup(UCLASS_GPIO); |
| 247 | if (!drv) { |
| 248 | puts("Cannot find GPIO driver\n"); |
| 249 | return -ENOENT; |
| 250 | } |
| 251 | |
| 252 | name = calloc(1, 32); |
| 253 | sprintf(name, "meson-gpio"); |
| 254 | |
| 255 | /* Create child device UCLASS_GPIO and bind it */ |
Jerome Brunet | 7c9dcfe | 2018-10-05 09:35:26 +0200 | [diff] [blame] | 256 | device_bind(dev, priv->data->gpio_driver, name, NULL, gpio, &gpio_dev); |
Beniamino Galvani | 2009a8d | 2017-07-10 00:30:04 +0200 | [diff] [blame] | 257 | dev_set_of_offset(gpio_dev, gpio); |
| 258 | |
Beniamino Galvani | 677b535 | 2016-08-16 11:49:49 +0200 | [diff] [blame] | 259 | return 0; |
| 260 | } |