blob: 183863902f7fc97ebd2543389892f9e807956ac7 [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
Doug Berger2c218b92017-10-24 12:54:48 -0700117static void brcmstb_gpio_irq_ack(struct irq_data *d)
118{
119 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
120 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
121 struct brcmstb_gpio_priv *priv = bank->parent_priv;
122 u32 mask = BIT(d->hwirq);
123
124 gc->write_reg(priv->reg_base + GIO_STAT(bank->id), mask);
125}
126
Gregory Fong19a7b692015-07-31 18:17:43 -0700127static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
128{
129 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100130 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700131 struct brcmstb_gpio_priv *priv = bank->parent_priv;
132 u32 mask = BIT(d->hwirq);
133 u32 edge_insensitive, iedge_insensitive;
134 u32 edge_config, iedge_config;
135 u32 level, ilevel;
136 unsigned long flags;
137
138 switch (type) {
139 case IRQ_TYPE_LEVEL_LOW:
Doug Berger633007a2017-10-24 12:54:49 -0700140 level = mask;
Gregory Fong19a7b692015-07-31 18:17:43 -0700141 edge_config = 0;
142 edge_insensitive = 0;
143 break;
144 case IRQ_TYPE_LEVEL_HIGH:
145 level = mask;
Doug Berger633007a2017-10-24 12:54:49 -0700146 edge_config = mask;
Gregory Fong19a7b692015-07-31 18:17:43 -0700147 edge_insensitive = 0;
148 break;
149 case IRQ_TYPE_EDGE_FALLING:
150 level = 0;
151 edge_config = 0;
152 edge_insensitive = 0;
153 break;
154 case IRQ_TYPE_EDGE_RISING:
155 level = 0;
156 edge_config = mask;
157 edge_insensitive = 0;
158 break;
159 case IRQ_TYPE_EDGE_BOTH:
160 level = 0;
161 edge_config = 0; /* don't care, but want known value */
162 edge_insensitive = mask;
163 break;
164 default:
165 return -EINVAL;
166 }
167
Linus Walleij0f4630f2015-12-04 14:02:58 +0100168 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700169
Linus Walleij0f4630f2015-12-04 14:02:58 +0100170 iedge_config = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700171 GIO_EC(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100172 iedge_insensitive = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700173 GIO_EI(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100174 ilevel = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700175 GIO_LEVEL(bank->id)) & ~mask;
176
Linus Walleij0f4630f2015-12-04 14:02:58 +0100177 bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700178 iedge_config | edge_config);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100179 bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700180 iedge_insensitive | edge_insensitive);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100181 bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700182 ilevel | level);
183
Linus Walleij0f4630f2015-12-04 14:02:58 +0100184 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700185 return 0;
186}
187
Gregory Fong3afa1292015-07-31 18:17:44 -0700188static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
189 unsigned int enable)
Gregory Fong19a7b692015-07-31 18:17:43 -0700190{
Gregory Fong19a7b692015-07-31 18:17:43 -0700191 int ret = 0;
192
193 /*
194 * Only enable wake IRQ once for however many hwirqs can wake
195 * since they all use the same wake IRQ. Mask will be set
196 * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
197 */
198 if (enable)
199 ret = enable_irq_wake(priv->parent_wake_irq);
200 else
201 ret = disable_irq_wake(priv->parent_wake_irq);
202 if (ret)
203 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
204 enable ? "enable" : "disable");
205 return ret;
206}
207
Gregory Fong3afa1292015-07-31 18:17:44 -0700208static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
209{
210 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
211 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
212
213 return brcmstb_gpio_priv_set_wake(priv, enable);
214}
215
Gregory Fong19a7b692015-07-31 18:17:43 -0700216static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
217{
218 struct brcmstb_gpio_priv *priv = data;
219
220 if (!priv || irq != priv->parent_wake_irq)
221 return IRQ_NONE;
222 pm_wakeup_event(&priv->pdev->dev, 0);
223 return IRQ_HANDLED;
224}
225
226static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
227{
228 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100229 struct irq_domain *irq_domain = bank->gc.irqdomain;
Gregory Fong19a7b692015-07-31 18:17:43 -0700230 unsigned long status;
Gregory Fong19a7b692015-07-31 18:17:43 -0700231
Doug Berger142c1682017-10-24 12:54:47 -0700232 while ((status = brcmstb_gpio_get_active_irqs(bank))) {
Gregory Fong19a7b692015-07-31 18:17:43 -0700233 int bit;
234
235 for_each_set_bit(bit, &status, 32) {
Gregory Fong19a7b692015-07-31 18:17:43 -0700236 if (bit >= bank->width)
237 dev_warn(&priv->pdev->dev,
238 "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
239 bank->id, bit);
Gregory Fong19a7b692015-07-31 18:17:43 -0700240 generic_handle_irq(irq_find_mapping(irq_domain, bit));
241 }
242 }
Gregory Fong19a7b692015-07-31 18:17:43 -0700243}
244
245/* Each UPG GIO block has one IRQ for all banks */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200246static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
Gregory Fong19a7b692015-07-31 18:17:43 -0700247{
248 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
249 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
250 struct irq_chip *chip = irq_desc_get_chip(desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800251 struct brcmstb_gpio_bank *bank;
Gregory Fong19a7b692015-07-31 18:17:43 -0700252
253 /* Interrupts weren't properly cleared during probe */
254 BUG_ON(!priv || !chip);
255
256 chained_irq_enter(chip, desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800257 list_for_each_entry(bank, &priv->bank_list, node)
Gregory Fong19a7b692015-07-31 18:17:43 -0700258 brcmstb_gpio_irq_bank_handler(bank);
Gregory Fong19a7b692015-07-31 18:17:43 -0700259 chained_irq_exit(chip, desc);
260}
261
Gregory Fong3afa1292015-07-31 18:17:44 -0700262static int brcmstb_gpio_reboot(struct notifier_block *nb,
263 unsigned long action, void *data)
264{
265 struct brcmstb_gpio_priv *priv =
266 container_of(nb, struct brcmstb_gpio_priv, reboot_notifier);
267
268 /* Enable GPIO for S5 cold boot */
269 if (action == SYS_POWER_OFF)
270 brcmstb_gpio_priv_set_wake(priv, 1);
271
272 return NOTIFY_DONE;
273}
274
Gregory Fong3b0213d2015-05-28 19:14:05 -0700275/* Make sure that the number of banks matches up between properties */
276static int brcmstb_gpio_sanity_check_banks(struct device *dev,
277 struct device_node *np, struct resource *res)
278{
279 int res_num_banks = resource_size(res) / GIO_BANK_SIZE;
280 int num_banks =
281 of_property_count_u32_elems(np, "brcm,gpio-bank-widths");
282
283 if (res_num_banks != num_banks) {
284 dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n",
285 res_num_banks, num_banks);
286 return -EINVAL;
287 } else {
288 return 0;
289 }
290}
291
292static int brcmstb_gpio_remove(struct platform_device *pdev)
293{
294 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700295 struct brcmstb_gpio_bank *bank;
296 int ret = 0;
297
Gregory Fong22526072015-06-17 18:00:40 -0700298 if (!priv) {
299 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
300 return -EFAULT;
301 }
302
303 /*
304 * You can lose return values below, but we report all errors, and it's
305 * more important to actually perform all of the steps.
306 */
Axel Linb178e7e2016-02-20 09:50:37 +0800307 list_for_each_entry(bank, &priv->bank_list, node)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100308 gpiochip_remove(&bank->gc);
Axel Linb178e7e2016-02-20 09:50:37 +0800309
Gregory Fong3afa1292015-07-31 18:17:44 -0700310 if (priv->reboot_notifier.notifier_call) {
311 ret = unregister_reboot_notifier(&priv->reboot_notifier);
312 if (ret)
313 dev_err(&pdev->dev,
314 "failed to unregister reboot notifier\n");
315 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700316 return ret;
317}
318
319static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
320 const struct of_phandle_args *gpiospec, u32 *flags)
321{
322 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100323 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700324 int offset;
325
326 if (gc->of_gpio_n_cells != 2) {
327 WARN_ON(1);
328 return -EINVAL;
329 }
330
331 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
332 return -EINVAL;
333
334 offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
Gregory Fong19a7b692015-07-31 18:17:43 -0700335 if (offset >= gc->ngpio || offset < 0)
Gregory Fong3b0213d2015-05-28 19:14:05 -0700336 return -EINVAL;
337
338 if (unlikely(offset >= bank->width)) {
339 dev_warn_ratelimited(&priv->pdev->dev,
340 "Received request for invalid GPIO offset %d\n",
341 gpiospec->args[0]);
342 }
343
344 if (flags)
345 *flags = gpiospec->args[1];
346
347 return offset;
348}
349
Gregory Fong19a7b692015-07-31 18:17:43 -0700350/* Before calling, must have bank->parent_irq set and gpiochip registered */
351static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
352 struct brcmstb_gpio_bank *bank)
353{
354 struct brcmstb_gpio_priv *priv = bank->parent_priv;
355 struct device *dev = &pdev->dev;
356 struct device_node *np = dev->of_node;
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900357 int err;
Gregory Fong19a7b692015-07-31 18:17:43 -0700358
359 bank->irq_chip.name = dev_name(dev);
360 bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
361 bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
Doug Berger2c218b92017-10-24 12:54:48 -0700362 bank->irq_chip.irq_ack = brcmstb_gpio_irq_ack;
Gregory Fong19a7b692015-07-31 18:17:43 -0700363 bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
364
365 /* Ensures that all non-wakeup IRQs are disabled at suspend */
366 bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
367
Doug Berger0752df62017-10-24 12:54:46 -0700368 if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->parent_wake_irq &&
Gregory Fong19a7b692015-07-31 18:17:43 -0700369 of_property_read_bool(np, "wakeup-source")) {
370 priv->parent_wake_irq = platform_get_irq(pdev, 1);
371 if (priv->parent_wake_irq < 0) {
Doug Berger0752df62017-10-24 12:54:46 -0700372 priv->parent_wake_irq = 0;
Gregory Fong19a7b692015-07-31 18:17:43 -0700373 dev_warn(dev,
374 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
375 } else {
Gregory Fong3afa1292015-07-31 18:17:44 -0700376 /*
377 * Set wakeup capability before requesting wakeup
378 * interrupt, so we can process boot-time "wakeups"
379 * (e.g., from S5 cold boot)
380 */
381 device_set_wakeup_capable(dev, true);
382 device_wakeup_enable(dev);
383 err = devm_request_irq(dev, priv->parent_wake_irq,
Doug Berger0752df62017-10-24 12:54:46 -0700384 brcmstb_gpio_wake_irq_handler,
385 IRQF_SHARED,
386 "brcmstb-gpio-wake", priv);
Gregory Fong19a7b692015-07-31 18:17:43 -0700387
388 if (err < 0) {
389 dev_err(dev, "Couldn't request wake IRQ");
390 return err;
391 }
392
Gregory Fong3afa1292015-07-31 18:17:44 -0700393 priv->reboot_notifier.notifier_call =
394 brcmstb_gpio_reboot;
395 register_reboot_notifier(&priv->reboot_notifier);
Gregory Fong19a7b692015-07-31 18:17:43 -0700396 }
397 }
398
Doug Berger0752df62017-10-24 12:54:46 -0700399 if (priv->parent_wake_irq)
Gregory Fong19a7b692015-07-31 18:17:43 -0700400 bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
401
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900402 err = gpiochip_irqchip_add(&bank->gc, &bank->irq_chip, 0,
Doug Berger2c218b92017-10-24 12:54:48 -0700403 handle_level_irq, IRQ_TYPE_NONE);
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900404 if (err)
405 return err;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100406 gpiochip_set_chained_irqchip(&bank->gc, &bank->irq_chip,
Gregory Fong19a7b692015-07-31 18:17:43 -0700407 priv->parent_irq, brcmstb_gpio_irq_handler);
408
409 return 0;
410}
411
Gregory Fong3b0213d2015-05-28 19:14:05 -0700412static int brcmstb_gpio_probe(struct platform_device *pdev)
413{
414 struct device *dev = &pdev->dev;
415 struct device_node *np = dev->of_node;
416 void __iomem *reg_base;
417 struct brcmstb_gpio_priv *priv;
418 struct resource *res;
419 struct property *prop;
420 const __be32 *p;
421 u32 bank_width;
Gregory Fong19a7b692015-07-31 18:17:43 -0700422 int num_banks = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700423 int err;
424 static int gpio_base;
Florian Fainellice5a7e82016-01-06 10:55:22 -0800425 unsigned long flags = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700426
427 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
428 if (!priv)
429 return -ENOMEM;
Gregory Fong22526072015-06-17 18:00:40 -0700430 platform_set_drvdata(pdev, priv);
431 INIT_LIST_HEAD(&priv->bank_list);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700432
433 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434 reg_base = devm_ioremap_resource(dev, res);
435 if (IS_ERR(reg_base))
436 return PTR_ERR(reg_base);
437
438 priv->gpio_base = gpio_base;
439 priv->reg_base = reg_base;
440 priv->pdev = pdev;
441
Gregory Fong19a7b692015-07-31 18:17:43 -0700442 if (of_property_read_bool(np, "interrupt-controller")) {
443 priv->parent_irq = platform_get_irq(pdev, 0);
444 if (priv->parent_irq <= 0) {
445 dev_err(dev, "Couldn't get IRQ");
446 return -ENOENT;
447 }
448 } else {
449 priv->parent_irq = -ENOENT;
450 }
451
Gregory Fong3b0213d2015-05-28 19:14:05 -0700452 if (brcmstb_gpio_sanity_check_banks(dev, np, res))
453 return -EINVAL;
454
Florian Fainellice5a7e82016-01-06 10:55:22 -0800455 /*
456 * MIPS endianness is configured by boot strap, which also reverses all
457 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
458 * endian I/O).
459 *
460 * Other architectures (e.g., ARM) either do not support big endian, or
461 * else leave I/O in little endian mode.
462 */
463#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
464 flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
465#endif
466
Gregory Fong3b0213d2015-05-28 19:14:05 -0700467 of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
468 bank_width) {
469 struct brcmstb_gpio_bank *bank;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700470 struct gpio_chip *gc;
471
472 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
473 if (!bank) {
474 err = -ENOMEM;
475 goto fail;
476 }
477
478 bank->parent_priv = priv;
Gregory Fong19a7b692015-07-31 18:17:43 -0700479 bank->id = num_banks;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700480 if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
481 dev_err(dev, "Invalid bank width %d\n", bank_width);
Axel Lin35b3fc882016-04-10 18:15:15 +0800482 err = -EINVAL;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700483 goto fail;
484 } else {
485 bank->width = bank_width;
486 }
487
488 /*
489 * Regs are 4 bytes wide, have data reg, no set/clear regs,
490 * and direction bits have 0 = output and 1 = input
491 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100492 gc = &bank->gc;
493 err = bgpio_init(gc, dev, 4,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700494 reg_base + GIO_DATA(bank->id),
495 NULL, NULL, NULL,
Florian Fainellice5a7e82016-01-06 10:55:22 -0800496 reg_base + GIO_IODIR(bank->id), flags);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700497 if (err) {
498 dev_err(dev, "bgpio_init() failed\n");
499 goto fail;
500 }
501
Gregory Fong3b0213d2015-05-28 19:14:05 -0700502 gc->of_node = np;
503 gc->owner = THIS_MODULE;
Rob Herring7eb6ce22017-07-18 16:43:03 -0500504 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node);
Arvind Yadavba3e2172017-09-21 10:44:13 +0530505 if (!gc->label) {
506 err = -ENOMEM;
507 goto fail;
508 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700509 gc->base = gpio_base;
510 gc->of_gpio_n_cells = 2;
511 gc->of_xlate = brcmstb_gpio_of_xlate;
512 /* not all ngpio lines are valid, will use bank width later */
513 gc->ngpio = MAX_GPIO_PER_BANK;
514
Gregory Fong3afa1292015-07-31 18:17:44 -0700515 /*
516 * Mask all interrupts by default, since wakeup interrupts may
517 * be retained from S5 cold boot
518 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100519 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
Gregory Fong3afa1292015-07-31 18:17:44 -0700520
Linus Walleij0f4630f2015-12-04 14:02:58 +0100521 err = gpiochip_add_data(gc, bank);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700522 if (err) {
523 dev_err(dev, "Could not add gpiochip for bank %d\n",
524 bank->id);
525 goto fail;
526 }
527 gpio_base += gc->ngpio;
Gregory Fong19a7b692015-07-31 18:17:43 -0700528
529 if (priv->parent_irq > 0) {
530 err = brcmstb_gpio_irq_setup(pdev, bank);
531 if (err)
532 goto fail;
533 }
534
Gregory Fong3b0213d2015-05-28 19:14:05 -0700535 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
536 gc->base, gc->ngpio, bank->width);
537
538 /* Everything looks good, so add bank to list */
539 list_add(&bank->node, &priv->bank_list);
540
Gregory Fong19a7b692015-07-31 18:17:43 -0700541 num_banks++;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700542 }
543
544 dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
Gregory Fong19a7b692015-07-31 18:17:43 -0700545 num_banks, priv->gpio_base, gpio_base - 1);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700546
Gregory Fong3b0213d2015-05-28 19:14:05 -0700547 return 0;
548
549fail:
550 (void) brcmstb_gpio_remove(pdev);
551 return err;
552}
553
554static const struct of_device_id brcmstb_gpio_of_match[] = {
555 { .compatible = "brcm,brcmstb-gpio" },
556 {},
557};
558
559MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
560
561static struct platform_driver brcmstb_gpio_driver = {
562 .driver = {
563 .name = "brcmstb-gpio",
564 .of_match_table = brcmstb_gpio_of_match,
565 },
566 .probe = brcmstb_gpio_probe,
567 .remove = brcmstb_gpio_remove,
568};
569module_platform_driver(brcmstb_gpio_driver);
570
571MODULE_AUTHOR("Gregory Fong");
572MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
573MODULE_LICENSE("GPL v2");