Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch> |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 4 | * Copyright (C) 2021 Dario Binacchi <dariobin@libero.it> |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <dm/device_compat.h> |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 10 | #include <dm/devres.h> |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 11 | #include <dm/of_access.h> |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 12 | #include <dm/pinctrl.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 13 | #include <linux/libfdt.h> |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 14 | #include <linux/list.h> |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 15 | #include <asm/io.h> |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 16 | #include <sort.h> |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 17 | |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 18 | /** |
| 19 | * struct single_pdata - platform data |
| 20 | * @base: first configuration register |
| 21 | * @offset: index of last configuration register |
| 22 | * @mask: configuration-value mask bits |
| 23 | * @width: configuration register bit width |
| 24 | * @bits_per_mux: true if one register controls more than one pin |
| 25 | */ |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 26 | struct single_pdata { |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 27 | fdt_addr_t base; |
| 28 | int offset; |
| 29 | u32 mask; |
Dario Binacchi | 971c64a | 2021-04-11 09:39:44 +0200 | [diff] [blame] | 30 | u32 width; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 31 | bool bits_per_mux; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 34 | /** |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 35 | * struct single_func - pinctrl function |
| 36 | * @node: list node |
| 37 | * @name: pinctrl function name |
| 38 | * @npins: number of entries in pins array |
| 39 | * @pins: pins array |
| 40 | */ |
| 41 | struct single_func { |
| 42 | struct list_head node; |
| 43 | const char *name; |
| 44 | unsigned int npins; |
| 45 | unsigned int *pins; |
| 46 | }; |
| 47 | |
| 48 | /** |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 49 | * struct single_gpiofunc_range - pin ranges with same mux value of gpio fun |
| 50 | * @offset: offset base of pins |
| 51 | * @npins: number pins with the same mux value of gpio function |
| 52 | * @gpiofunc: mux value of gpio function |
| 53 | * @node: list node |
| 54 | */ |
| 55 | struct single_gpiofunc_range { |
| 56 | u32 offset; |
| 57 | u32 npins; |
| 58 | u32 gpiofunc; |
| 59 | struct list_head node; |
| 60 | }; |
| 61 | |
| 62 | /** |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 63 | * struct single_priv - private data |
| 64 | * @bits_per_pin: number of bits per pin |
| 65 | * @npins: number of selectable pins |
Dario Binacchi | 0b12162 | 2021-04-11 09:39:48 +0200 | [diff] [blame] | 66 | * @pin_name: temporary buffer to store the pin name |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 67 | * @functions: list pin functions |
| 68 | * @gpiofuncs: list gpio functions |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 69 | */ |
| 70 | struct single_priv { |
Dario Binacchi | 5532262 | 2021-04-11 09:39:50 +0200 | [diff] [blame] | 71 | #if (IS_ENABLED(CONFIG_SANDBOX)) |
| 72 | u32 *sandbox_regs; |
| 73 | #endif |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 74 | unsigned int bits_per_pin; |
| 75 | unsigned int npins; |
Dario Binacchi | 0b12162 | 2021-04-11 09:39:48 +0200 | [diff] [blame] | 76 | char pin_name[PINNAME_SIZE]; |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 77 | struct list_head functions; |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 78 | struct list_head gpiofuncs; |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 82 | * struct single_fdt_pin_cfg - pin configuration |
| 83 | * |
| 84 | * This structure is used for the pin configuration parameters in case |
| 85 | * the register controls only one pin. |
| 86 | * |
| 87 | * @reg: configuration register offset |
| 88 | * @val: configuration register value |
| 89 | */ |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 90 | struct single_fdt_pin_cfg { |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 91 | fdt32_t reg; |
| 92 | fdt32_t val; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 95 | /** |
| 96 | * struct single_fdt_bits_cfg - pin configuration |
| 97 | * |
| 98 | * This structure is used for the pin configuration parameters in case |
| 99 | * the register controls more than one pin. |
| 100 | * |
| 101 | * @reg: configuration register offset |
| 102 | * @val: configuration register value |
| 103 | * @mask: configuration register mask |
| 104 | */ |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 105 | struct single_fdt_bits_cfg { |
Dario Binacchi | 4ace4fa | 2021-04-11 09:39:39 +0200 | [diff] [blame] | 106 | fdt32_t reg; |
| 107 | fdt32_t val; |
| 108 | fdt32_t mask; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
Dario Binacchi | 5532262 | 2021-04-11 09:39:50 +0200 | [diff] [blame] | 111 | #if (!IS_ENABLED(CONFIG_SANDBOX)) |
| 112 | |
Dario Binacchi | 180531f | 2021-04-11 09:39:46 +0200 | [diff] [blame] | 113 | static unsigned int single_read(struct udevice *dev, fdt_addr_t reg) |
| 114 | { |
| 115 | struct single_pdata *pdata = dev_get_plat(dev); |
| 116 | |
| 117 | switch (pdata->width) { |
| 118 | case 8: |
| 119 | return readb(reg); |
| 120 | case 16: |
| 121 | return readw(reg); |
| 122 | default: /* 32 bits */ |
| 123 | return readl(reg); |
| 124 | } |
| 125 | |
| 126 | return readb(reg); |
| 127 | } |
| 128 | |
| 129 | static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg) |
| 130 | { |
| 131 | struct single_pdata *pdata = dev_get_plat(dev); |
| 132 | |
| 133 | switch (pdata->width) { |
| 134 | case 8: |
| 135 | writeb(val, reg); |
| 136 | break; |
| 137 | case 16: |
| 138 | writew(val, reg); |
| 139 | break; |
| 140 | default: /* 32 bits */ |
| 141 | writel(val, reg); |
| 142 | } |
| 143 | } |
| 144 | |
Dario Binacchi | 5532262 | 2021-04-11 09:39:50 +0200 | [diff] [blame] | 145 | #else /* CONFIG_SANDBOX */ |
| 146 | |
| 147 | static unsigned int single_read(struct udevice *dev, fdt_addr_t reg) |
| 148 | { |
| 149 | struct single_priv *priv = dev_get_priv(dev); |
| 150 | |
| 151 | return priv->sandbox_regs[reg]; |
| 152 | } |
| 153 | |
| 154 | static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg) |
| 155 | { |
| 156 | struct single_priv *priv = dev_get_priv(dev); |
| 157 | |
| 158 | priv->sandbox_regs[reg] = val; |
| 159 | } |
| 160 | |
| 161 | #endif /* CONFIG_SANDBOX */ |
| 162 | |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 163 | /** |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 164 | * single_get_pin_by_offset() - get a pin based on the register offset |
| 165 | * @dev: single driver instance |
| 166 | * @offset: register offset from the base |
| 167 | */ |
| 168 | static int single_get_pin_by_offset(struct udevice *dev, unsigned int offset) |
| 169 | { |
| 170 | struct single_pdata *pdata = dev_get_plat(dev); |
| 171 | struct single_priv *priv = dev_get_priv(dev); |
| 172 | |
| 173 | if (offset > pdata->offset) { |
| 174 | dev_err(dev, "mux offset out of range: 0x%x (0x%x)\n", |
| 175 | offset, pdata->offset); |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | if (pdata->bits_per_mux) |
| 180 | return (offset * BITS_PER_BYTE) / priv->bits_per_pin; |
| 181 | |
| 182 | return offset / (pdata->width / BITS_PER_BYTE); |
| 183 | } |
| 184 | |
| 185 | static int single_get_offset_by_pin(struct udevice *dev, unsigned int pin) |
| 186 | { |
| 187 | struct single_pdata *pdata = dev_get_plat(dev); |
| 188 | struct single_priv *priv = dev_get_priv(dev); |
| 189 | unsigned int mux_bytes; |
| 190 | |
| 191 | if (pin >= priv->npins) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | mux_bytes = pdata->width / BITS_PER_BYTE; |
| 195 | if (pdata->bits_per_mux) { |
| 196 | int byte_num; |
| 197 | |
| 198 | byte_num = (priv->bits_per_pin * pin) / BITS_PER_BYTE; |
| 199 | return (byte_num / mux_bytes) * mux_bytes; |
| 200 | } |
| 201 | |
| 202 | return pin * mux_bytes; |
| 203 | } |
| 204 | |
| 205 | static const char *single_get_pin_function(struct udevice *dev, |
| 206 | unsigned int pin) |
| 207 | { |
| 208 | struct single_priv *priv = dev_get_priv(dev); |
| 209 | struct single_func *func; |
| 210 | int i; |
| 211 | |
| 212 | list_for_each_entry(func, &priv->functions, node) { |
| 213 | for (i = 0; i < func->npins; i++) { |
| 214 | if (pin == func->pins[i]) |
| 215 | return func->name; |
| 216 | |
| 217 | if (pin < func->pins[i]) |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | static int single_get_pin_muxing(struct udevice *dev, unsigned int pin, |
| 226 | char *buf, int size) |
| 227 | { |
| 228 | struct single_pdata *pdata = dev_get_plat(dev); |
| 229 | struct single_priv *priv = dev_get_priv(dev); |
| 230 | fdt_addr_t reg; |
| 231 | const char *fname; |
| 232 | unsigned int val; |
| 233 | int offset, pin_shift = 0; |
| 234 | |
| 235 | offset = single_get_offset_by_pin(dev, pin); |
| 236 | if (offset < 0) |
| 237 | return offset; |
| 238 | |
| 239 | reg = pdata->base + offset; |
| 240 | val = single_read(dev, reg); |
| 241 | |
| 242 | if (pdata->bits_per_mux) |
| 243 | pin_shift = pin % (pdata->width / priv->bits_per_pin) * |
| 244 | priv->bits_per_pin; |
| 245 | |
| 246 | val &= (pdata->mask << pin_shift); |
| 247 | fname = single_get_pin_function(dev, pin); |
| 248 | snprintf(buf, size, "%pa 0x%08x %s", ®, val, |
| 249 | fname ? fname : "UNCLAIMED"); |
| 250 | return 0; |
| 251 | } |
| 252 | |
Bharat Gooty | fd921d2 | 2021-08-24 15:46:32 +0530 | [diff] [blame] | 253 | static int single_request(struct udevice *dev, int pin, int flags) |
| 254 | { |
| 255 | struct single_priv *priv = dev_get_priv(dev); |
| 256 | struct single_pdata *pdata = dev_get_plat(dev); |
| 257 | struct single_gpiofunc_range *frange = NULL; |
| 258 | struct list_head *pos, *tmp; |
| 259 | phys_addr_t reg; |
| 260 | int mux_bytes = 0; |
| 261 | u32 data; |
| 262 | |
| 263 | /* If function mask is null, needn't enable it. */ |
| 264 | if (!pdata->mask) |
| 265 | return -ENOTSUPP; |
| 266 | |
| 267 | list_for_each_safe(pos, tmp, &priv->gpiofuncs) { |
| 268 | frange = list_entry(pos, struct single_gpiofunc_range, node); |
| 269 | if ((pin >= frange->offset + frange->npins) || |
| 270 | pin < frange->offset) |
| 271 | continue; |
| 272 | |
| 273 | mux_bytes = pdata->width / BITS_PER_BYTE; |
| 274 | reg = pdata->base + pin * mux_bytes; |
| 275 | |
| 276 | data = single_read(dev, reg); |
| 277 | data &= ~pdata->mask; |
| 278 | data |= frange->gpiofunc; |
| 279 | single_write(dev, data, reg); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 286 | static struct single_func *single_allocate_function(struct udevice *dev, |
| 287 | unsigned int group_pins) |
| 288 | { |
| 289 | struct single_func *func; |
| 290 | |
| 291 | func = devm_kmalloc(dev, sizeof(*func), GFP_KERNEL); |
| 292 | if (!func) |
| 293 | return ERR_PTR(-ENOMEM); |
| 294 | |
| 295 | func->pins = devm_kmalloc(dev, sizeof(unsigned int) * group_pins, |
| 296 | GFP_KERNEL); |
| 297 | if (!func->pins) |
| 298 | return ERR_PTR(-ENOMEM); |
| 299 | |
| 300 | return func; |
| 301 | } |
| 302 | |
| 303 | static int single_pin_compare(const void *s1, const void *s2) |
| 304 | { |
| 305 | int pin1 = *(const unsigned int *)s1; |
| 306 | int pin2 = *(const unsigned int *)s2; |
| 307 | |
| 308 | return pin1 - pin2; |
| 309 | } |
| 310 | |
| 311 | /** |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 312 | * single_configure_pins() - Configure pins based on FDT data |
| 313 | * |
| 314 | * @dev: Pointer to single pin configuration device which is the parent of |
| 315 | * the pins node holding the pin configuration data. |
| 316 | * @pins: Pointer to the first element of an array of register/value pairs |
| 317 | * of type 'struct single_fdt_pin_cfg'. Each such pair describes the |
| 318 | * the pin to be configured and the value to be used for configuration. |
| 319 | * This pointer points to a 'pinctrl-single,pins' property in the |
| 320 | * device-tree. |
| 321 | * @size: Size of the 'pins' array in bytes. |
| 322 | * The number of register/value pairs in the 'pins' array therefore |
| 323 | * equals to 'size / sizeof(struct single_fdt_pin_cfg)'. |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 324 | * @fname: Function name. |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 325 | */ |
| 326 | static int single_configure_pins(struct udevice *dev, |
| 327 | const struct single_fdt_pin_cfg *pins, |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 328 | int size, const char *fname) |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 329 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 330 | struct single_pdata *pdata = dev_get_plat(dev); |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 331 | struct single_priv *priv = dev_get_priv(dev); |
| 332 | int n, pin, count = size / sizeof(struct single_fdt_pin_cfg); |
| 333 | struct single_func *func; |
Dario Binacchi | 6719294 | 2021-04-11 09:39:40 +0200 | [diff] [blame] | 334 | phys_addr_t reg; |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 335 | u32 offset, val; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 336 | |
Dario Binacchi | d85b93e | 2021-04-11 09:39:45 +0200 | [diff] [blame] | 337 | /* If function mask is null, needn't enable it. */ |
| 338 | if (!pdata->mask) |
| 339 | return 0; |
| 340 | |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 341 | func = single_allocate_function(dev, count); |
| 342 | if (IS_ERR(func)) |
| 343 | return PTR_ERR(func); |
| 344 | |
| 345 | func->name = fname; |
| 346 | func->npins = 0; |
James Balean | 46f51dc | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 347 | for (n = 0; n < count; n++, pins++) { |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 348 | offset = fdt32_to_cpu(pins->reg); |
Dario Binacchi | 230bc62 | 2021-04-22 22:28:56 +0200 | [diff] [blame] | 349 | if (offset > pdata->offset) { |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 350 | dev_err(dev, " invalid register offset 0x%x\n", |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 351 | offset); |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 352 | continue; |
| 353 | } |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 354 | |
| 355 | reg = pdata->base + offset; |
James Balean | 46f51dc | 2017-04-18 21:06:35 -0500 | [diff] [blame] | 356 | val = fdt32_to_cpu(pins->val) & pdata->mask; |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 357 | pin = single_get_pin_by_offset(dev, offset); |
| 358 | if (pin < 0) { |
| 359 | dev_err(dev, " failed to get pin by offset %x\n", |
| 360 | offset); |
| 361 | continue; |
| 362 | } |
| 363 | |
Dario Binacchi | 180531f | 2021-04-11 09:39:46 +0200 | [diff] [blame] | 364 | single_write(dev, (single_read(dev, reg) & ~pdata->mask) | val, |
| 365 | reg); |
Dario Binacchi | fcf6a2b | 2021-04-11 09:39:42 +0200 | [diff] [blame] | 366 | dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val); |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 367 | func->pins[func->npins] = pin; |
| 368 | func->npins++; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 369 | } |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 370 | |
| 371 | qsort(func->pins, func->npins, sizeof(func->pins[0]), |
| 372 | single_pin_compare); |
| 373 | list_add(&func->node, &priv->functions); |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 377 | static int single_configure_bits(struct udevice *dev, |
| 378 | const struct single_fdt_bits_cfg *pins, |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 379 | int size, const char *fname) |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 380 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 381 | struct single_pdata *pdata = dev_get_plat(dev); |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 382 | struct single_priv *priv = dev_get_priv(dev); |
| 383 | int n, pin, count = size / sizeof(struct single_fdt_bits_cfg); |
| 384 | int npins_in_reg, pin_num_from_lsb; |
| 385 | struct single_func *func; |
Dario Binacchi | 6719294 | 2021-04-11 09:39:40 +0200 | [diff] [blame] | 386 | phys_addr_t reg; |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 387 | u32 offset, val, mask, bit_pos, val_pos, mask_pos, submask; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 388 | |
Dario Binacchi | 69414d8 | 2021-04-22 18:35:58 +0200 | [diff] [blame] | 389 | /* If function mask is null, needn't enable it. */ |
| 390 | if (!pdata->mask) |
| 391 | return 0; |
| 392 | |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 393 | npins_in_reg = pdata->width / priv->bits_per_pin; |
| 394 | func = single_allocate_function(dev, count * npins_in_reg); |
| 395 | if (IS_ERR(func)) |
| 396 | return PTR_ERR(func); |
| 397 | |
| 398 | func->name = fname; |
| 399 | func->npins = 0; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 400 | for (n = 0; n < count; n++, pins++) { |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 401 | offset = fdt32_to_cpu(pins->reg); |
Dario Binacchi | 230bc62 | 2021-04-22 22:28:56 +0200 | [diff] [blame] | 402 | if (offset > pdata->offset) { |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 403 | dev_dbg(dev, " invalid register offset 0x%x\n", |
| 404 | offset); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 405 | continue; |
| 406 | } |
Dario Binacchi | 9b884e7 | 2021-04-11 09:39:41 +0200 | [diff] [blame] | 407 | |
| 408 | reg = pdata->base + offset; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 409 | |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 410 | pin = single_get_pin_by_offset(dev, offset); |
| 411 | if (pin < 0) { |
| 412 | dev_err(dev, " failed to get pin by offset 0x%pa\n", |
| 413 | ®); |
| 414 | continue; |
| 415 | } |
| 416 | |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 417 | mask = fdt32_to_cpu(pins->mask); |
| 418 | val = fdt32_to_cpu(pins->val) & mask; |
Dario Binacchi | 180531f | 2021-04-11 09:39:46 +0200 | [diff] [blame] | 419 | single_write(dev, (single_read(dev, reg) & ~mask) | val, reg); |
Dario Binacchi | fcf6a2b | 2021-04-11 09:39:42 +0200 | [diff] [blame] | 420 | dev_dbg(dev, " reg/val %pa/0x%08x\n", ®, val); |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 421 | |
| 422 | while (mask) { |
| 423 | bit_pos = __ffs(mask); |
| 424 | pin_num_from_lsb = bit_pos / priv->bits_per_pin; |
| 425 | mask_pos = pdata->mask << bit_pos; |
| 426 | val_pos = val & mask_pos; |
| 427 | submask = mask & mask_pos; |
| 428 | |
| 429 | if ((mask & mask_pos) == 0) { |
| 430 | dev_err(dev, "Invalid mask at 0x%x\n", offset); |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | mask &= ~mask_pos; |
| 435 | |
| 436 | if (submask != mask_pos) { |
| 437 | dev_warn(dev, |
| 438 | "Invalid submask 0x%x at 0x%x\n", |
| 439 | submask, offset); |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | func->pins[func->npins] = pin + pin_num_from_lsb; |
| 444 | func->npins++; |
| 445 | } |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 446 | } |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 447 | |
| 448 | qsort(func->pins, func->npins, sizeof(func->pins[0]), |
| 449 | single_pin_compare); |
| 450 | list_add(&func->node, &priv->functions); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 451 | return 0; |
| 452 | } |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 453 | static int single_set_state(struct udevice *dev, |
| 454 | struct udevice *config) |
| 455 | { |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 456 | const struct single_fdt_pin_cfg *prop; |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 457 | const struct single_fdt_bits_cfg *prop_bits; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 458 | int len; |
| 459 | |
Lokesh Vutla | dbfd9e0 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 460 | prop = dev_read_prop(config, "pinctrl-single,pins", &len); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 461 | |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 462 | if (prop) { |
| 463 | dev_dbg(dev, "configuring pins for %s\n", config->name); |
| 464 | if (len % sizeof(struct single_fdt_pin_cfg)) { |
| 465 | dev_dbg(dev, " invalid pin configuration in fdt\n"); |
| 466 | return -FDT_ERR_BADSTRUCTURE; |
| 467 | } |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 468 | single_configure_pins(dev, prop, len, config->name); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 469 | return 0; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 472 | /* pinctrl-single,pins not found so check for pinctrl-single,bits */ |
Lokesh Vutla | dbfd9e0 | 2020-04-22 22:55:31 +0530 | [diff] [blame] | 473 | prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 474 | if (prop_bits) { |
| 475 | dev_dbg(dev, "configuring pins for %s\n", config->name); |
| 476 | if (len % sizeof(struct single_fdt_bits_cfg)) { |
| 477 | dev_dbg(dev, " invalid bits configuration in fdt\n"); |
| 478 | return -FDT_ERR_BADSTRUCTURE; |
| 479 | } |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 480 | single_configure_bits(dev, prop_bits, len, config->name); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */ |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 485 | return len; |
| 486 | } |
| 487 | |
Dario Binacchi | 0b12162 | 2021-04-11 09:39:48 +0200 | [diff] [blame] | 488 | static const char *single_get_pin_name(struct udevice *dev, |
| 489 | unsigned int selector) |
| 490 | { |
| 491 | struct single_priv *priv = dev_get_priv(dev); |
| 492 | |
| 493 | if (selector >= priv->npins) |
| 494 | snprintf(priv->pin_name, PINNAME_SIZE, "Error"); |
| 495 | else |
| 496 | snprintf(priv->pin_name, PINNAME_SIZE, "PIN%u", selector); |
| 497 | |
| 498 | return priv->pin_name; |
| 499 | } |
| 500 | |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 501 | static int single_get_pins_count(struct udevice *dev) |
| 502 | { |
| 503 | struct single_priv *priv = dev_get_priv(dev); |
| 504 | |
| 505 | return priv->npins; |
| 506 | } |
| 507 | |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 508 | static int single_add_gpio_func(struct udevice *dev) |
| 509 | { |
| 510 | struct single_priv *priv = dev_get_priv(dev); |
| 511 | const char *propname = "pinctrl-single,gpio-range"; |
| 512 | const char *cellname = "#pinctrl-single,gpio-range-cells"; |
| 513 | struct single_gpiofunc_range *range; |
| 514 | struct ofnode_phandle_args gpiospec; |
| 515 | int ret, i; |
| 516 | |
| 517 | for (i = 0; ; i++) { |
| 518 | ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), propname, |
| 519 | cellname, 0, i, &gpiospec); |
| 520 | /* Do not treat it as error. Only treat it as end condition. */ |
| 521 | if (ret) { |
| 522 | ret = 0; |
| 523 | break; |
| 524 | } |
| 525 | range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL); |
| 526 | if (!range) { |
| 527 | ret = -ENOMEM; |
| 528 | break; |
| 529 | } |
| 530 | range->offset = gpiospec.args[0]; |
| 531 | range->npins = gpiospec.args[1]; |
| 532 | range->gpiofunc = gpiospec.args[2]; |
| 533 | list_add_tail(&range->node, &priv->gpiofuncs); |
| 534 | } |
| 535 | return ret; |
| 536 | } |
| 537 | |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 538 | static int single_probe(struct udevice *dev) |
| 539 | { |
| 540 | struct single_pdata *pdata = dev_get_plat(dev); |
| 541 | struct single_priv *priv = dev_get_priv(dev); |
| 542 | u32 size; |
| 543 | |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 544 | INIT_LIST_HEAD(&priv->functions); |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 545 | INIT_LIST_HEAD(&priv->gpiofuncs); |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 546 | |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 547 | size = pdata->offset + pdata->width / BITS_PER_BYTE; |
Dario Binacchi | 5532262 | 2021-04-11 09:39:50 +0200 | [diff] [blame] | 548 | #if (CONFIG_IS_ENABLED(SANDBOX)) |
| 549 | priv->sandbox_regs = |
| 550 | devm_kzalloc(dev, size * sizeof(*priv->sandbox_regs), |
| 551 | GFP_KERNEL); |
| 552 | if (!priv->sandbox_regs) |
| 553 | return -ENOMEM; |
| 554 | #endif |
| 555 | |
Simon Glass | 4d159b6 | 2021-05-13 19:39:28 -0600 | [diff] [blame] | 556 | /* looks like a possible divide by 0, but data->width avoids this */ |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 557 | priv->npins = size / (pdata->width / BITS_PER_BYTE); |
| 558 | if (pdata->bits_per_mux) { |
Dario Binacchi | 69414d8 | 2021-04-22 18:35:58 +0200 | [diff] [blame] | 559 | if (!pdata->mask) { |
| 560 | dev_err(dev, "function mask needs to be non-zero\n"); |
| 561 | return -EINVAL; |
| 562 | } |
| 563 | |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 564 | priv->bits_per_pin = fls(pdata->mask); |
| 565 | priv->npins *= (pdata->width / priv->bits_per_pin); |
| 566 | } |
| 567 | |
Bharat Gooty | 62f86c6 | 2021-08-24 15:46:31 +0530 | [diff] [blame] | 568 | if (single_add_gpio_func(dev)) |
| 569 | dev_dbg(dev, "gpio functions are not added\n"); |
| 570 | |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 571 | dev_dbg(dev, "%d pins\n", priv->npins); |
| 572 | return 0; |
| 573 | } |
| 574 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 575 | static int single_of_to_plat(struct udevice *dev) |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 576 | { |
| 577 | fdt_addr_t addr; |
Dario Binacchi | 9fd8a43 | 2021-04-11 09:39:43 +0200 | [diff] [blame] | 578 | fdt_size_t size; |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 579 | struct single_pdata *pdata = dev_get_plat(dev); |
Dario Binacchi | 971c64a | 2021-04-11 09:39:44 +0200 | [diff] [blame] | 580 | int ret; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 581 | |
Dario Binacchi | 971c64a | 2021-04-11 09:39:44 +0200 | [diff] [blame] | 582 | ret = dev_read_u32(dev, "pinctrl-single,register-width", &pdata->width); |
| 583 | if (ret) { |
| 584 | dev_err(dev, "missing register width\n"); |
| 585 | return ret; |
| 586 | } |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 587 | |
Dario Binacchi | 180531f | 2021-04-11 09:39:46 +0200 | [diff] [blame] | 588 | switch (pdata->width) { |
| 589 | case 8: |
| 590 | case 16: |
| 591 | case 32: |
| 592 | break; |
| 593 | default: |
| 594 | dev_err(dev, "wrong register width\n"); |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
Vignesh Raghavendra | 1e78790 | 2021-05-07 14:40:34 +0530 | [diff] [blame] | 598 | addr = dev_read_addr_size_index(dev, 0, &size); |
Dario Binacchi | 9fd8a43 | 2021-04-11 09:39:43 +0200 | [diff] [blame] | 599 | if (addr == FDT_ADDR_T_NONE) { |
Vignesh Raghavendra | 1e78790 | 2021-05-07 14:40:34 +0530 | [diff] [blame] | 600 | dev_err(dev, "failed to get base register address\n"); |
Dario Binacchi | 9fd8a43 | 2021-04-11 09:39:43 +0200 | [diff] [blame] | 601 | return -EINVAL; |
| 602 | } |
| 603 | |
| 604 | pdata->offset = size - pdata->width / BITS_PER_BYTE; |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 605 | pdata->base = addr; |
| 606 | |
Dario Binacchi | d85b93e | 2021-04-11 09:39:45 +0200 | [diff] [blame] | 607 | ret = dev_read_u32(dev, "pinctrl-single,function-mask", &pdata->mask); |
| 608 | if (ret) { |
| 609 | pdata->mask = 0; |
| 610 | dev_warn(dev, "missing function register mask\n"); |
| 611 | } |
| 612 | |
Patrick Delaunay | 719cab6 | 2020-01-13 11:34:55 +0100 | [diff] [blame] | 613 | pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux"); |
Adam Ford | 159a887 | 2019-06-10 13:15:55 -0500 | [diff] [blame] | 614 | |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | const struct pinctrl_ops single_pinctrl_ops = { |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 619 | .get_pins_count = single_get_pins_count, |
Dario Binacchi | 0b12162 | 2021-04-11 09:39:48 +0200 | [diff] [blame] | 620 | .get_pin_name = single_get_pin_name, |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 621 | .set_state = single_set_state, |
Dario Binacchi | 76d470d | 2021-04-11 09:39:49 +0200 | [diff] [blame] | 622 | .get_pin_muxing = single_get_pin_muxing, |
Bharat Gooty | fd921d2 | 2021-08-24 15:46:32 +0530 | [diff] [blame] | 623 | .request = single_request, |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | static const struct udevice_id single_pinctrl_match[] = { |
| 627 | { .compatible = "pinctrl-single" }, |
| 628 | { /* sentinel */ } |
| 629 | }; |
| 630 | |
| 631 | U_BOOT_DRIVER(single_pinctrl) = { |
| 632 | .name = "single-pinctrl", |
| 633 | .id = UCLASS_PINCTRL, |
| 634 | .of_match = single_pinctrl_match, |
| 635 | .ops = &single_pinctrl_ops, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 636 | .plat_auto = sizeof(struct single_pdata), |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 637 | .priv_auto = sizeof(struct single_priv), |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 638 | .of_to_plat = single_of_to_plat, |
Dario Binacchi | 1dd7ae2 | 2021-04-11 09:39:47 +0200 | [diff] [blame] | 639 | .probe = single_probe, |
Felix Brack | 44d5c37 | 2017-03-22 11:26:44 +0100 | [diff] [blame] | 640 | }; |