Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Broadcom Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation version 2. |
| 7 | * |
| 8 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 9 | * kind, whether express or implied; without even the implied warranty |
| 10 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/bitops.h> |
| 15 | #include <linux/gpio/driver.h> |
| 16 | #include <linux/of_device.h> |
| 17 | #include <linux/of_irq.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/basic_mmio_gpio.h> |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 20 | #include <linux/irqdomain.h> |
| 21 | #include <linux/irqchip/chained_irq.h> |
| 22 | #include <linux/interrupt.h> |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 23 | #include <linux/reboot.h> |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 24 | |
| 25 | #define GIO_BANK_SIZE 0x20 |
| 26 | #define GIO_ODEN(bank) (((bank) * GIO_BANK_SIZE) + 0x00) |
| 27 | #define GIO_DATA(bank) (((bank) * GIO_BANK_SIZE) + 0x04) |
| 28 | #define GIO_IODIR(bank) (((bank) * GIO_BANK_SIZE) + 0x08) |
| 29 | #define GIO_EC(bank) (((bank) * GIO_BANK_SIZE) + 0x0c) |
| 30 | #define GIO_EI(bank) (((bank) * GIO_BANK_SIZE) + 0x10) |
| 31 | #define GIO_MASK(bank) (((bank) * GIO_BANK_SIZE) + 0x14) |
| 32 | #define GIO_LEVEL(bank) (((bank) * GIO_BANK_SIZE) + 0x18) |
| 33 | #define GIO_STAT(bank) (((bank) * GIO_BANK_SIZE) + 0x1c) |
| 34 | |
| 35 | struct brcmstb_gpio_bank { |
| 36 | struct list_head node; |
| 37 | int id; |
| 38 | struct bgpio_chip bgc; |
| 39 | struct brcmstb_gpio_priv *parent_priv; |
| 40 | u32 width; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 41 | struct irq_chip irq_chip; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct brcmstb_gpio_priv { |
| 45 | struct list_head bank_list; |
| 46 | void __iomem *reg_base; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 47 | struct platform_device *pdev; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 48 | int parent_irq; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 49 | int gpio_base; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 50 | bool can_wake; |
| 51 | int parent_wake_irq; |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 52 | struct notifier_block reboot_notifier; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | #define MAX_GPIO_PER_BANK 32 |
| 56 | #define GPIO_BANK(gpio) ((gpio) >> 5) |
| 57 | /* assumes MAX_GPIO_PER_BANK is a multiple of 2 */ |
| 58 | #define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1)) |
| 59 | |
| 60 | static inline struct brcmstb_gpio_bank * |
| 61 | brcmstb_gpio_gc_to_bank(struct gpio_chip *gc) |
| 62 | { |
| 63 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 64 | return container_of(bgc, struct brcmstb_gpio_bank, bgc); |
| 65 | } |
| 66 | |
| 67 | static inline struct brcmstb_gpio_priv * |
| 68 | brcmstb_gpio_gc_to_priv(struct gpio_chip *gc) |
| 69 | { |
| 70 | struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc); |
| 71 | return bank->parent_priv; |
| 72 | } |
| 73 | |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 74 | static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank, |
| 75 | unsigned int offset, bool enable) |
| 76 | { |
| 77 | struct bgpio_chip *bgc = &bank->bgc; |
| 78 | struct brcmstb_gpio_priv *priv = bank->parent_priv; |
| 79 | u32 mask = bgc->pin2mask(bgc, offset); |
| 80 | u32 imask; |
| 81 | unsigned long flags; |
| 82 | |
| 83 | spin_lock_irqsave(&bgc->lock, flags); |
| 84 | imask = bgc->read_reg(priv->reg_base + GIO_MASK(bank->id)); |
| 85 | if (enable) |
| 86 | imask |= mask; |
| 87 | else |
| 88 | imask &= ~mask; |
| 89 | bgc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask); |
| 90 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 91 | } |
| 92 | |
| 93 | /* -------------------- IRQ chip functions -------------------- */ |
| 94 | |
| 95 | static void brcmstb_gpio_irq_mask(struct irq_data *d) |
| 96 | { |
| 97 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 98 | struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc); |
| 99 | |
| 100 | brcmstb_gpio_set_imask(bank, d->hwirq, false); |
| 101 | } |
| 102 | |
| 103 | static void brcmstb_gpio_irq_unmask(struct irq_data *d) |
| 104 | { |
| 105 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 106 | struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc); |
| 107 | |
| 108 | brcmstb_gpio_set_imask(bank, d->hwirq, true); |
| 109 | } |
| 110 | |
| 111 | static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 112 | { |
| 113 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 114 | struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc); |
| 115 | struct brcmstb_gpio_priv *priv = bank->parent_priv; |
| 116 | u32 mask = BIT(d->hwirq); |
| 117 | u32 edge_insensitive, iedge_insensitive; |
| 118 | u32 edge_config, iedge_config; |
| 119 | u32 level, ilevel; |
| 120 | unsigned long flags; |
| 121 | |
| 122 | switch (type) { |
| 123 | case IRQ_TYPE_LEVEL_LOW: |
| 124 | level = 0; |
| 125 | edge_config = 0; |
| 126 | edge_insensitive = 0; |
| 127 | break; |
| 128 | case IRQ_TYPE_LEVEL_HIGH: |
| 129 | level = mask; |
| 130 | edge_config = 0; |
| 131 | edge_insensitive = 0; |
| 132 | break; |
| 133 | case IRQ_TYPE_EDGE_FALLING: |
| 134 | level = 0; |
| 135 | edge_config = 0; |
| 136 | edge_insensitive = 0; |
| 137 | break; |
| 138 | case IRQ_TYPE_EDGE_RISING: |
| 139 | level = 0; |
| 140 | edge_config = mask; |
| 141 | edge_insensitive = 0; |
| 142 | break; |
| 143 | case IRQ_TYPE_EDGE_BOTH: |
| 144 | level = 0; |
| 145 | edge_config = 0; /* don't care, but want known value */ |
| 146 | edge_insensitive = mask; |
| 147 | break; |
| 148 | default: |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | spin_lock_irqsave(&bank->bgc.lock, flags); |
| 153 | |
| 154 | iedge_config = bank->bgc.read_reg(priv->reg_base + |
| 155 | GIO_EC(bank->id)) & ~mask; |
| 156 | iedge_insensitive = bank->bgc.read_reg(priv->reg_base + |
| 157 | GIO_EI(bank->id)) & ~mask; |
| 158 | ilevel = bank->bgc.read_reg(priv->reg_base + |
| 159 | GIO_LEVEL(bank->id)) & ~mask; |
| 160 | |
| 161 | bank->bgc.write_reg(priv->reg_base + GIO_EC(bank->id), |
| 162 | iedge_config | edge_config); |
| 163 | bank->bgc.write_reg(priv->reg_base + GIO_EI(bank->id), |
| 164 | iedge_insensitive | edge_insensitive); |
| 165 | bank->bgc.write_reg(priv->reg_base + GIO_LEVEL(bank->id), |
| 166 | ilevel | level); |
| 167 | |
| 168 | spin_unlock_irqrestore(&bank->bgc.lock, flags); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 172 | static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv, |
| 173 | unsigned int enable) |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 174 | { |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 175 | int ret = 0; |
| 176 | |
| 177 | /* |
| 178 | * Only enable wake IRQ once for however many hwirqs can wake |
| 179 | * since they all use the same wake IRQ. Mask will be set |
| 180 | * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag. |
| 181 | */ |
| 182 | if (enable) |
| 183 | ret = enable_irq_wake(priv->parent_wake_irq); |
| 184 | else |
| 185 | ret = disable_irq_wake(priv->parent_wake_irq); |
| 186 | if (ret) |
| 187 | dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n", |
| 188 | enable ? "enable" : "disable"); |
| 189 | return ret; |
| 190 | } |
| 191 | |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 192 | static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable) |
| 193 | { |
| 194 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
| 195 | struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc); |
| 196 | |
| 197 | return brcmstb_gpio_priv_set_wake(priv, enable); |
| 198 | } |
| 199 | |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 200 | static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data) |
| 201 | { |
| 202 | struct brcmstb_gpio_priv *priv = data; |
| 203 | |
| 204 | if (!priv || irq != priv->parent_wake_irq) |
| 205 | return IRQ_NONE; |
| 206 | pm_wakeup_event(&priv->pdev->dev, 0); |
| 207 | return IRQ_HANDLED; |
| 208 | } |
| 209 | |
| 210 | static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank) |
| 211 | { |
| 212 | struct brcmstb_gpio_priv *priv = bank->parent_priv; |
| 213 | struct irq_domain *irq_domain = bank->bgc.gc.irqdomain; |
| 214 | void __iomem *reg_base = priv->reg_base; |
| 215 | unsigned long status; |
| 216 | unsigned long flags; |
| 217 | |
| 218 | spin_lock_irqsave(&bank->bgc.lock, flags); |
| 219 | while ((status = bank->bgc.read_reg(reg_base + GIO_STAT(bank->id)) & |
| 220 | bank->bgc.read_reg(reg_base + GIO_MASK(bank->id)))) { |
| 221 | int bit; |
| 222 | |
| 223 | for_each_set_bit(bit, &status, 32) { |
| 224 | u32 stat = bank->bgc.read_reg(reg_base + |
| 225 | GIO_STAT(bank->id)); |
| 226 | if (bit >= bank->width) |
| 227 | dev_warn(&priv->pdev->dev, |
| 228 | "IRQ for invalid GPIO (bank=%d, offset=%d)\n", |
| 229 | bank->id, bit); |
| 230 | bank->bgc.write_reg(reg_base + GIO_STAT(bank->id), |
| 231 | stat | BIT(bit)); |
| 232 | generic_handle_irq(irq_find_mapping(irq_domain, bit)); |
| 233 | } |
| 234 | } |
| 235 | spin_unlock_irqrestore(&bank->bgc.lock, flags); |
| 236 | } |
| 237 | |
| 238 | /* Each UPG GIO block has one IRQ for all banks */ |
| 239 | static void brcmstb_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 240 | { |
| 241 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
| 242 | struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc); |
| 243 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 244 | struct list_head *pos; |
| 245 | |
| 246 | /* Interrupts weren't properly cleared during probe */ |
| 247 | BUG_ON(!priv || !chip); |
| 248 | |
| 249 | chained_irq_enter(chip, desc); |
| 250 | list_for_each(pos, &priv->bank_list) { |
| 251 | struct brcmstb_gpio_bank *bank = |
| 252 | list_entry(pos, struct brcmstb_gpio_bank, node); |
| 253 | brcmstb_gpio_irq_bank_handler(bank); |
| 254 | } |
| 255 | chained_irq_exit(chip, desc); |
| 256 | } |
| 257 | |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 258 | static int brcmstb_gpio_reboot(struct notifier_block *nb, |
| 259 | unsigned long action, void *data) |
| 260 | { |
| 261 | struct brcmstb_gpio_priv *priv = |
| 262 | container_of(nb, struct brcmstb_gpio_priv, reboot_notifier); |
| 263 | |
| 264 | /* Enable GPIO for S5 cold boot */ |
| 265 | if (action == SYS_POWER_OFF) |
| 266 | brcmstb_gpio_priv_set_wake(priv, 1); |
| 267 | |
| 268 | return NOTIFY_DONE; |
| 269 | } |
| 270 | |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 271 | /* Make sure that the number of banks matches up between properties */ |
| 272 | static int brcmstb_gpio_sanity_check_banks(struct device *dev, |
| 273 | struct device_node *np, struct resource *res) |
| 274 | { |
| 275 | int res_num_banks = resource_size(res) / GIO_BANK_SIZE; |
| 276 | int num_banks = |
| 277 | of_property_count_u32_elems(np, "brcm,gpio-bank-widths"); |
| 278 | |
| 279 | if (res_num_banks != num_banks) { |
| 280 | dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n", |
| 281 | res_num_banks, num_banks); |
| 282 | return -EINVAL; |
| 283 | } else { |
| 284 | return 0; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static int brcmstb_gpio_remove(struct platform_device *pdev) |
| 289 | { |
| 290 | struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev); |
| 291 | struct list_head *pos; |
| 292 | struct brcmstb_gpio_bank *bank; |
| 293 | int ret = 0; |
| 294 | |
| 295 | list_for_each(pos, &priv->bank_list) { |
| 296 | bank = list_entry(pos, struct brcmstb_gpio_bank, node); |
| 297 | ret = bgpio_remove(&bank->bgc); |
| 298 | if (ret) |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 299 | dev_err(&pdev->dev, "gpiochip_remove fail in cleanup\n"); |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 300 | } |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 301 | if (priv->reboot_notifier.notifier_call) { |
| 302 | ret = unregister_reboot_notifier(&priv->reboot_notifier); |
| 303 | if (ret) |
| 304 | dev_err(&pdev->dev, |
| 305 | "failed to unregister reboot notifier\n"); |
| 306 | } |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static int brcmstb_gpio_of_xlate(struct gpio_chip *gc, |
| 311 | const struct of_phandle_args *gpiospec, u32 *flags) |
| 312 | { |
| 313 | struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc); |
| 314 | struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc); |
| 315 | int offset; |
| 316 | |
| 317 | if (gc->of_gpio_n_cells != 2) { |
| 318 | WARN_ON(1); |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | |
| 322 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | offset = gpiospec->args[0] - (gc->base - priv->gpio_base); |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 326 | if (offset >= gc->ngpio || offset < 0) |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 327 | return -EINVAL; |
| 328 | |
| 329 | if (unlikely(offset >= bank->width)) { |
| 330 | dev_warn_ratelimited(&priv->pdev->dev, |
| 331 | "Received request for invalid GPIO offset %d\n", |
| 332 | gpiospec->args[0]); |
| 333 | } |
| 334 | |
| 335 | if (flags) |
| 336 | *flags = gpiospec->args[1]; |
| 337 | |
| 338 | return offset; |
| 339 | } |
| 340 | |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 341 | /* Before calling, must have bank->parent_irq set and gpiochip registered */ |
| 342 | static int brcmstb_gpio_irq_setup(struct platform_device *pdev, |
| 343 | struct brcmstb_gpio_bank *bank) |
| 344 | { |
| 345 | struct brcmstb_gpio_priv *priv = bank->parent_priv; |
| 346 | struct device *dev = &pdev->dev; |
| 347 | struct device_node *np = dev->of_node; |
| 348 | |
| 349 | bank->irq_chip.name = dev_name(dev); |
| 350 | bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask; |
| 351 | bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask; |
| 352 | bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type; |
| 353 | |
| 354 | /* Ensures that all non-wakeup IRQs are disabled at suspend */ |
| 355 | bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND; |
| 356 | |
| 357 | if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->can_wake && |
| 358 | of_property_read_bool(np, "wakeup-source")) { |
| 359 | priv->parent_wake_irq = platform_get_irq(pdev, 1); |
| 360 | if (priv->parent_wake_irq < 0) { |
| 361 | dev_warn(dev, |
| 362 | "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep"); |
| 363 | } else { |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 364 | int err; |
| 365 | |
| 366 | /* |
| 367 | * Set wakeup capability before requesting wakeup |
| 368 | * interrupt, so we can process boot-time "wakeups" |
| 369 | * (e.g., from S5 cold boot) |
| 370 | */ |
| 371 | device_set_wakeup_capable(dev, true); |
| 372 | device_wakeup_enable(dev); |
| 373 | err = devm_request_irq(dev, priv->parent_wake_irq, |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 374 | brcmstb_gpio_wake_irq_handler, 0, |
| 375 | "brcmstb-gpio-wake", priv); |
| 376 | |
| 377 | if (err < 0) { |
| 378 | dev_err(dev, "Couldn't request wake IRQ"); |
| 379 | return err; |
| 380 | } |
| 381 | |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 382 | priv->reboot_notifier.notifier_call = |
| 383 | brcmstb_gpio_reboot; |
| 384 | register_reboot_notifier(&priv->reboot_notifier); |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 385 | priv->can_wake = true; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (priv->can_wake) |
| 390 | bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake; |
| 391 | |
| 392 | gpiochip_irqchip_add(&bank->bgc.gc, &bank->irq_chip, 0, |
| 393 | handle_simple_irq, IRQ_TYPE_NONE); |
| 394 | gpiochip_set_chained_irqchip(&bank->bgc.gc, &bank->irq_chip, |
| 395 | priv->parent_irq, brcmstb_gpio_irq_handler); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 400 | static int brcmstb_gpio_probe(struct platform_device *pdev) |
| 401 | { |
| 402 | struct device *dev = &pdev->dev; |
| 403 | struct device_node *np = dev->of_node; |
| 404 | void __iomem *reg_base; |
| 405 | struct brcmstb_gpio_priv *priv; |
| 406 | struct resource *res; |
| 407 | struct property *prop; |
| 408 | const __be32 *p; |
| 409 | u32 bank_width; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 410 | int num_banks = 0; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 411 | int err; |
| 412 | static int gpio_base; |
| 413 | |
| 414 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 415 | if (!priv) |
| 416 | return -ENOMEM; |
| 417 | |
| 418 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 419 | reg_base = devm_ioremap_resource(dev, res); |
| 420 | if (IS_ERR(reg_base)) |
| 421 | return PTR_ERR(reg_base); |
| 422 | |
| 423 | priv->gpio_base = gpio_base; |
| 424 | priv->reg_base = reg_base; |
| 425 | priv->pdev = pdev; |
| 426 | |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 427 | if (of_property_read_bool(np, "interrupt-controller")) { |
| 428 | priv->parent_irq = platform_get_irq(pdev, 0); |
| 429 | if (priv->parent_irq <= 0) { |
| 430 | dev_err(dev, "Couldn't get IRQ"); |
| 431 | return -ENOENT; |
| 432 | } |
| 433 | } else { |
| 434 | priv->parent_irq = -ENOENT; |
| 435 | } |
| 436 | |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 437 | INIT_LIST_HEAD(&priv->bank_list); |
| 438 | if (brcmstb_gpio_sanity_check_banks(dev, np, res)) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p, |
| 442 | bank_width) { |
| 443 | struct brcmstb_gpio_bank *bank; |
| 444 | struct bgpio_chip *bgc; |
| 445 | struct gpio_chip *gc; |
| 446 | |
| 447 | bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL); |
| 448 | if (!bank) { |
| 449 | err = -ENOMEM; |
| 450 | goto fail; |
| 451 | } |
| 452 | |
| 453 | bank->parent_priv = priv; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 454 | bank->id = num_banks; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 455 | if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) { |
| 456 | dev_err(dev, "Invalid bank width %d\n", bank_width); |
| 457 | goto fail; |
| 458 | } else { |
| 459 | bank->width = bank_width; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Regs are 4 bytes wide, have data reg, no set/clear regs, |
| 464 | * and direction bits have 0 = output and 1 = input |
| 465 | */ |
| 466 | bgc = &bank->bgc; |
| 467 | err = bgpio_init(bgc, dev, 4, |
| 468 | reg_base + GIO_DATA(bank->id), |
| 469 | NULL, NULL, NULL, |
| 470 | reg_base + GIO_IODIR(bank->id), 0); |
| 471 | if (err) { |
| 472 | dev_err(dev, "bgpio_init() failed\n"); |
| 473 | goto fail; |
| 474 | } |
| 475 | |
| 476 | gc = &bgc->gc; |
| 477 | gc->of_node = np; |
| 478 | gc->owner = THIS_MODULE; |
| 479 | gc->label = np->full_name; |
| 480 | gc->base = gpio_base; |
| 481 | gc->of_gpio_n_cells = 2; |
| 482 | gc->of_xlate = brcmstb_gpio_of_xlate; |
| 483 | /* not all ngpio lines are valid, will use bank width later */ |
| 484 | gc->ngpio = MAX_GPIO_PER_BANK; |
| 485 | |
Gregory Fong | 3afa129 | 2015-07-31 18:17:44 -0700 | [diff] [blame^] | 486 | /* |
| 487 | * Mask all interrupts by default, since wakeup interrupts may |
| 488 | * be retained from S5 cold boot |
| 489 | */ |
| 490 | bank->bgc.write_reg(reg_base + GIO_MASK(bank->id), 0); |
| 491 | |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 492 | err = gpiochip_add(gc); |
| 493 | if (err) { |
| 494 | dev_err(dev, "Could not add gpiochip for bank %d\n", |
| 495 | bank->id); |
| 496 | goto fail; |
| 497 | } |
| 498 | gpio_base += gc->ngpio; |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 499 | |
| 500 | if (priv->parent_irq > 0) { |
| 501 | err = brcmstb_gpio_irq_setup(pdev, bank); |
| 502 | if (err) |
| 503 | goto fail; |
| 504 | } |
| 505 | |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 506 | dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id, |
| 507 | gc->base, gc->ngpio, bank->width); |
| 508 | |
| 509 | /* Everything looks good, so add bank to list */ |
| 510 | list_add(&bank->node, &priv->bank_list); |
| 511 | |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 512 | num_banks++; |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n", |
Gregory Fong | 19a7b69 | 2015-07-31 18:17:43 -0700 | [diff] [blame] | 516 | num_banks, priv->gpio_base, gpio_base - 1); |
Gregory Fong | 3b0213d | 2015-05-28 19:14:05 -0700 | [diff] [blame] | 517 | |
| 518 | platform_set_drvdata(pdev, priv); |
| 519 | |
| 520 | return 0; |
| 521 | |
| 522 | fail: |
| 523 | (void) brcmstb_gpio_remove(pdev); |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | static const struct of_device_id brcmstb_gpio_of_match[] = { |
| 528 | { .compatible = "brcm,brcmstb-gpio" }, |
| 529 | {}, |
| 530 | }; |
| 531 | |
| 532 | MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match); |
| 533 | |
| 534 | static struct platform_driver brcmstb_gpio_driver = { |
| 535 | .driver = { |
| 536 | .name = "brcmstb-gpio", |
| 537 | .of_match_table = brcmstb_gpio_of_match, |
| 538 | }, |
| 539 | .probe = brcmstb_gpio_probe, |
| 540 | .remove = brcmstb_gpio_remove, |
| 541 | }; |
| 542 | module_platform_driver(brcmstb_gpio_driver); |
| 543 | |
| 544 | MODULE_AUTHOR("Gregory Fong"); |
| 545 | MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO"); |
| 546 | MODULE_LICENSE("GPL v2"); |