Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Apple SoC pinctrl+GPIO+external IRQ driver |
| 4 | * |
| 5 | * Copyright (C) The Asahi Linux Contributors |
| 6 | * Copyright (C) 2020 Corellium LLC |
| 7 | * |
| 8 | * Based on: pinctrl-pistachio.c |
| 9 | * Copyright (C) 2014 Imagination Technologies Ltd. |
| 10 | * Copyright (C) 2014 Google, Inc. |
| 11 | */ |
| 12 | |
| 13 | #include <dt-bindings/pinctrl/apple.h> |
Joey Gouly | 7c06f08 | 2021-11-21 16:56:35 +0000 | [diff] [blame^] | 14 | #include <linux/bits.h> |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | #include <linux/pinctrl/pinctrl.h> |
| 22 | #include <linux/pinctrl/pinmux.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regmap.h> |
| 25 | |
| 26 | #include "pinctrl-utils.h" |
| 27 | #include "core.h" |
| 28 | #include "pinmux.h" |
| 29 | |
| 30 | struct apple_gpio_pinctrl { |
| 31 | struct device *dev; |
| 32 | struct pinctrl_dev *pctldev; |
| 33 | |
| 34 | void __iomem *base; |
| 35 | struct regmap *map; |
| 36 | |
| 37 | struct pinctrl_desc pinctrl_desc; |
| 38 | struct gpio_chip gpio_chip; |
| 39 | struct irq_chip irq_chip; |
| 40 | u8 irqgrps[0]; |
| 41 | }; |
| 42 | |
| 43 | #define REG_GPIO(x) (4 * (x)) |
| 44 | #define REG_GPIOx_DATA BIT(0) |
| 45 | #define REG_GPIOx_MODE GENMASK(3, 1) |
| 46 | #define REG_GPIOx_OUT 1 |
| 47 | #define REG_GPIOx_IN_IRQ_HI 2 |
| 48 | #define REG_GPIOx_IN_IRQ_LO 3 |
| 49 | #define REG_GPIOx_IN_IRQ_UP 4 |
| 50 | #define REG_GPIOx_IN_IRQ_DN 5 |
| 51 | #define REG_GPIOx_IN_IRQ_ANY 6 |
| 52 | #define REG_GPIOx_IN_IRQ_OFF 7 |
| 53 | #define REG_GPIOx_PERIPH GENMASK(6, 5) |
| 54 | #define REG_GPIOx_PULL GENMASK(8, 7) |
| 55 | #define REG_GPIOx_PULL_OFF 0 |
| 56 | #define REG_GPIOx_PULL_DOWN 1 |
| 57 | #define REG_GPIOx_PULL_UP_STRONG 2 |
| 58 | #define REG_GPIOx_PULL_UP 3 |
| 59 | #define REG_GPIOx_INPUT_ENABLE BIT(9) |
| 60 | #define REG_GPIOx_DRIVE_STRENGTH0 GENMASK(11, 10) |
| 61 | #define REG_GPIOx_SCHMITT BIT(15) |
| 62 | #define REG_GPIOx_GRP GENMASK(18, 16) |
| 63 | #define REG_GPIOx_LOCK BIT(21) |
| 64 | #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22) |
| 65 | #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5)) |
| 66 | |
| 67 | struct regmap_config regmap_config = { |
| 68 | .reg_bits = 32, |
| 69 | .val_bits = 32, |
| 70 | .reg_stride = 4, |
| 71 | .cache_type = REGCACHE_FLAT, |
| 72 | .max_register = 512 * sizeof(u32), |
| 73 | .num_reg_defaults_raw = 512, |
Joey Gouly | 5ad6973 | 2021-11-21 16:56:33 +0000 | [diff] [blame] | 74 | .use_relaxed_mmio = true, |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Joey Gouly | 67a6c28 | 2021-11-21 16:56:34 +0000 | [diff] [blame] | 77 | /* No locking needed to mask/unmask IRQs as the interrupt mode is per pin-register. */ |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 78 | static void apple_gpio_set_reg(struct apple_gpio_pinctrl *pctl, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 79 | unsigned int pin, u32 mask, u32 value) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 80 | { |
| 81 | regmap_update_bits(pctl->map, REG_GPIO(pin), mask, value); |
| 82 | } |
| 83 | |
| 84 | static uint32_t apple_gpio_get_reg(struct apple_gpio_pinctrl *pctl, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 85 | unsigned int pin) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 86 | { |
| 87 | unsigned int val = 0; |
| 88 | |
| 89 | regmap_read(pctl->map, REG_GPIO(pin), &val); |
| 90 | return val; |
| 91 | } |
| 92 | |
| 93 | /* Pin controller functions */ |
| 94 | |
| 95 | static int apple_gpio_dt_node_to_map(struct pinctrl_dev *pctldev, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 96 | struct device_node *node, |
| 97 | struct pinctrl_map **map, |
| 98 | unsigned *num_maps) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 99 | { |
| 100 | unsigned reserved_maps; |
| 101 | struct apple_gpio_pinctrl *pctl; |
| 102 | u32 pinfunc, pin, func; |
| 103 | int num_pins, i, ret; |
| 104 | const char *group_name; |
| 105 | const char *function_name; |
| 106 | |
| 107 | *map = NULL; |
| 108 | *num_maps = 0; |
| 109 | reserved_maps = 0; |
| 110 | |
| 111 | pctl = pinctrl_dev_get_drvdata(pctldev); |
| 112 | |
| 113 | ret = of_property_count_u32_elems(node, "pinmux"); |
| 114 | if (ret <= 0) { |
| 115 | dev_err(pctl->dev, |
| 116 | "missing or empty pinmux property in node %pOFn.\n", |
| 117 | node); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | num_pins = ret; |
| 122 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 123 | ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps, num_maps, num_pins); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | for (i = 0; i < num_pins; i++) { |
| 128 | ret = of_property_read_u32_index(node, "pinmux", i, &pinfunc); |
| 129 | if (ret) |
| 130 | goto free_map; |
| 131 | |
| 132 | pin = APPLE_PIN(pinfunc); |
| 133 | func = APPLE_FUNC(pinfunc); |
| 134 | |
| 135 | if (func >= pinmux_generic_get_function_count(pctldev)) { |
| 136 | ret = -EINVAL; |
| 137 | goto free_map; |
| 138 | } |
| 139 | |
| 140 | group_name = pinctrl_generic_get_group_name(pctldev, pin); |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 141 | function_name = pinmux_generic_get_function_name(pctl->pctldev, func); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 142 | ret = pinctrl_utils_add_map_mux(pctl->pctldev, map, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 143 | &reserved_maps, num_maps, |
| 144 | group_name, function_name); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 145 | if (ret) |
| 146 | goto free_map; |
| 147 | } |
| 148 | |
| 149 | free_map: |
| 150 | if (ret < 0) |
| 151 | pinctrl_utils_free_map(pctldev, *map, *num_maps); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static const struct pinctrl_ops apple_gpio_pinctrl_ops = { |
| 157 | .get_groups_count = pinctrl_generic_get_group_count, |
| 158 | .get_group_name = pinctrl_generic_get_group_name, |
| 159 | .get_group_pins = pinctrl_generic_get_group_pins, |
| 160 | .dt_node_to_map = apple_gpio_dt_node_to_map, |
| 161 | .dt_free_map = pinctrl_utils_free_map, |
| 162 | }; |
| 163 | |
| 164 | /* Pin multiplexer functions */ |
| 165 | |
| 166 | static int apple_gpio_pinmux_set(struct pinctrl_dev *pctldev, unsigned func, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 167 | unsigned group) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 168 | { |
| 169 | struct apple_gpio_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 170 | |
| 171 | apple_gpio_set_reg( |
| 172 | pctl, group, REG_GPIOx_PERIPH | REG_GPIOx_INPUT_ENABLE, |
| 173 | FIELD_PREP(REG_GPIOx_PERIPH, func) | REG_GPIOx_INPUT_ENABLE); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static const struct pinmux_ops apple_gpio_pinmux_ops = { |
| 179 | .get_functions_count = pinmux_generic_get_function_count, |
| 180 | .get_function_name = pinmux_generic_get_function_name, |
| 181 | .get_function_groups = pinmux_generic_get_function_groups, |
| 182 | .set_mux = apple_gpio_pinmux_set, |
| 183 | .strict = true, |
| 184 | }; |
| 185 | |
| 186 | /* GPIO chip functions */ |
| 187 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 188 | static int apple_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 189 | { |
| 190 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 191 | unsigned int reg = apple_gpio_get_reg(pctl, offset); |
| 192 | |
| 193 | return (FIELD_GET(REG_GPIOx_MODE, reg) == REG_GPIOx_OUT) ? |
| 194 | GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; |
| 195 | } |
| 196 | |
| 197 | static int apple_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 198 | { |
| 199 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 200 | unsigned int reg = apple_gpio_get_reg(pctl, offset); |
| 201 | |
| 202 | /* |
| 203 | * If this is an input GPIO, read the actual value (not the |
| 204 | * cached regmap value) |
| 205 | */ |
| 206 | if (FIELD_GET(REG_GPIOx_MODE, reg) != REG_GPIOx_OUT) |
| 207 | reg = readl_relaxed(pctl->base + REG_GPIO(offset)); |
| 208 | |
| 209 | return !!(reg & REG_GPIOx_DATA); |
| 210 | } |
| 211 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 212 | static void apple_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 213 | { |
| 214 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 215 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 216 | apple_gpio_set_reg(pctl, offset, REG_GPIOx_DATA, value ? REG_GPIOx_DATA : 0); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 219 | static int apple_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 220 | { |
| 221 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 222 | |
| 223 | apple_gpio_set_reg(pctl, offset, |
| 224 | REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA | |
| 225 | REG_GPIOx_INPUT_ENABLE, |
| 226 | FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF) | |
| 227 | REG_GPIOx_INPUT_ENABLE); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int apple_gpio_direction_output(struct gpio_chip *chip, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 232 | unsigned int offset, int value) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 233 | { |
| 234 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 235 | |
| 236 | apple_gpio_set_reg(pctl, offset, |
| 237 | REG_GPIOx_PERIPH | REG_GPIOx_MODE | REG_GPIOx_DATA, |
| 238 | FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_OUT) | |
| 239 | (value ? REG_GPIOx_DATA : 0)); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* IRQ chip functions */ |
| 244 | |
| 245 | static void apple_gpio_irq_ack(struct irq_data *data) |
| 246 | { |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 247 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data)); |
| 248 | unsigned int irqgrp = FIELD_GET(REG_GPIOx_GRP, apple_gpio_get_reg(pctl, data->hwirq)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 249 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 250 | writel(BIT(data->hwirq & 31), pctl->base + REG_IRQ(irqgrp, data->hwirq)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Sven Peter | 9b3b94e | 2021-11-01 16:06:40 +0100 | [diff] [blame] | 253 | static unsigned int apple_gpio_irq_type(unsigned int type) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 254 | { |
| 255 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 256 | case IRQ_TYPE_EDGE_RISING: |
| 257 | return REG_GPIOx_IN_IRQ_UP; |
| 258 | case IRQ_TYPE_EDGE_FALLING: |
| 259 | return REG_GPIOx_IN_IRQ_DN; |
| 260 | case IRQ_TYPE_EDGE_BOTH: |
| 261 | return REG_GPIOx_IN_IRQ_ANY; |
| 262 | case IRQ_TYPE_LEVEL_HIGH: |
| 263 | return REG_GPIOx_IN_IRQ_HI; |
| 264 | case IRQ_TYPE_LEVEL_LOW: |
| 265 | return REG_GPIOx_IN_IRQ_LO; |
| 266 | default: |
Sven Peter | 9b3b94e | 2021-11-01 16:06:40 +0100 | [diff] [blame] | 267 | return REG_GPIOx_IN_IRQ_OFF; |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | static void apple_gpio_irq_mask(struct irq_data *data) |
| 272 | { |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 273 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data)); |
| 274 | |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 275 | apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 276 | FIELD_PREP(REG_GPIOx_MODE, REG_GPIOx_IN_IRQ_OFF)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void apple_gpio_irq_unmask(struct irq_data *data) |
| 280 | { |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 281 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data)); |
Sven Peter | 9b3b94e | 2021-11-01 16:06:40 +0100 | [diff] [blame] | 282 | unsigned int irqtype = apple_gpio_irq_type(irqd_get_trigger_type(data)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 283 | |
| 284 | apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 285 | FIELD_PREP(REG_GPIOx_MODE, irqtype)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static unsigned int apple_gpio_irq_startup(struct irq_data *data) |
| 289 | { |
| 290 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
| 291 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(chip); |
| 292 | |
| 293 | apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_GRP, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 294 | FIELD_PREP(REG_GPIOx_GRP, 0)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 295 | |
| 296 | apple_gpio_direction_input(chip, data->hwirq); |
| 297 | apple_gpio_irq_unmask(data); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 302 | static int apple_gpio_irq_set_type(struct irq_data *data, unsigned int type) |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 303 | { |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 304 | struct apple_gpio_pinctrl *pctl = gpiochip_get_data(irq_data_get_irq_chip_data(data)); |
Sven Peter | 9b3b94e | 2021-11-01 16:06:40 +0100 | [diff] [blame] | 305 | unsigned int irqtype = apple_gpio_irq_type(type); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 306 | |
Sven Peter | 9b3b94e | 2021-11-01 16:06:40 +0100 | [diff] [blame] | 307 | if (irqtype == REG_GPIOx_IN_IRQ_OFF) |
| 308 | return -EINVAL; |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 309 | |
| 310 | apple_gpio_set_reg(pctl, data->hwirq, REG_GPIOx_MODE, |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 311 | FIELD_PREP(REG_GPIOx_MODE, irqtype)); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 312 | |
| 313 | if (type & IRQ_TYPE_LEVEL_MASK) |
| 314 | irq_set_handler_locked(data, handle_level_irq); |
| 315 | else |
| 316 | irq_set_handler_locked(data, handle_edge_irq); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void apple_gpio_irq_handler(struct irq_desc *desc) |
| 321 | { |
| 322 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 323 | u8 *grpp = irq_desc_get_handler_data(desc); |
| 324 | struct apple_gpio_pinctrl *pctl; |
| 325 | unsigned int pinh, pinl; |
| 326 | unsigned long pending; |
| 327 | struct gpio_chip *gc; |
| 328 | |
| 329 | pctl = container_of(grpp - *grpp, typeof(*pctl), irqgrps[0]); |
| 330 | gc = &pctl->gpio_chip; |
| 331 | |
| 332 | chained_irq_enter(chip, desc); |
| 333 | for (pinh = 0; pinh < gc->ngpio; pinh += 32) { |
| 334 | pending = readl_relaxed(pctl->base + REG_IRQ(*grpp, pinh)); |
| 335 | for_each_set_bit(pinl, &pending, 32) |
| 336 | generic_handle_domain_irq(gc->irq.domain, pinh + pinl); |
| 337 | } |
| 338 | chained_irq_exit(chip, desc); |
| 339 | } |
| 340 | |
| 341 | static struct irq_chip apple_gpio_irqchip = { |
| 342 | .name = "Apple-GPIO", |
| 343 | .irq_startup = apple_gpio_irq_startup, |
| 344 | .irq_ack = apple_gpio_irq_ack, |
| 345 | .irq_mask = apple_gpio_irq_mask, |
| 346 | .irq_unmask = apple_gpio_irq_unmask, |
| 347 | .irq_set_type = apple_gpio_irq_set_type, |
| 348 | }; |
| 349 | |
| 350 | /* Probe & register */ |
| 351 | |
| 352 | static int apple_gpio_register(struct apple_gpio_pinctrl *pctl) |
| 353 | { |
| 354 | struct gpio_irq_chip *girq = &pctl->gpio_chip.irq; |
| 355 | void **irq_data = NULL; |
| 356 | int ret; |
| 357 | |
| 358 | if (!of_property_read_bool(pctl->dev->of_node, "gpio-controller")) |
| 359 | return dev_err_probe(pctl->dev, -ENODEV, |
| 360 | "No gpio-controller property\n"); |
| 361 | |
| 362 | pctl->irq_chip = apple_gpio_irqchip; |
| 363 | |
| 364 | pctl->gpio_chip.label = dev_name(pctl->dev); |
| 365 | pctl->gpio_chip.request = gpiochip_generic_request; |
| 366 | pctl->gpio_chip.free = gpiochip_generic_free; |
| 367 | pctl->gpio_chip.get_direction = apple_gpio_get_direction; |
| 368 | pctl->gpio_chip.direction_input = apple_gpio_direction_input; |
| 369 | pctl->gpio_chip.direction_output = apple_gpio_direction_output; |
| 370 | pctl->gpio_chip.get = apple_gpio_get; |
| 371 | pctl->gpio_chip.set = apple_gpio_set; |
| 372 | pctl->gpio_chip.base = -1; |
| 373 | pctl->gpio_chip.ngpio = pctl->pinctrl_desc.npins; |
| 374 | pctl->gpio_chip.parent = pctl->dev; |
| 375 | pctl->gpio_chip.of_node = pctl->dev->of_node; |
| 376 | |
| 377 | if (girq->num_parents) { |
| 378 | int i; |
| 379 | |
| 380 | girq->chip = &pctl->irq_chip; |
| 381 | girq->parent_handler = apple_gpio_irq_handler; |
| 382 | |
| 383 | girq->parents = kmalloc_array(girq->num_parents, |
| 384 | sizeof(*girq->parents), |
| 385 | GFP_KERNEL); |
| 386 | irq_data = kmalloc_array(girq->num_parents, sizeof(*irq_data), |
| 387 | GFP_KERNEL); |
| 388 | if (!girq->parents || !irq_data) { |
| 389 | ret = -ENOMEM; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | for (i = 0; i < girq->num_parents; i++) { |
Joey Gouly | 361856d | 2021-11-21 16:56:32 +0000 | [diff] [blame] | 394 | ret = platform_get_irq(to_platform_device(pctl->dev), i); |
Joey Gouly | a0f160f | 2021-10-26 18:58:14 +0100 | [diff] [blame] | 395 | if (ret < 0) |
| 396 | goto out; |
| 397 | |
| 398 | girq->parents[i] = ret; |
| 399 | pctl->irqgrps[i] = i; |
| 400 | irq_data[i] = &pctl->irqgrps[i]; |
| 401 | } |
| 402 | |
| 403 | girq->parent_handler_data_array = irq_data; |
| 404 | girq->per_parent_data = true; |
| 405 | girq->default_type = IRQ_TYPE_NONE; |
| 406 | girq->handler = handle_level_irq; |
| 407 | } |
| 408 | |
| 409 | ret = devm_gpiochip_add_data(pctl->dev, &pctl->gpio_chip, pctl); |
| 410 | out: |
| 411 | kfree(girq->parents); |
| 412 | kfree(irq_data); |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | static int apple_gpio_pinctrl_probe(struct platform_device *pdev) |
| 418 | { |
| 419 | struct apple_gpio_pinctrl *pctl; |
| 420 | struct pinctrl_pin_desc *pins; |
| 421 | unsigned int npins; |
| 422 | const char **pin_names; |
| 423 | unsigned int *pin_nums; |
| 424 | static const char* pinmux_functions[] = { |
| 425 | "gpio", "periph1", "periph2", "periph3" |
| 426 | }; |
| 427 | unsigned int i, nirqs = 0; |
| 428 | int res; |
| 429 | |
| 430 | if (of_property_read_bool(pdev->dev.of_node, "interrupt-controller")) { |
| 431 | res = platform_irq_count(pdev); |
| 432 | if (res > 0) |
| 433 | nirqs = res; |
| 434 | } |
| 435 | |
| 436 | pctl = devm_kzalloc(&pdev->dev, struct_size(pctl, irqgrps, nirqs), |
| 437 | GFP_KERNEL); |
| 438 | if (!pctl) |
| 439 | return -ENOMEM; |
| 440 | pctl->dev = &pdev->dev; |
| 441 | pctl->gpio_chip.irq.num_parents = nirqs; |
| 442 | dev_set_drvdata(&pdev->dev, pctl); |
| 443 | |
| 444 | if (of_property_read_u32(pdev->dev.of_node, "apple,npins", &npins)) |
| 445 | return dev_err_probe(&pdev->dev, -EINVAL, |
| 446 | "apple,npins property not found\n"); |
| 447 | |
| 448 | pins = devm_kmalloc_array(&pdev->dev, npins, sizeof(pins[0]), |
| 449 | GFP_KERNEL); |
| 450 | pin_names = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_names[0]), |
| 451 | GFP_KERNEL); |
| 452 | pin_nums = devm_kmalloc_array(&pdev->dev, npins, sizeof(pin_nums[0]), |
| 453 | GFP_KERNEL); |
| 454 | if (!pins || !pin_names || !pin_nums) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | pctl->base = devm_platform_ioremap_resource(pdev, 0); |
| 458 | if (IS_ERR(pctl->base)) |
| 459 | return PTR_ERR(pctl->base); |
| 460 | |
| 461 | pctl->map = devm_regmap_init_mmio(&pdev->dev, pctl->base, ®map_config); |
| 462 | if (IS_ERR(pctl->map)) |
| 463 | return dev_err_probe(&pdev->dev, PTR_ERR(pctl->map), |
| 464 | "Failed to create regmap\n"); |
| 465 | |
| 466 | for (i = 0; i < npins; i++) { |
| 467 | pins[i].number = i; |
| 468 | pins[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "PIN%u", i); |
| 469 | pins[i].drv_data = pctl; |
| 470 | pin_names[i] = pins[i].name; |
| 471 | pin_nums[i] = i; |
| 472 | } |
| 473 | |
| 474 | pctl->pinctrl_desc.name = dev_name(pctl->dev); |
| 475 | pctl->pinctrl_desc.pins = pins; |
| 476 | pctl->pinctrl_desc.npins = npins; |
| 477 | pctl->pinctrl_desc.pctlops = &apple_gpio_pinctrl_ops; |
| 478 | pctl->pinctrl_desc.pmxops = &apple_gpio_pinmux_ops; |
| 479 | |
| 480 | pctl->pctldev = devm_pinctrl_register(&pdev->dev, &pctl->pinctrl_desc, pctl); |
| 481 | if (IS_ERR(pctl->pctldev)) |
| 482 | return dev_err_probe(&pdev->dev, PTR_ERR(pctl->pctldev), |
| 483 | "Failed to register pinctrl device.\n"); |
| 484 | |
| 485 | for (i = 0; i < npins; i++) { |
| 486 | res = pinctrl_generic_add_group(pctl->pctldev, pins[i].name, |
| 487 | pin_nums + i, 1, pctl); |
| 488 | if (res < 0) |
| 489 | return dev_err_probe(pctl->dev, res, |
| 490 | "Failed to register group"); |
| 491 | } |
| 492 | |
| 493 | for (i = 0; i < ARRAY_SIZE(pinmux_functions); ++i) { |
| 494 | res = pinmux_generic_add_function(pctl->pctldev, pinmux_functions[i], |
| 495 | pin_names, npins, pctl); |
| 496 | if (res < 0) |
| 497 | return dev_err_probe(pctl->dev, res, |
| 498 | "Failed to register function."); |
| 499 | } |
| 500 | |
| 501 | return apple_gpio_register(pctl); |
| 502 | } |
| 503 | |
| 504 | static const struct of_device_id apple_gpio_pinctrl_of_match[] = { |
| 505 | { .compatible = "apple,pinctrl", }, |
| 506 | { } |
| 507 | }; |
| 508 | |
| 509 | static struct platform_driver apple_gpio_pinctrl_driver = { |
| 510 | .driver = { |
| 511 | .name = "apple-gpio-pinctrl", |
| 512 | .of_match_table = apple_gpio_pinctrl_of_match, |
| 513 | .suppress_bind_attrs = true, |
| 514 | }, |
| 515 | .probe = apple_gpio_pinctrl_probe, |
| 516 | }; |
| 517 | module_platform_driver(apple_gpio_pinctrl_driver); |
| 518 | |
| 519 | MODULE_DESCRIPTION("Apple pinctrl/GPIO driver"); |
| 520 | MODULE_AUTHOR("Stan Skowronek <stan@corellium.com>"); |
| 521 | MODULE_AUTHOR("Joey Gouly <joey.gouly@arm.com>"); |
| 522 | MODULE_LICENSE("GPL v2"); |