blob: fd8c0412f8af2ba0d7274801455c84e559091f47 [file] [log] [blame]
Gregory Fong3b0213d2015-05-28 19:14:05 -07001/*
Doug Berger0752df62017-10-24 12:54:46 -07002 * Copyright (C) 2015-2017 Broadcom
Gregory Fong3b0213d2015-05-28 19:14:05 -07003 *
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>
Gregory Fong19a7b692015-07-31 18:17:43 -070019#include <linux/irqdomain.h>
20#include <linux/irqchip/chained_irq.h>
21#include <linux/interrupt.h>
Gregory Fong3afa1292015-07-31 18:17:44 -070022#include <linux/reboot.h>
Linus Walleijd7442362017-10-20 15:45:34 +020023#include <linux/bitops.h>
Gregory Fong3b0213d2015-05-28 19:14:05 -070024
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
35struct brcmstb_gpio_bank {
36 struct list_head node;
37 int id;
Linus Walleij0f4630f2015-12-04 14:02:58 +010038 struct gpio_chip gc;
Gregory Fong3b0213d2015-05-28 19:14:05 -070039 struct brcmstb_gpio_priv *parent_priv;
40 u32 width;
Gregory Fong19a7b692015-07-31 18:17:43 -070041 struct irq_chip irq_chip;
Gregory Fong3b0213d2015-05-28 19:14:05 -070042};
43
44struct brcmstb_gpio_priv {
45 struct list_head bank_list;
46 void __iomem *reg_base;
Gregory Fong3b0213d2015-05-28 19:14:05 -070047 struct platform_device *pdev;
Gregory Fong19a7b692015-07-31 18:17:43 -070048 int parent_irq;
Gregory Fong3b0213d2015-05-28 19:14:05 -070049 int gpio_base;
Gregory Fong19a7b692015-07-31 18:17:43 -070050 int parent_wake_irq;
Gregory Fong3afa1292015-07-31 18:17:44 -070051 struct notifier_block reboot_notifier;
Gregory Fong3b0213d2015-05-28 19:14:05 -070052};
53
54#define MAX_GPIO_PER_BANK 32
55#define GPIO_BANK(gpio) ((gpio) >> 5)
56/* assumes MAX_GPIO_PER_BANK is a multiple of 2 */
57#define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1))
58
Gregory Fong3b0213d2015-05-28 19:14:05 -070059static inline struct brcmstb_gpio_priv *
60brcmstb_gpio_gc_to_priv(struct gpio_chip *gc)
61{
Linus Walleij0f4630f2015-12-04 14:02:58 +010062 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -070063 return bank->parent_priv;
64}
65
Doug Berger142c1682017-10-24 12:54:47 -070066static unsigned long
67brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank)
68{
69 void __iomem *reg_base = bank->parent_priv->reg_base;
70 unsigned long status;
71 unsigned long flags;
72
73 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
74 status = bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) &
75 bank->gc.read_reg(reg_base + GIO_MASK(bank->id));
76 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
77
78 return status;
79}
80
Gregory Fong19a7b692015-07-31 18:17:43 -070081static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
82 unsigned int offset, bool enable)
83{
Linus Walleij0f4630f2015-12-04 14:02:58 +010084 struct gpio_chip *gc = &bank->gc;
Gregory Fong19a7b692015-07-31 18:17:43 -070085 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Gregory Fong19a7b692015-07-31 18:17:43 -070086 u32 imask;
87 unsigned long flags;
88
Linus Walleij0f4630f2015-12-04 14:02:58 +010089 spin_lock_irqsave(&gc->bgpio_lock, flags);
90 imask = gc->read_reg(priv->reg_base + GIO_MASK(bank->id));
Gregory Fong19a7b692015-07-31 18:17:43 -070091 if (enable)
Linus Walleijd7442362017-10-20 15:45:34 +020092 imask |= BIT(offset);
Gregory Fong19a7b692015-07-31 18:17:43 -070093 else
Linus Walleijd7442362017-10-20 15:45:34 +020094 imask &= ~BIT(offset);
Linus Walleij0f4630f2015-12-04 14:02:58 +010095 gc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask);
96 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -070097}
98
99/* -------------------- IRQ chip functions -------------------- */
100
101static void brcmstb_gpio_irq_mask(struct irq_data *d)
102{
103 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100104 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700105
106 brcmstb_gpio_set_imask(bank, d->hwirq, false);
107}
108
109static void brcmstb_gpio_irq_unmask(struct irq_data *d)
110{
111 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100112 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700113
114 brcmstb_gpio_set_imask(bank, d->hwirq, true);
115}
116
117static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
118{
119 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100120 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700121 struct brcmstb_gpio_priv *priv = bank->parent_priv;
122 u32 mask = BIT(d->hwirq);
123 u32 edge_insensitive, iedge_insensitive;
124 u32 edge_config, iedge_config;
125 u32 level, ilevel;
126 unsigned long flags;
127
128 switch (type) {
129 case IRQ_TYPE_LEVEL_LOW:
130 level = 0;
131 edge_config = 0;
132 edge_insensitive = 0;
133 break;
134 case IRQ_TYPE_LEVEL_HIGH:
135 level = mask;
136 edge_config = 0;
137 edge_insensitive = 0;
138 break;
139 case IRQ_TYPE_EDGE_FALLING:
140 level = 0;
141 edge_config = 0;
142 edge_insensitive = 0;
143 break;
144 case IRQ_TYPE_EDGE_RISING:
145 level = 0;
146 edge_config = mask;
147 edge_insensitive = 0;
148 break;
149 case IRQ_TYPE_EDGE_BOTH:
150 level = 0;
151 edge_config = 0; /* don't care, but want known value */
152 edge_insensitive = mask;
153 break;
154 default:
155 return -EINVAL;
156 }
157
Linus Walleij0f4630f2015-12-04 14:02:58 +0100158 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700159
Linus Walleij0f4630f2015-12-04 14:02:58 +0100160 iedge_config = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700161 GIO_EC(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100162 iedge_insensitive = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700163 GIO_EI(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100164 ilevel = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700165 GIO_LEVEL(bank->id)) & ~mask;
166
Linus Walleij0f4630f2015-12-04 14:02:58 +0100167 bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700168 iedge_config | edge_config);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100169 bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700170 iedge_insensitive | edge_insensitive);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100171 bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700172 ilevel | level);
173
Linus Walleij0f4630f2015-12-04 14:02:58 +0100174 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700175 return 0;
176}
177
Gregory Fong3afa1292015-07-31 18:17:44 -0700178static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
179 unsigned int enable)
Gregory Fong19a7b692015-07-31 18:17:43 -0700180{
Gregory Fong19a7b692015-07-31 18:17:43 -0700181 int ret = 0;
182
183 /*
184 * Only enable wake IRQ once for however many hwirqs can wake
185 * since they all use the same wake IRQ. Mask will be set
186 * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
187 */
188 if (enable)
189 ret = enable_irq_wake(priv->parent_wake_irq);
190 else
191 ret = disable_irq_wake(priv->parent_wake_irq);
192 if (ret)
193 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
194 enable ? "enable" : "disable");
195 return ret;
196}
197
Gregory Fong3afa1292015-07-31 18:17:44 -0700198static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
199{
200 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
201 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
202
203 return brcmstb_gpio_priv_set_wake(priv, enable);
204}
205
Gregory Fong19a7b692015-07-31 18:17:43 -0700206static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
207{
208 struct brcmstb_gpio_priv *priv = data;
209
210 if (!priv || irq != priv->parent_wake_irq)
211 return IRQ_NONE;
212 pm_wakeup_event(&priv->pdev->dev, 0);
213 return IRQ_HANDLED;
214}
215
216static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
217{
218 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100219 struct irq_domain *irq_domain = bank->gc.irqdomain;
Gregory Fong19a7b692015-07-31 18:17:43 -0700220 void __iomem *reg_base = priv->reg_base;
221 unsigned long status;
Gregory Fong19a7b692015-07-31 18:17:43 -0700222
Doug Berger142c1682017-10-24 12:54:47 -0700223 while ((status = brcmstb_gpio_get_active_irqs(bank))) {
Gregory Fong19a7b692015-07-31 18:17:43 -0700224 int bit;
225
226 for_each_set_bit(bit, &status, 32) {
Linus Walleij0f4630f2015-12-04 14:02:58 +0100227 u32 stat = bank->gc.read_reg(reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700228 GIO_STAT(bank->id));
229 if (bit >= bank->width)
230 dev_warn(&priv->pdev->dev,
231 "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
232 bank->id, bit);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100233 bank->gc.write_reg(reg_base + GIO_STAT(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700234 stat | BIT(bit));
235 generic_handle_irq(irq_find_mapping(irq_domain, bit));
236 }
237 }
Gregory Fong19a7b692015-07-31 18:17:43 -0700238}
239
240/* Each UPG GIO block has one IRQ for all banks */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200241static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
Gregory Fong19a7b692015-07-31 18:17:43 -0700242{
243 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
244 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
245 struct irq_chip *chip = irq_desc_get_chip(desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800246 struct brcmstb_gpio_bank *bank;
Gregory Fong19a7b692015-07-31 18:17:43 -0700247
248 /* Interrupts weren't properly cleared during probe */
249 BUG_ON(!priv || !chip);
250
251 chained_irq_enter(chip, desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800252 list_for_each_entry(bank, &priv->bank_list, node)
Gregory Fong19a7b692015-07-31 18:17:43 -0700253 brcmstb_gpio_irq_bank_handler(bank);
Gregory Fong19a7b692015-07-31 18:17:43 -0700254 chained_irq_exit(chip, desc);
255}
256
Gregory Fong3afa1292015-07-31 18:17:44 -0700257static int brcmstb_gpio_reboot(struct notifier_block *nb,
258 unsigned long action, void *data)
259{
260 struct brcmstb_gpio_priv *priv =
261 container_of(nb, struct brcmstb_gpio_priv, reboot_notifier);
262
263 /* Enable GPIO for S5 cold boot */
264 if (action == SYS_POWER_OFF)
265 brcmstb_gpio_priv_set_wake(priv, 1);
266
267 return NOTIFY_DONE;
268}
269
Gregory Fong3b0213d2015-05-28 19:14:05 -0700270/* Make sure that the number of banks matches up between properties */
271static int brcmstb_gpio_sanity_check_banks(struct device *dev,
272 struct device_node *np, struct resource *res)
273{
274 int res_num_banks = resource_size(res) / GIO_BANK_SIZE;
275 int num_banks =
276 of_property_count_u32_elems(np, "brcm,gpio-bank-widths");
277
278 if (res_num_banks != num_banks) {
279 dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n",
280 res_num_banks, num_banks);
281 return -EINVAL;
282 } else {
283 return 0;
284 }
285}
286
287static int brcmstb_gpio_remove(struct platform_device *pdev)
288{
289 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700290 struct brcmstb_gpio_bank *bank;
291 int ret = 0;
292
Gregory Fong22526072015-06-17 18:00:40 -0700293 if (!priv) {
294 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
295 return -EFAULT;
296 }
297
298 /*
299 * You can lose return values below, but we report all errors, and it's
300 * more important to actually perform all of the steps.
301 */
Axel Linb178e7e2016-02-20 09:50:37 +0800302 list_for_each_entry(bank, &priv->bank_list, node)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100303 gpiochip_remove(&bank->gc);
Axel Linb178e7e2016-02-20 09:50:37 +0800304
Gregory Fong3afa1292015-07-31 18:17:44 -0700305 if (priv->reboot_notifier.notifier_call) {
306 ret = unregister_reboot_notifier(&priv->reboot_notifier);
307 if (ret)
308 dev_err(&pdev->dev,
309 "failed to unregister reboot notifier\n");
310 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700311 return ret;
312}
313
314static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
315 const struct of_phandle_args *gpiospec, u32 *flags)
316{
317 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100318 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700319 int offset;
320
321 if (gc->of_gpio_n_cells != 2) {
322 WARN_ON(1);
323 return -EINVAL;
324 }
325
326 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
327 return -EINVAL;
328
329 offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
Gregory Fong19a7b692015-07-31 18:17:43 -0700330 if (offset >= gc->ngpio || offset < 0)
Gregory Fong3b0213d2015-05-28 19:14:05 -0700331 return -EINVAL;
332
333 if (unlikely(offset >= bank->width)) {
334 dev_warn_ratelimited(&priv->pdev->dev,
335 "Received request for invalid GPIO offset %d\n",
336 gpiospec->args[0]);
337 }
338
339 if (flags)
340 *flags = gpiospec->args[1];
341
342 return offset;
343}
344
Gregory Fong19a7b692015-07-31 18:17:43 -0700345/* Before calling, must have bank->parent_irq set and gpiochip registered */
346static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
347 struct brcmstb_gpio_bank *bank)
348{
349 struct brcmstb_gpio_priv *priv = bank->parent_priv;
350 struct device *dev = &pdev->dev;
351 struct device_node *np = dev->of_node;
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900352 int err;
Gregory Fong19a7b692015-07-31 18:17:43 -0700353
354 bank->irq_chip.name = dev_name(dev);
355 bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
356 bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
357 bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
358
359 /* Ensures that all non-wakeup IRQs are disabled at suspend */
360 bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
361
Doug Berger0752df62017-10-24 12:54:46 -0700362 if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->parent_wake_irq &&
Gregory Fong19a7b692015-07-31 18:17:43 -0700363 of_property_read_bool(np, "wakeup-source")) {
364 priv->parent_wake_irq = platform_get_irq(pdev, 1);
365 if (priv->parent_wake_irq < 0) {
Doug Berger0752df62017-10-24 12:54:46 -0700366 priv->parent_wake_irq = 0;
Gregory Fong19a7b692015-07-31 18:17:43 -0700367 dev_warn(dev,
368 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
369 } else {
Gregory Fong3afa1292015-07-31 18:17:44 -0700370 /*
371 * Set wakeup capability before requesting wakeup
372 * interrupt, so we can process boot-time "wakeups"
373 * (e.g., from S5 cold boot)
374 */
375 device_set_wakeup_capable(dev, true);
376 device_wakeup_enable(dev);
377 err = devm_request_irq(dev, priv->parent_wake_irq,
Doug Berger0752df62017-10-24 12:54:46 -0700378 brcmstb_gpio_wake_irq_handler,
379 IRQF_SHARED,
380 "brcmstb-gpio-wake", priv);
Gregory Fong19a7b692015-07-31 18:17:43 -0700381
382 if (err < 0) {
383 dev_err(dev, "Couldn't request wake IRQ");
384 return err;
385 }
386
Gregory Fong3afa1292015-07-31 18:17:44 -0700387 priv->reboot_notifier.notifier_call =
388 brcmstb_gpio_reboot;
389 register_reboot_notifier(&priv->reboot_notifier);
Gregory Fong19a7b692015-07-31 18:17:43 -0700390 }
391 }
392
Doug Berger0752df62017-10-24 12:54:46 -0700393 if (priv->parent_wake_irq)
Gregory Fong19a7b692015-07-31 18:17:43 -0700394 bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
395
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900396 err = gpiochip_irqchip_add(&bank->gc, &bank->irq_chip, 0,
397 handle_simple_irq, IRQ_TYPE_NONE);
398 if (err)
399 return err;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100400 gpiochip_set_chained_irqchip(&bank->gc, &bank->irq_chip,
Gregory Fong19a7b692015-07-31 18:17:43 -0700401 priv->parent_irq, brcmstb_gpio_irq_handler);
402
403 return 0;
404}
405
Gregory Fong3b0213d2015-05-28 19:14:05 -0700406static int brcmstb_gpio_probe(struct platform_device *pdev)
407{
408 struct device *dev = &pdev->dev;
409 struct device_node *np = dev->of_node;
410 void __iomem *reg_base;
411 struct brcmstb_gpio_priv *priv;
412 struct resource *res;
413 struct property *prop;
414 const __be32 *p;
415 u32 bank_width;
Gregory Fong19a7b692015-07-31 18:17:43 -0700416 int num_banks = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700417 int err;
418 static int gpio_base;
Florian Fainellice5a7e82016-01-06 10:55:22 -0800419 unsigned long flags = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700420
421 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
422 if (!priv)
423 return -ENOMEM;
Gregory Fong22526072015-06-17 18:00:40 -0700424 platform_set_drvdata(pdev, priv);
425 INIT_LIST_HEAD(&priv->bank_list);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700426
427 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 reg_base = devm_ioremap_resource(dev, res);
429 if (IS_ERR(reg_base))
430 return PTR_ERR(reg_base);
431
432 priv->gpio_base = gpio_base;
433 priv->reg_base = reg_base;
434 priv->pdev = pdev;
435
Gregory Fong19a7b692015-07-31 18:17:43 -0700436 if (of_property_read_bool(np, "interrupt-controller")) {
437 priv->parent_irq = platform_get_irq(pdev, 0);
438 if (priv->parent_irq <= 0) {
439 dev_err(dev, "Couldn't get IRQ");
440 return -ENOENT;
441 }
442 } else {
443 priv->parent_irq = -ENOENT;
444 }
445
Gregory Fong3b0213d2015-05-28 19:14:05 -0700446 if (brcmstb_gpio_sanity_check_banks(dev, np, res))
447 return -EINVAL;
448
Florian Fainellice5a7e82016-01-06 10:55:22 -0800449 /*
450 * MIPS endianness is configured by boot strap, which also reverses all
451 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
452 * endian I/O).
453 *
454 * Other architectures (e.g., ARM) either do not support big endian, or
455 * else leave I/O in little endian mode.
456 */
457#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
458 flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
459#endif
460
Gregory Fong3b0213d2015-05-28 19:14:05 -0700461 of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
462 bank_width) {
463 struct brcmstb_gpio_bank *bank;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700464 struct gpio_chip *gc;
465
466 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
467 if (!bank) {
468 err = -ENOMEM;
469 goto fail;
470 }
471
472 bank->parent_priv = priv;
Gregory Fong19a7b692015-07-31 18:17:43 -0700473 bank->id = num_banks;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700474 if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
475 dev_err(dev, "Invalid bank width %d\n", bank_width);
Axel Lin35b3fc882016-04-10 18:15:15 +0800476 err = -EINVAL;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700477 goto fail;
478 } else {
479 bank->width = bank_width;
480 }
481
482 /*
483 * Regs are 4 bytes wide, have data reg, no set/clear regs,
484 * and direction bits have 0 = output and 1 = input
485 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100486 gc = &bank->gc;
487 err = bgpio_init(gc, dev, 4,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700488 reg_base + GIO_DATA(bank->id),
489 NULL, NULL, NULL,
Florian Fainellice5a7e82016-01-06 10:55:22 -0800490 reg_base + GIO_IODIR(bank->id), flags);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700491 if (err) {
492 dev_err(dev, "bgpio_init() failed\n");
493 goto fail;
494 }
495
Gregory Fong3b0213d2015-05-28 19:14:05 -0700496 gc->of_node = np;
497 gc->owner = THIS_MODULE;
Rob Herring7eb6ce22017-07-18 16:43:03 -0500498 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node);
Arvind Yadavba3e2172017-09-21 10:44:13 +0530499 if (!gc->label) {
500 err = -ENOMEM;
501 goto fail;
502 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700503 gc->base = gpio_base;
504 gc->of_gpio_n_cells = 2;
505 gc->of_xlate = brcmstb_gpio_of_xlate;
506 /* not all ngpio lines are valid, will use bank width later */
507 gc->ngpio = MAX_GPIO_PER_BANK;
508
Gregory Fong3afa1292015-07-31 18:17:44 -0700509 /*
510 * Mask all interrupts by default, since wakeup interrupts may
511 * be retained from S5 cold boot
512 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100513 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
Gregory Fong3afa1292015-07-31 18:17:44 -0700514
Linus Walleij0f4630f2015-12-04 14:02:58 +0100515 err = gpiochip_add_data(gc, bank);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700516 if (err) {
517 dev_err(dev, "Could not add gpiochip for bank %d\n",
518 bank->id);
519 goto fail;
520 }
521 gpio_base += gc->ngpio;
Gregory Fong19a7b692015-07-31 18:17:43 -0700522
523 if (priv->parent_irq > 0) {
524 err = brcmstb_gpio_irq_setup(pdev, bank);
525 if (err)
526 goto fail;
527 }
528
Gregory Fong3b0213d2015-05-28 19:14:05 -0700529 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
530 gc->base, gc->ngpio, bank->width);
531
532 /* Everything looks good, so add bank to list */
533 list_add(&bank->node, &priv->bank_list);
534
Gregory Fong19a7b692015-07-31 18:17:43 -0700535 num_banks++;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700536 }
537
538 dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
Gregory Fong19a7b692015-07-31 18:17:43 -0700539 num_banks, priv->gpio_base, gpio_base - 1);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700540
Gregory Fong3b0213d2015-05-28 19:14:05 -0700541 return 0;
542
543fail:
544 (void) brcmstb_gpio_remove(pdev);
545 return err;
546}
547
548static const struct of_device_id brcmstb_gpio_of_match[] = {
549 { .compatible = "brcm,brcmstb-gpio" },
550 {},
551};
552
553MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
554
555static struct platform_driver brcmstb_gpio_driver = {
556 .driver = {
557 .name = "brcmstb-gpio",
558 .of_match_table = brcmstb_gpio_of_match,
559 },
560 .probe = brcmstb_gpio_probe,
561 .remove = brcmstb_gpio_remove,
562};
563module_platform_driver(brcmstb_gpio_driver);
564
565MODULE_AUTHOR("Gregory Fong");
566MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
567MODULE_LICENSE("GPL v2");