blob: 74ef8924886734655def6864dbb96a8d07e7cc07 [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 Fong3b0213d2015-05-28 19:14:05 -070022
Doug Berger47142212017-10-24 12:54:51 -070023enum gio_reg_index {
24 GIO_REG_ODEN = 0,
25 GIO_REG_DATA,
26 GIO_REG_IODIR,
27 GIO_REG_EC,
28 GIO_REG_EI,
29 GIO_REG_MASK,
30 GIO_REG_LEVEL,
31 GIO_REG_STAT,
32 NUMBER_OF_GIO_REGISTERS
33};
34
35#define GIO_BANK_SIZE (NUMBER_OF_GIO_REGISTERS * sizeof(u32))
36#define GIO_BANK_OFF(bank, off) (((bank) * GIO_BANK_SIZE) + (off * sizeof(u32)))
37#define GIO_ODEN(bank) GIO_BANK_OFF(bank, GIO_REG_ODEN)
38#define GIO_DATA(bank) GIO_BANK_OFF(bank, GIO_REG_DATA)
39#define GIO_IODIR(bank) GIO_BANK_OFF(bank, GIO_REG_IODIR)
40#define GIO_EC(bank) GIO_BANK_OFF(bank, GIO_REG_EC)
41#define GIO_EI(bank) GIO_BANK_OFF(bank, GIO_REG_EI)
42#define GIO_MASK(bank) GIO_BANK_OFF(bank, GIO_REG_MASK)
43#define GIO_LEVEL(bank) GIO_BANK_OFF(bank, GIO_REG_LEVEL)
44#define GIO_STAT(bank) GIO_BANK_OFF(bank, GIO_REG_STAT)
Gregory Fong3b0213d2015-05-28 19:14:05 -070045
46struct brcmstb_gpio_bank {
47 struct list_head node;
48 int id;
Linus Walleij0f4630f2015-12-04 14:02:58 +010049 struct gpio_chip gc;
Gregory Fong3b0213d2015-05-28 19:14:05 -070050 struct brcmstb_gpio_priv *parent_priv;
51 u32 width;
Doug Berger47142212017-10-24 12:54:51 -070052 u32 wake_active;
53 u32 saved_regs[GIO_REG_STAT]; /* Don't save and restore GIO_REG_STAT */
Gregory Fong3b0213d2015-05-28 19:14:05 -070054};
55
56struct brcmstb_gpio_priv {
57 struct list_head bank_list;
58 void __iomem *reg_base;
Gregory Fong3b0213d2015-05-28 19:14:05 -070059 struct platform_device *pdev;
Doug Berger0ba31dc2017-10-24 12:54:50 -070060 struct irq_domain *irq_domain;
61 struct irq_chip irq_chip;
Gregory Fong19a7b692015-07-31 18:17:43 -070062 int parent_irq;
Gregory Fong3b0213d2015-05-28 19:14:05 -070063 int gpio_base;
Doug Berger0ba31dc2017-10-24 12:54:50 -070064 int num_gpios;
Gregory Fong19a7b692015-07-31 18:17:43 -070065 int parent_wake_irq;
Gregory Fong3b0213d2015-05-28 19:14:05 -070066};
67
Doug Berger0ba31dc2017-10-24 12:54:50 -070068#define MAX_GPIO_PER_BANK 32
Gregory Fong3b0213d2015-05-28 19:14:05 -070069#define GPIO_BANK(gpio) ((gpio) >> 5)
70/* assumes MAX_GPIO_PER_BANK is a multiple of 2 */
71#define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1))
72
Gregory Fong3b0213d2015-05-28 19:14:05 -070073static inline struct brcmstb_gpio_priv *
74brcmstb_gpio_gc_to_priv(struct gpio_chip *gc)
75{
Linus Walleij0f4630f2015-12-04 14:02:58 +010076 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -070077 return bank->parent_priv;
78}
79
Doug Berger142c1682017-10-24 12:54:47 -070080static unsigned long
Doug Berger47142212017-10-24 12:54:51 -070081__brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank)
Doug Berger142c1682017-10-24 12:54:47 -070082{
83 void __iomem *reg_base = bank->parent_priv->reg_base;
Doug Berger47142212017-10-24 12:54:51 -070084
85 return bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) &
86 bank->gc.read_reg(reg_base + GIO_MASK(bank->id));
87}
88
89static unsigned long
90brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank)
91{
Doug Berger142c1682017-10-24 12:54:47 -070092 unsigned long status;
93 unsigned long flags;
94
95 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
Doug Berger47142212017-10-24 12:54:51 -070096 status = __brcmstb_gpio_get_active_irqs(bank);
Doug Berger142c1682017-10-24 12:54:47 -070097 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
98
99 return status;
100}
101
Doug Berger0ba31dc2017-10-24 12:54:50 -0700102static int brcmstb_gpio_hwirq_to_offset(irq_hw_number_t hwirq,
103 struct brcmstb_gpio_bank *bank)
104{
105 return hwirq - (bank->gc.base - bank->parent_priv->gpio_base);
106}
107
Gregory Fong19a7b692015-07-31 18:17:43 -0700108static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
Doug Berger0ba31dc2017-10-24 12:54:50 -0700109 unsigned int hwirq, bool enable)
Gregory Fong19a7b692015-07-31 18:17:43 -0700110{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100111 struct gpio_chip *gc = &bank->gc;
Gregory Fong19a7b692015-07-31 18:17:43 -0700112 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700113 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(hwirq, bank));
Gregory Fong19a7b692015-07-31 18:17:43 -0700114 u32 imask;
115 unsigned long flags;
116
Linus Walleij0f4630f2015-12-04 14:02:58 +0100117 spin_lock_irqsave(&gc->bgpio_lock, flags);
118 imask = gc->read_reg(priv->reg_base + GIO_MASK(bank->id));
Gregory Fong19a7b692015-07-31 18:17:43 -0700119 if (enable)
Doug Berger0ba31dc2017-10-24 12:54:50 -0700120 imask |= mask;
Gregory Fong19a7b692015-07-31 18:17:43 -0700121 else
Doug Berger0ba31dc2017-10-24 12:54:50 -0700122 imask &= ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100123 gc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask);
124 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700125}
126
Doug Berger0ba31dc2017-10-24 12:54:50 -0700127static int brcmstb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
128{
129 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
130 /* gc_offset is relative to this gpio_chip; want real offset */
131 int hwirq = offset + (gc->base - priv->gpio_base);
132
133 if (hwirq >= priv->num_gpios)
134 return -ENXIO;
135 return irq_create_mapping(priv->irq_domain, hwirq);
136}
137
Gregory Fong19a7b692015-07-31 18:17:43 -0700138/* -------------------- IRQ chip functions -------------------- */
139
140static void brcmstb_gpio_irq_mask(struct irq_data *d)
141{
142 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100143 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700144
145 brcmstb_gpio_set_imask(bank, d->hwirq, false);
146}
147
148static void brcmstb_gpio_irq_unmask(struct irq_data *d)
149{
150 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100151 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700152
153 brcmstb_gpio_set_imask(bank, d->hwirq, true);
154}
155
Doug Berger2c218b92017-10-24 12:54:48 -0700156static void brcmstb_gpio_irq_ack(struct irq_data *d)
157{
158 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
159 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
160 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700161 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
Doug Berger2c218b92017-10-24 12:54:48 -0700162
163 gc->write_reg(priv->reg_base + GIO_STAT(bank->id), mask);
164}
165
Gregory Fong19a7b692015-07-31 18:17:43 -0700166static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
167{
168 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100169 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700170 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700171 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
Gregory Fong19a7b692015-07-31 18:17:43 -0700172 u32 edge_insensitive, iedge_insensitive;
173 u32 edge_config, iedge_config;
174 u32 level, ilevel;
175 unsigned long flags;
176
177 switch (type) {
178 case IRQ_TYPE_LEVEL_LOW:
Doug Berger633007a2017-10-24 12:54:49 -0700179 level = mask;
Gregory Fong19a7b692015-07-31 18:17:43 -0700180 edge_config = 0;
181 edge_insensitive = 0;
182 break;
183 case IRQ_TYPE_LEVEL_HIGH:
184 level = mask;
Doug Berger633007a2017-10-24 12:54:49 -0700185 edge_config = mask;
Gregory Fong19a7b692015-07-31 18:17:43 -0700186 edge_insensitive = 0;
187 break;
188 case IRQ_TYPE_EDGE_FALLING:
189 level = 0;
190 edge_config = 0;
191 edge_insensitive = 0;
192 break;
193 case IRQ_TYPE_EDGE_RISING:
194 level = 0;
195 edge_config = mask;
196 edge_insensitive = 0;
197 break;
198 case IRQ_TYPE_EDGE_BOTH:
199 level = 0;
200 edge_config = 0; /* don't care, but want known value */
201 edge_insensitive = mask;
202 break;
203 default:
204 return -EINVAL;
205 }
206
Linus Walleij0f4630f2015-12-04 14:02:58 +0100207 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700208
Linus Walleij0f4630f2015-12-04 14:02:58 +0100209 iedge_config = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700210 GIO_EC(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100211 iedge_insensitive = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700212 GIO_EI(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100213 ilevel = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700214 GIO_LEVEL(bank->id)) & ~mask;
215
Linus Walleij0f4630f2015-12-04 14:02:58 +0100216 bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700217 iedge_config | edge_config);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100218 bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700219 iedge_insensitive | edge_insensitive);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100220 bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700221 ilevel | level);
222
Linus Walleij0f4630f2015-12-04 14:02:58 +0100223 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700224 return 0;
225}
226
Gregory Fong3afa1292015-07-31 18:17:44 -0700227static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
228 unsigned int enable)
Gregory Fong19a7b692015-07-31 18:17:43 -0700229{
Gregory Fong19a7b692015-07-31 18:17:43 -0700230 int ret = 0;
231
Gregory Fong19a7b692015-07-31 18:17:43 -0700232 if (enable)
233 ret = enable_irq_wake(priv->parent_wake_irq);
234 else
235 ret = disable_irq_wake(priv->parent_wake_irq);
236 if (ret)
237 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
238 enable ? "enable" : "disable");
239 return ret;
240}
241
Gregory Fong3afa1292015-07-31 18:17:44 -0700242static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
243{
244 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Doug Berger47142212017-10-24 12:54:51 -0700245 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
246 struct brcmstb_gpio_priv *priv = bank->parent_priv;
247 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
248
249 /*
250 * Do not do anything specific for now, suspend/resume callbacks will
251 * configure the interrupt mask appropriately
252 */
253 if (enable)
254 bank->wake_active |= mask;
255 else
256 bank->wake_active &= ~mask;
Gregory Fong3afa1292015-07-31 18:17:44 -0700257
258 return brcmstb_gpio_priv_set_wake(priv, enable);
259}
260
Gregory Fong19a7b692015-07-31 18:17:43 -0700261static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
262{
263 struct brcmstb_gpio_priv *priv = data;
264
265 if (!priv || irq != priv->parent_wake_irq)
266 return IRQ_NONE;
Doug Berger47142212017-10-24 12:54:51 -0700267
268 /* Nothing to do */
Gregory Fong19a7b692015-07-31 18:17:43 -0700269 return IRQ_HANDLED;
270}
271
272static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
273{
274 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700275 struct irq_domain *domain = priv->irq_domain;
276 int hwbase = bank->gc.base - priv->gpio_base;
Gregory Fong19a7b692015-07-31 18:17:43 -0700277 unsigned long status;
Gregory Fong19a7b692015-07-31 18:17:43 -0700278
Doug Berger142c1682017-10-24 12:54:47 -0700279 while ((status = brcmstb_gpio_get_active_irqs(bank))) {
Marc Zyngierdbd1c542021-05-04 17:42:18 +0100280 unsigned int offset;
Gregory Fong19a7b692015-07-31 18:17:43 -0700281
Doug Berger0ba31dc2017-10-24 12:54:50 -0700282 for_each_set_bit(offset, &status, 32) {
283 if (offset >= bank->width)
Gregory Fong19a7b692015-07-31 18:17:43 -0700284 dev_warn(&priv->pdev->dev,
285 "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
Doug Berger0ba31dc2017-10-24 12:54:50 -0700286 bank->id, offset);
Marc Zyngierdbd1c542021-05-04 17:42:18 +0100287 generic_handle_domain_irq(domain, hwbase + offset);
Gregory Fong19a7b692015-07-31 18:17:43 -0700288 }
289 }
Gregory Fong19a7b692015-07-31 18:17:43 -0700290}
291
292/* Each UPG GIO block has one IRQ for all banks */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200293static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
Gregory Fong19a7b692015-07-31 18:17:43 -0700294{
Doug Berger0ba31dc2017-10-24 12:54:50 -0700295 struct brcmstb_gpio_priv *priv = irq_desc_get_handler_data(desc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700296 struct irq_chip *chip = irq_desc_get_chip(desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800297 struct brcmstb_gpio_bank *bank;
Gregory Fong19a7b692015-07-31 18:17:43 -0700298
299 /* Interrupts weren't properly cleared during probe */
300 BUG_ON(!priv || !chip);
301
302 chained_irq_enter(chip, desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800303 list_for_each_entry(bank, &priv->bank_list, node)
Gregory Fong19a7b692015-07-31 18:17:43 -0700304 brcmstb_gpio_irq_bank_handler(bank);
Gregory Fong19a7b692015-07-31 18:17:43 -0700305 chained_irq_exit(chip, desc);
306}
307
Doug Berger0ba31dc2017-10-24 12:54:50 -0700308static struct brcmstb_gpio_bank *brcmstb_gpio_hwirq_to_bank(
309 struct brcmstb_gpio_priv *priv, irq_hw_number_t hwirq)
310{
311 struct brcmstb_gpio_bank *bank;
312 int i = 0;
313
314 /* banks are in descending order */
315 list_for_each_entry_reverse(bank, &priv->bank_list, node) {
316 i += bank->gc.ngpio;
317 if (hwirq < i)
318 return bank;
319 }
320 return NULL;
321}
322
323/*
324 * This lock class tells lockdep that GPIO irqs are in a different
325 * category than their parents, so it won't report false recursion.
326 */
327static struct lock_class_key brcmstb_gpio_irq_lock_class;
Andrew Lunn39c3fd52017-12-02 18:11:04 +0100328static struct lock_class_key brcmstb_gpio_irq_request_class;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700329
330
331static int brcmstb_gpio_irq_map(struct irq_domain *d, unsigned int irq,
332 irq_hw_number_t hwirq)
333{
334 struct brcmstb_gpio_priv *priv = d->host_data;
335 struct brcmstb_gpio_bank *bank =
336 brcmstb_gpio_hwirq_to_bank(priv, hwirq);
337 struct platform_device *pdev = priv->pdev;
338 int ret;
339
340 if (!bank)
341 return -EINVAL;
342
343 dev_dbg(&pdev->dev, "Mapping irq %d for gpio line %d (bank %d)\n",
344 irq, (int)hwirq, bank->id);
345 ret = irq_set_chip_data(irq, &bank->gc);
346 if (ret < 0)
347 return ret;
Andrew Lunn39c3fd52017-12-02 18:11:04 +0100348 irq_set_lockdep_class(irq, &brcmstb_gpio_irq_lock_class,
Thomas Gleixner8880c132017-12-29 16:29:15 +0100349 &brcmstb_gpio_irq_request_class);
Doug Berger0ba31dc2017-10-24 12:54:50 -0700350 irq_set_chip_and_handler(irq, &priv->irq_chip, handle_level_irq);
351 irq_set_noprobe(irq);
352 return 0;
353}
354
355static void brcmstb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
356{
357 irq_set_chip_and_handler(irq, NULL, NULL);
358 irq_set_chip_data(irq, NULL);
359}
360
361static const struct irq_domain_ops brcmstb_gpio_irq_domain_ops = {
362 .map = brcmstb_gpio_irq_map,
363 .unmap = brcmstb_gpio_irq_unmap,
364 .xlate = irq_domain_xlate_twocell,
365};
366
Gregory Fong3b0213d2015-05-28 19:14:05 -0700367/* Make sure that the number of banks matches up between properties */
368static int brcmstb_gpio_sanity_check_banks(struct device *dev,
369 struct device_node *np, struct resource *res)
370{
371 int res_num_banks = resource_size(res) / GIO_BANK_SIZE;
372 int num_banks =
373 of_property_count_u32_elems(np, "brcm,gpio-bank-widths");
374
375 if (res_num_banks != num_banks) {
376 dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n",
377 res_num_banks, num_banks);
378 return -EINVAL;
379 } else {
380 return 0;
381 }
382}
383
384static int brcmstb_gpio_remove(struct platform_device *pdev)
385{
386 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700387 struct brcmstb_gpio_bank *bank;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700388 int offset, ret = 0, virq;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700389
Gregory Fong22526072015-06-17 18:00:40 -0700390 if (!priv) {
391 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
392 return -EFAULT;
393 }
394
Doug Berger0ba31dc2017-10-24 12:54:50 -0700395 if (priv->parent_irq > 0)
396 irq_set_chained_handler_and_data(priv->parent_irq, NULL, NULL);
397
398 /* Remove all IRQ mappings and delete the domain */
399 if (priv->irq_domain) {
400 for (offset = 0; offset < priv->num_gpios; offset++) {
401 virq = irq_find_mapping(priv->irq_domain, offset);
402 irq_dispose_mapping(virq);
403 }
404 irq_domain_remove(priv->irq_domain);
405 }
406
Gregory Fong22526072015-06-17 18:00:40 -0700407 /*
408 * You can lose return values below, but we report all errors, and it's
409 * more important to actually perform all of the steps.
410 */
Axel Linb178e7e2016-02-20 09:50:37 +0800411 list_for_each_entry(bank, &priv->bank_list, node)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100412 gpiochip_remove(&bank->gc);
Axel Linb178e7e2016-02-20 09:50:37 +0800413
Gregory Fong3b0213d2015-05-28 19:14:05 -0700414 return ret;
415}
416
417static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
418 const struct of_phandle_args *gpiospec, u32 *flags)
419{
420 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100421 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700422 int offset;
423
424 if (gc->of_gpio_n_cells != 2) {
425 WARN_ON(1);
426 return -EINVAL;
427 }
428
429 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
430 return -EINVAL;
431
432 offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
Gregory Fong19a7b692015-07-31 18:17:43 -0700433 if (offset >= gc->ngpio || offset < 0)
Gregory Fong3b0213d2015-05-28 19:14:05 -0700434 return -EINVAL;
435
436 if (unlikely(offset >= bank->width)) {
437 dev_warn_ratelimited(&priv->pdev->dev,
438 "Received request for invalid GPIO offset %d\n",
439 gpiospec->args[0]);
440 }
441
442 if (flags)
443 *flags = gpiospec->args[1];
444
445 return offset;
446}
447
Doug Berger0ba31dc2017-10-24 12:54:50 -0700448/* priv->parent_irq and priv->num_gpios must be set before calling */
Gregory Fong19a7b692015-07-31 18:17:43 -0700449static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
Doug Berger0ba31dc2017-10-24 12:54:50 -0700450 struct brcmstb_gpio_priv *priv)
Gregory Fong19a7b692015-07-31 18:17:43 -0700451{
Gregory Fong19a7b692015-07-31 18:17:43 -0700452 struct device *dev = &pdev->dev;
453 struct device_node *np = dev->of_node;
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900454 int err;
Gregory Fong19a7b692015-07-31 18:17:43 -0700455
Doug Berger0ba31dc2017-10-24 12:54:50 -0700456 priv->irq_domain =
457 irq_domain_add_linear(np, priv->num_gpios,
458 &brcmstb_gpio_irq_domain_ops,
459 priv);
460 if (!priv->irq_domain) {
461 dev_err(dev, "Couldn't allocate IRQ domain\n");
462 return -ENXIO;
463 }
Gregory Fong19a7b692015-07-31 18:17:43 -0700464
Doug Berger0ba31dc2017-10-24 12:54:50 -0700465 if (of_property_read_bool(np, "wakeup-source")) {
Gregory Fong19a7b692015-07-31 18:17:43 -0700466 priv->parent_wake_irq = platform_get_irq(pdev, 1);
467 if (priv->parent_wake_irq < 0) {
Doug Berger0752df62017-10-24 12:54:46 -0700468 priv->parent_wake_irq = 0;
Gregory Fong19a7b692015-07-31 18:17:43 -0700469 dev_warn(dev,
470 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
471 } else {
Gregory Fong3afa1292015-07-31 18:17:44 -0700472 /*
Doug Berger47142212017-10-24 12:54:51 -0700473 * Set wakeup capability so we can process boot-time
474 * "wakeups" (e.g., from S5 cold boot)
Gregory Fong3afa1292015-07-31 18:17:44 -0700475 */
476 device_set_wakeup_capable(dev, true);
477 device_wakeup_enable(dev);
478 err = devm_request_irq(dev, priv->parent_wake_irq,
Doug Berger0752df62017-10-24 12:54:46 -0700479 brcmstb_gpio_wake_irq_handler,
480 IRQF_SHARED,
481 "brcmstb-gpio-wake", priv);
Gregory Fong19a7b692015-07-31 18:17:43 -0700482
483 if (err < 0) {
484 dev_err(dev, "Couldn't request wake IRQ");
Doug Berger0ba31dc2017-10-24 12:54:50 -0700485 goto out_free_domain;
Gregory Fong19a7b692015-07-31 18:17:43 -0700486 }
Gregory Fong19a7b692015-07-31 18:17:43 -0700487 }
488 }
489
Doug Berger0ba31dc2017-10-24 12:54:50 -0700490 priv->irq_chip.name = dev_name(dev);
491 priv->irq_chip.irq_disable = brcmstb_gpio_irq_mask;
492 priv->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
493 priv->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
494 priv->irq_chip.irq_ack = brcmstb_gpio_irq_ack;
495 priv->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
Gregory Fong19a7b692015-07-31 18:17:43 -0700496
Doug Berger0ba31dc2017-10-24 12:54:50 -0700497 if (priv->parent_wake_irq)
498 priv->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
499
500 irq_set_chained_handler_and_data(priv->parent_irq,
501 brcmstb_gpio_irq_handler, priv);
Doug Berger47142212017-10-24 12:54:51 -0700502 irq_set_status_flags(priv->parent_irq, IRQ_DISABLE_UNLAZY);
Gregory Fong19a7b692015-07-31 18:17:43 -0700503
504 return 0;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700505
506out_free_domain:
507 irq_domain_remove(priv->irq_domain);
508
509 return err;
Gregory Fong19a7b692015-07-31 18:17:43 -0700510}
511
Doug Berger47142212017-10-24 12:54:51 -0700512static void brcmstb_gpio_bank_save(struct brcmstb_gpio_priv *priv,
513 struct brcmstb_gpio_bank *bank)
514{
515 struct gpio_chip *gc = &bank->gc;
516 unsigned int i;
517
518 for (i = 0; i < GIO_REG_STAT; i++)
519 bank->saved_regs[i] = gc->read_reg(priv->reg_base +
520 GIO_BANK_OFF(bank->id, i));
521}
522
523static void brcmstb_gpio_quiesce(struct device *dev, bool save)
524{
525 struct brcmstb_gpio_priv *priv = dev_get_drvdata(dev);
526 struct brcmstb_gpio_bank *bank;
527 struct gpio_chip *gc;
528 u32 imask;
529
530 /* disable non-wake interrupt */
531 if (priv->parent_irq >= 0)
532 disable_irq(priv->parent_irq);
533
534 list_for_each_entry(bank, &priv->bank_list, node) {
535 gc = &bank->gc;
536
537 if (save)
538 brcmstb_gpio_bank_save(priv, bank);
539
540 /* Unmask GPIOs which have been flagged as wake-up sources */
541 if (priv->parent_wake_irq)
542 imask = bank->wake_active;
543 else
544 imask = 0;
545 gc->write_reg(priv->reg_base + GIO_MASK(bank->id),
546 imask);
547 }
548}
549
550static void brcmstb_gpio_shutdown(struct platform_device *pdev)
551{
552 /* Enable GPIO for S5 cold boot */
553 brcmstb_gpio_quiesce(&pdev->dev, false);
554}
555
556#ifdef CONFIG_PM_SLEEP
557static void brcmstb_gpio_bank_restore(struct brcmstb_gpio_priv *priv,
558 struct brcmstb_gpio_bank *bank)
559{
560 struct gpio_chip *gc = &bank->gc;
561 unsigned int i;
562
563 for (i = 0; i < GIO_REG_STAT; i++)
564 gc->write_reg(priv->reg_base + GIO_BANK_OFF(bank->id, i),
565 bank->saved_regs[i]);
566}
567
568static int brcmstb_gpio_suspend(struct device *dev)
569{
570 brcmstb_gpio_quiesce(dev, true);
571 return 0;
572}
573
574static int brcmstb_gpio_resume(struct device *dev)
575{
576 struct brcmstb_gpio_priv *priv = dev_get_drvdata(dev);
577 struct brcmstb_gpio_bank *bank;
578 bool need_wakeup_event = false;
579
580 list_for_each_entry(bank, &priv->bank_list, node) {
581 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
582 brcmstb_gpio_bank_restore(priv, bank);
583 }
584
585 if (priv->parent_wake_irq && need_wakeup_event)
586 pm_wakeup_event(dev, 0);
587
588 /* enable non-wake interrupt */
589 if (priv->parent_irq >= 0)
590 enable_irq(priv->parent_irq);
591
592 return 0;
593}
594
595#else
596#define brcmstb_gpio_suspend NULL
597#define brcmstb_gpio_resume NULL
598#endif /* CONFIG_PM_SLEEP */
599
600static const struct dev_pm_ops brcmstb_gpio_pm_ops = {
601 .suspend_noirq = brcmstb_gpio_suspend,
602 .resume_noirq = brcmstb_gpio_resume,
603};
604
Gregory Fong3b0213d2015-05-28 19:14:05 -0700605static int brcmstb_gpio_probe(struct platform_device *pdev)
606{
607 struct device *dev = &pdev->dev;
608 struct device_node *np = dev->of_node;
609 void __iomem *reg_base;
610 struct brcmstb_gpio_priv *priv;
611 struct resource *res;
612 struct property *prop;
613 const __be32 *p;
614 u32 bank_width;
Gregory Fong19a7b692015-07-31 18:17:43 -0700615 int num_banks = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700616 int err;
617 static int gpio_base;
Florian Fainellice5a7e812016-01-06 10:55:22 -0800618 unsigned long flags = 0;
Doug Berger47142212017-10-24 12:54:51 -0700619 bool need_wakeup_event = false;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700620
621 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
622 if (!priv)
623 return -ENOMEM;
Gregory Fong22526072015-06-17 18:00:40 -0700624 platform_set_drvdata(pdev, priv);
625 INIT_LIST_HEAD(&priv->bank_list);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700626
627 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
628 reg_base = devm_ioremap_resource(dev, res);
629 if (IS_ERR(reg_base))
630 return PTR_ERR(reg_base);
631
632 priv->gpio_base = gpio_base;
633 priv->reg_base = reg_base;
634 priv->pdev = pdev;
635
Gregory Fong19a7b692015-07-31 18:17:43 -0700636 if (of_property_read_bool(np, "interrupt-controller")) {
637 priv->parent_irq = platform_get_irq(pdev, 0);
Stephen Boyd15bddb72019-07-30 11:15:15 -0700638 if (priv->parent_irq <= 0)
Gregory Fong19a7b692015-07-31 18:17:43 -0700639 return -ENOENT;
Gregory Fong19a7b692015-07-31 18:17:43 -0700640 } else {
641 priv->parent_irq = -ENOENT;
642 }
643
Gregory Fong3b0213d2015-05-28 19:14:05 -0700644 if (brcmstb_gpio_sanity_check_banks(dev, np, res))
645 return -EINVAL;
646
Florian Fainellice5a7e812016-01-06 10:55:22 -0800647 /*
648 * MIPS endianness is configured by boot strap, which also reverses all
649 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
650 * endian I/O).
651 *
652 * Other architectures (e.g., ARM) either do not support big endian, or
653 * else leave I/O in little endian mode.
654 */
655#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
656 flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
657#endif
658
Gregory Fong3b0213d2015-05-28 19:14:05 -0700659 of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
660 bank_width) {
661 struct brcmstb_gpio_bank *bank;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700662 struct gpio_chip *gc;
663
Justin Chenbfba2232018-08-17 16:47:39 -0700664 /*
665 * If bank_width is 0, then there is an empty bank in the
666 * register block. Special handling for this case.
667 */
668 if (bank_width == 0) {
669 dev_dbg(dev, "Width 0 found: Empty bank @ %d\n",
670 num_banks);
671 num_banks++;
672 gpio_base += MAX_GPIO_PER_BANK;
673 continue;
674 }
675
Gregory Fong3b0213d2015-05-28 19:14:05 -0700676 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
677 if (!bank) {
678 err = -ENOMEM;
679 goto fail;
680 }
681
682 bank->parent_priv = priv;
Gregory Fong19a7b692015-07-31 18:17:43 -0700683 bank->id = num_banks;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700684 if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
685 dev_err(dev, "Invalid bank width %d\n", bank_width);
Axel Lin35b3fc882016-04-10 18:15:15 +0800686 err = -EINVAL;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700687 goto fail;
688 } else {
689 bank->width = bank_width;
690 }
691
692 /*
693 * Regs are 4 bytes wide, have data reg, no set/clear regs,
694 * and direction bits have 0 = output and 1 = input
695 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100696 gc = &bank->gc;
697 err = bgpio_init(gc, dev, 4,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700698 reg_base + GIO_DATA(bank->id),
699 NULL, NULL, NULL,
Florian Fainellice5a7e812016-01-06 10:55:22 -0800700 reg_base + GIO_IODIR(bank->id), flags);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700701 if (err) {
702 dev_err(dev, "bgpio_init() failed\n");
703 goto fail;
704 }
705
Gregory Fong3b0213d2015-05-28 19:14:05 -0700706 gc->owner = THIS_MODULE;
Andy Shevchenkoe85dd532021-12-23 15:15:38 +0200707 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
Arvind Yadavba3e2172017-09-21 10:44:13 +0530708 if (!gc->label) {
709 err = -ENOMEM;
710 goto fail;
711 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700712 gc->base = gpio_base;
713 gc->of_gpio_n_cells = 2;
714 gc->of_xlate = brcmstb_gpio_of_xlate;
715 /* not all ngpio lines are valid, will use bank width later */
716 gc->ngpio = MAX_GPIO_PER_BANK;
Sergio Paracuellose5de9d282021-07-28 06:12:53 +0200717 gc->offset = bank->id * MAX_GPIO_PER_BANK;
Doug Berger0ba31dc2017-10-24 12:54:50 -0700718 if (priv->parent_irq > 0)
719 gc->to_irq = brcmstb_gpio_to_irq;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700720
Gregory Fong3afa1292015-07-31 18:17:44 -0700721 /*
722 * Mask all interrupts by default, since wakeup interrupts may
723 * be retained from S5 cold boot
724 */
Doug Berger47142212017-10-24 12:54:51 -0700725 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100726 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
Gregory Fong3afa1292015-07-31 18:17:44 -0700727
Linus Walleij0f4630f2015-12-04 14:02:58 +0100728 err = gpiochip_add_data(gc, bank);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700729 if (err) {
730 dev_err(dev, "Could not add gpiochip for bank %d\n",
731 bank->id);
732 goto fail;
733 }
734 gpio_base += gc->ngpio;
Gregory Fong19a7b692015-07-31 18:17:43 -0700735
Gregory Fong3b0213d2015-05-28 19:14:05 -0700736 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
737 gc->base, gc->ngpio, bank->width);
738
739 /* Everything looks good, so add bank to list */
740 list_add(&bank->node, &priv->bank_list);
741
Gregory Fong19a7b692015-07-31 18:17:43 -0700742 num_banks++;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700743 }
744
Doug Berger0ba31dc2017-10-24 12:54:50 -0700745 priv->num_gpios = gpio_base - priv->gpio_base;
746 if (priv->parent_irq > 0) {
747 err = brcmstb_gpio_irq_setup(pdev, priv);
748 if (err)
749 goto fail;
750 }
751
Doug Berger47142212017-10-24 12:54:51 -0700752 if (priv->parent_wake_irq && need_wakeup_event)
753 pm_wakeup_event(dev, 0);
754
Gregory Fong3b0213d2015-05-28 19:14:05 -0700755 return 0;
756
757fail:
758 (void) brcmstb_gpio_remove(pdev);
759 return err;
760}
761
762static const struct of_device_id brcmstb_gpio_of_match[] = {
763 { .compatible = "brcm,brcmstb-gpio" },
764 {},
765};
766
767MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
768
769static struct platform_driver brcmstb_gpio_driver = {
770 .driver = {
771 .name = "brcmstb-gpio",
772 .of_match_table = brcmstb_gpio_of_match,
Doug Berger47142212017-10-24 12:54:51 -0700773 .pm = &brcmstb_gpio_pm_ops,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700774 },
775 .probe = brcmstb_gpio_probe,
776 .remove = brcmstb_gpio_remove,
Doug Berger47142212017-10-24 12:54:51 -0700777 .shutdown = brcmstb_gpio_shutdown,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700778};
779module_platform_driver(brcmstb_gpio_driver);
780
781MODULE_AUTHOR("Gregory Fong");
782MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
783MODULE_LICENSE("GPL v2");