GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / gpio / gpio-brcmstb.c
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/irqdomain.h>
20 #include <linux/irqchip/chained_irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/reboot.h>
23
24 #define GIO_BANK_SIZE           0x20
25 #define GIO_ODEN(bank)          (((bank) * GIO_BANK_SIZE) + 0x00)
26 #define GIO_DATA(bank)          (((bank) * GIO_BANK_SIZE) + 0x04)
27 #define GIO_IODIR(bank)         (((bank) * GIO_BANK_SIZE) + 0x08)
28 #define GIO_EC(bank)            (((bank) * GIO_BANK_SIZE) + 0x0c)
29 #define GIO_EI(bank)            (((bank) * GIO_BANK_SIZE) + 0x10)
30 #define GIO_MASK(bank)          (((bank) * GIO_BANK_SIZE) + 0x14)
31 #define GIO_LEVEL(bank)         (((bank) * GIO_BANK_SIZE) + 0x18)
32 #define GIO_STAT(bank)          (((bank) * GIO_BANK_SIZE) + 0x1c)
33
34 struct brcmstb_gpio_bank {
35         struct list_head node;
36         int id;
37         struct gpio_chip gc;
38         struct brcmstb_gpio_priv *parent_priv;
39         u32 width;
40         struct irq_chip irq_chip;
41 };
42
43 struct brcmstb_gpio_priv {
44         struct list_head bank_list;
45         void __iomem *reg_base;
46         struct platform_device *pdev;
47         int parent_irq;
48         int gpio_base;
49         bool can_wake;
50         int parent_wake_irq;
51         struct notifier_block reboot_notifier;
52 };
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
59 static inline struct brcmstb_gpio_priv *
60 brcmstb_gpio_gc_to_priv(struct gpio_chip *gc)
61 {
62         struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
63         return bank->parent_priv;
64 }
65
66 static unsigned long
67 brcmstb_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
81 static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
82                 unsigned int offset, bool enable)
83 {
84         struct gpio_chip *gc = &bank->gc;
85         struct brcmstb_gpio_priv *priv = bank->parent_priv;
86         u32 mask = gc->pin2mask(gc, offset);
87         u32 imask;
88         unsigned long flags;
89
90         spin_lock_irqsave(&gc->bgpio_lock, flags);
91         imask = gc->read_reg(priv->reg_base + GIO_MASK(bank->id));
92         if (enable)
93                 imask |= mask;
94         else
95                 imask &= ~mask;
96         gc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask);
97         spin_unlock_irqrestore(&gc->bgpio_lock, flags);
98 }
99
100 /* -------------------- IRQ chip functions -------------------- */
101
102 static void brcmstb_gpio_irq_mask(struct irq_data *d)
103 {
104         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
105         struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
106
107         brcmstb_gpio_set_imask(bank, d->hwirq, false);
108 }
109
110 static void brcmstb_gpio_irq_unmask(struct irq_data *d)
111 {
112         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
113         struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
114
115         brcmstb_gpio_set_imask(bank, d->hwirq, true);
116 }
117
118 static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
119 {
120         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
121         struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
122         struct brcmstb_gpio_priv *priv = bank->parent_priv;
123         u32 mask = BIT(d->hwirq);
124         u32 edge_insensitive, iedge_insensitive;
125         u32 edge_config, iedge_config;
126         u32 level, ilevel;
127         unsigned long flags;
128
129         switch (type) {
130         case IRQ_TYPE_LEVEL_LOW:
131                 level = 0;
132                 edge_config = 0;
133                 edge_insensitive = 0;
134                 break;
135         case IRQ_TYPE_LEVEL_HIGH:
136                 level = mask;
137                 edge_config = 0;
138                 edge_insensitive = 0;
139                 break;
140         case IRQ_TYPE_EDGE_FALLING:
141                 level = 0;
142                 edge_config = 0;
143                 edge_insensitive = 0;
144                 break;
145         case IRQ_TYPE_EDGE_RISING:
146                 level = 0;
147                 edge_config = mask;
148                 edge_insensitive = 0;
149                 break;
150         case IRQ_TYPE_EDGE_BOTH:
151                 level = 0;
152                 edge_config = 0;  /* don't care, but want known value */
153                 edge_insensitive = mask;
154                 break;
155         default:
156                 return -EINVAL;
157         }
158
159         spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
160
161         iedge_config = bank->gc.read_reg(priv->reg_base +
162                         GIO_EC(bank->id)) & ~mask;
163         iedge_insensitive = bank->gc.read_reg(priv->reg_base +
164                         GIO_EI(bank->id)) & ~mask;
165         ilevel = bank->gc.read_reg(priv->reg_base +
166                         GIO_LEVEL(bank->id)) & ~mask;
167
168         bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id),
169                         iedge_config | edge_config);
170         bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id),
171                         iedge_insensitive | edge_insensitive);
172         bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
173                         ilevel | level);
174
175         spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
176         return 0;
177 }
178
179 static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
180                 unsigned int enable)
181 {
182         int ret = 0;
183
184         /*
185          * Only enable wake IRQ once for however many hwirqs can wake
186          * since they all use the same wake IRQ.  Mask will be set
187          * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
188          */
189         if (enable)
190                 ret = enable_irq_wake(priv->parent_wake_irq);
191         else
192                 ret = disable_irq_wake(priv->parent_wake_irq);
193         if (ret)
194                 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
195                                 enable ? "enable" : "disable");
196         return ret;
197 }
198
199 static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
200 {
201         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
202         struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
203
204         return brcmstb_gpio_priv_set_wake(priv, enable);
205 }
206
207 static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
208 {
209         struct brcmstb_gpio_priv *priv = data;
210
211         if (!priv || irq != priv->parent_wake_irq)
212                 return IRQ_NONE;
213         pm_wakeup_event(&priv->pdev->dev, 0);
214         return IRQ_HANDLED;
215 }
216
217 static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
218 {
219         struct brcmstb_gpio_priv *priv = bank->parent_priv;
220         struct irq_domain *irq_domain = bank->gc.irqdomain;
221         void __iomem *reg_base = priv->reg_base;
222         unsigned long status;
223
224         while ((status = brcmstb_gpio_get_active_irqs(bank))) {
225                 int bit;
226
227                 for_each_set_bit(bit, &status, 32) {
228                         u32 stat = bank->gc.read_reg(reg_base +
229                                                       GIO_STAT(bank->id));
230                         if (bit >= bank->width)
231                                 dev_warn(&priv->pdev->dev,
232                                          "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
233                                          bank->id, bit);
234                         bank->gc.write_reg(reg_base + GIO_STAT(bank->id),
235                                             stat | BIT(bit));
236                         generic_handle_irq(irq_find_mapping(irq_domain, bit));
237                 }
238         }
239 }
240
241 /* Each UPG GIO block has one IRQ for all banks */
242 static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
243 {
244         struct gpio_chip *gc = irq_desc_get_handler_data(desc);
245         struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
246         struct irq_chip *chip = irq_desc_get_chip(desc);
247         struct brcmstb_gpio_bank *bank;
248
249         /* Interrupts weren't properly cleared during probe */
250         BUG_ON(!priv || !chip);
251
252         chained_irq_enter(chip, desc);
253         list_for_each_entry(bank, &priv->bank_list, node)
254                 brcmstb_gpio_irq_bank_handler(bank);
255         chained_irq_exit(chip, desc);
256 }
257
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
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 brcmstb_gpio_bank *bank;
292         int ret = 0;
293
294         if (!priv) {
295                 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
296                 return -EFAULT;
297         }
298
299         /*
300          * You can lose return values below, but we report all errors, and it's
301          * more important to actually perform all of the steps.
302          */
303         list_for_each_entry(bank, &priv->bank_list, node)
304                 gpiochip_remove(&bank->gc);
305
306         if (priv->reboot_notifier.notifier_call) {
307                 ret = unregister_reboot_notifier(&priv->reboot_notifier);
308                 if (ret)
309                         dev_err(&pdev->dev,
310                                 "failed to unregister reboot notifier\n");
311         }
312         return ret;
313 }
314
315 static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
316                 const struct of_phandle_args *gpiospec, u32 *flags)
317 {
318         struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
319         struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
320         int offset;
321
322         if (gc->of_gpio_n_cells != 2) {
323                 WARN_ON(1);
324                 return -EINVAL;
325         }
326
327         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
328                 return -EINVAL;
329
330         offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
331         if (offset >= gc->ngpio || offset < 0)
332                 return -EINVAL;
333
334         if (unlikely(offset >= bank->width)) {
335                 dev_warn_ratelimited(&priv->pdev->dev,
336                         "Received request for invalid GPIO offset %d\n",
337                         gpiospec->args[0]);
338         }
339
340         if (flags)
341                 *flags = gpiospec->args[1];
342
343         return offset;
344 }
345
346 /* Before calling, must have bank->parent_irq set and gpiochip registered */
347 static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
348                 struct brcmstb_gpio_bank *bank)
349 {
350         struct brcmstb_gpio_priv *priv = bank->parent_priv;
351         struct device *dev = &pdev->dev;
352         struct device_node *np = dev->of_node;
353         int err;
354
355         bank->irq_chip.name = dev_name(dev);
356         bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
357         bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
358         bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
359
360         /* Ensures that all non-wakeup IRQs are disabled at suspend */
361         bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
362
363         if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->can_wake &&
364                         of_property_read_bool(np, "wakeup-source")) {
365                 priv->parent_wake_irq = platform_get_irq(pdev, 1);
366                 if (priv->parent_wake_irq < 0) {
367                         dev_warn(dev,
368                                 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
369                 } else {
370                         /*
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,
378                                         brcmstb_gpio_wake_irq_handler, 0,
379                                         "brcmstb-gpio-wake", priv);
380
381                         if (err < 0) {
382                                 dev_err(dev, "Couldn't request wake IRQ");
383                                 return err;
384                         }
385
386                         priv->reboot_notifier.notifier_call =
387                                 brcmstb_gpio_reboot;
388                         register_reboot_notifier(&priv->reboot_notifier);
389                         priv->can_wake = true;
390                 }
391         }
392
393         if (priv->can_wake)
394                 bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
395
396         err = gpiochip_irqchip_add(&bank->gc, &bank->irq_chip, 0,
397                                    handle_simple_irq, IRQ_TYPE_NONE);
398         if (err)
399                 return err;
400         gpiochip_set_chained_irqchip(&bank->gc, &bank->irq_chip,
401                         priv->parent_irq, brcmstb_gpio_irq_handler);
402
403         return 0;
404 }
405
406 static 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;
416         int num_banks = 0;
417         int err;
418         static int gpio_base;
419         unsigned long flags = 0;
420
421         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
422         if (!priv)
423                 return -ENOMEM;
424         platform_set_drvdata(pdev, priv);
425         INIT_LIST_HEAD(&priv->bank_list);
426
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
436         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
446         if (brcmstb_gpio_sanity_check_banks(dev, np, res))
447                 return -EINVAL;
448
449         /*
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
461         of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
462                         bank_width) {
463                 struct brcmstb_gpio_bank *bank;
464                 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;
473                 bank->id = num_banks;
474                 if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
475                         dev_err(dev, "Invalid bank width %d\n", bank_width);
476                         err = -EINVAL;
477                         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                  */
486                 gc = &bank->gc;
487                 err = bgpio_init(gc, dev, 4,
488                                 reg_base + GIO_DATA(bank->id),
489                                 NULL, NULL, NULL,
490                                 reg_base + GIO_IODIR(bank->id), flags);
491                 if (err) {
492                         dev_err(dev, "bgpio_init() failed\n");
493                         goto fail;
494                 }
495
496                 gc->of_node = np;
497                 gc->owner = THIS_MODULE;
498                 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node);
499                 gc->base = gpio_base;
500                 gc->of_gpio_n_cells = 2;
501                 gc->of_xlate = brcmstb_gpio_of_xlate;
502                 /* not all ngpio lines are valid, will use bank width later */
503                 gc->ngpio = MAX_GPIO_PER_BANK;
504
505                 /*
506                  * Mask all interrupts by default, since wakeup interrupts may
507                  * be retained from S5 cold boot
508                  */
509                 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
510
511                 err = gpiochip_add_data(gc, bank);
512                 if (err) {
513                         dev_err(dev, "Could not add gpiochip for bank %d\n",
514                                         bank->id);
515                         goto fail;
516                 }
517                 gpio_base += gc->ngpio;
518
519                 if (priv->parent_irq > 0) {
520                         err = brcmstb_gpio_irq_setup(pdev, bank);
521                         if (err)
522                                 goto fail;
523                 }
524
525                 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
526                         gc->base, gc->ngpio, bank->width);
527
528                 /* Everything looks good, so add bank to list */
529                 list_add(&bank->node, &priv->bank_list);
530
531                 num_banks++;
532         }
533
534         dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
535                         num_banks, priv->gpio_base, gpio_base - 1);
536
537         return 0;
538
539 fail:
540         (void) brcmstb_gpio_remove(pdev);
541         return err;
542 }
543
544 static const struct of_device_id brcmstb_gpio_of_match[] = {
545         { .compatible = "brcm,brcmstb-gpio" },
546         {},
547 };
548
549 MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
550
551 static struct platform_driver brcmstb_gpio_driver = {
552         .driver = {
553                 .name = "brcmstb-gpio",
554                 .of_match_table = brcmstb_gpio_of_match,
555         },
556         .probe = brcmstb_gpio_probe,
557         .remove = brcmstb_gpio_remove,
558 };
559 module_platform_driver(brcmstb_gpio_driver);
560
561 MODULE_AUTHOR("Gregory Fong");
562 MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
563 MODULE_LICENSE("GPL v2");