blob: 2031b73e066c18099c46ae6341f19d8b9c02b7f3 [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
Gregory Fong19a7b692015-07-31 18:17:43 -070066static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
67 unsigned int offset, bool enable)
68{
Linus Walleij0f4630f2015-12-04 14:02:58 +010069 struct gpio_chip *gc = &bank->gc;
Gregory Fong19a7b692015-07-31 18:17:43 -070070 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Gregory Fong19a7b692015-07-31 18:17:43 -070071 u32 imask;
72 unsigned long flags;
73
Linus Walleij0f4630f2015-12-04 14:02:58 +010074 spin_lock_irqsave(&gc->bgpio_lock, flags);
75 imask = gc->read_reg(priv->reg_base + GIO_MASK(bank->id));
Gregory Fong19a7b692015-07-31 18:17:43 -070076 if (enable)
Linus Walleijd7442362017-10-20 15:45:34 +020077 imask |= BIT(offset);
Gregory Fong19a7b692015-07-31 18:17:43 -070078 else
Linus Walleijd7442362017-10-20 15:45:34 +020079 imask &= ~BIT(offset);
Linus Walleij0f4630f2015-12-04 14:02:58 +010080 gc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask);
81 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -070082}
83
84/* -------------------- IRQ chip functions -------------------- */
85
86static void brcmstb_gpio_irq_mask(struct irq_data *d)
87{
88 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +010089 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -070090
91 brcmstb_gpio_set_imask(bank, d->hwirq, false);
92}
93
94static void brcmstb_gpio_irq_unmask(struct irq_data *d)
95{
96 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +010097 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -070098
99 brcmstb_gpio_set_imask(bank, d->hwirq, true);
100}
101
102static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
103{
104 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100105 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong19a7b692015-07-31 18:17:43 -0700106 struct brcmstb_gpio_priv *priv = bank->parent_priv;
107 u32 mask = BIT(d->hwirq);
108 u32 edge_insensitive, iedge_insensitive;
109 u32 edge_config, iedge_config;
110 u32 level, ilevel;
111 unsigned long flags;
112
113 switch (type) {
114 case IRQ_TYPE_LEVEL_LOW:
115 level = 0;
116 edge_config = 0;
117 edge_insensitive = 0;
118 break;
119 case IRQ_TYPE_LEVEL_HIGH:
120 level = mask;
121 edge_config = 0;
122 edge_insensitive = 0;
123 break;
124 case IRQ_TYPE_EDGE_FALLING:
125 level = 0;
126 edge_config = 0;
127 edge_insensitive = 0;
128 break;
129 case IRQ_TYPE_EDGE_RISING:
130 level = 0;
131 edge_config = mask;
132 edge_insensitive = 0;
133 break;
134 case IRQ_TYPE_EDGE_BOTH:
135 level = 0;
136 edge_config = 0; /* don't care, but want known value */
137 edge_insensitive = mask;
138 break;
139 default:
140 return -EINVAL;
141 }
142
Linus Walleij0f4630f2015-12-04 14:02:58 +0100143 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700144
Linus Walleij0f4630f2015-12-04 14:02:58 +0100145 iedge_config = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700146 GIO_EC(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100147 iedge_insensitive = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700148 GIO_EI(bank->id)) & ~mask;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100149 ilevel = bank->gc.read_reg(priv->reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700150 GIO_LEVEL(bank->id)) & ~mask;
151
Linus Walleij0f4630f2015-12-04 14:02:58 +0100152 bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700153 iedge_config | edge_config);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100154 bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700155 iedge_insensitive | edge_insensitive);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100156 bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700157 ilevel | level);
158
Linus Walleij0f4630f2015-12-04 14:02:58 +0100159 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700160 return 0;
161}
162
Gregory Fong3afa1292015-07-31 18:17:44 -0700163static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
164 unsigned int enable)
Gregory Fong19a7b692015-07-31 18:17:43 -0700165{
Gregory Fong19a7b692015-07-31 18:17:43 -0700166 int ret = 0;
167
168 /*
169 * Only enable wake IRQ once for however many hwirqs can wake
170 * since they all use the same wake IRQ. Mask will be set
171 * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
172 */
173 if (enable)
174 ret = enable_irq_wake(priv->parent_wake_irq);
175 else
176 ret = disable_irq_wake(priv->parent_wake_irq);
177 if (ret)
178 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
179 enable ? "enable" : "disable");
180 return ret;
181}
182
Gregory Fong3afa1292015-07-31 18:17:44 -0700183static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
184{
185 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
186 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
187
188 return brcmstb_gpio_priv_set_wake(priv, enable);
189}
190
Gregory Fong19a7b692015-07-31 18:17:43 -0700191static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
192{
193 struct brcmstb_gpio_priv *priv = data;
194
195 if (!priv || irq != priv->parent_wake_irq)
196 return IRQ_NONE;
197 pm_wakeup_event(&priv->pdev->dev, 0);
198 return IRQ_HANDLED;
199}
200
201static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
202{
203 struct brcmstb_gpio_priv *priv = bank->parent_priv;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100204 struct irq_domain *irq_domain = bank->gc.irqdomain;
Gregory Fong19a7b692015-07-31 18:17:43 -0700205 void __iomem *reg_base = priv->reg_base;
206 unsigned long status;
207 unsigned long flags;
208
Linus Walleij0f4630f2015-12-04 14:02:58 +0100209 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
210 while ((status = bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) &
211 bank->gc.read_reg(reg_base + GIO_MASK(bank->id)))) {
Gregory Fong19a7b692015-07-31 18:17:43 -0700212 int bit;
213
214 for_each_set_bit(bit, &status, 32) {
Linus Walleij0f4630f2015-12-04 14:02:58 +0100215 u32 stat = bank->gc.read_reg(reg_base +
Gregory Fong19a7b692015-07-31 18:17:43 -0700216 GIO_STAT(bank->id));
217 if (bit >= bank->width)
218 dev_warn(&priv->pdev->dev,
219 "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
220 bank->id, bit);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100221 bank->gc.write_reg(reg_base + GIO_STAT(bank->id),
Gregory Fong19a7b692015-07-31 18:17:43 -0700222 stat | BIT(bit));
223 generic_handle_irq(irq_find_mapping(irq_domain, bit));
224 }
225 }
Linus Walleij0f4630f2015-12-04 14:02:58 +0100226 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
Gregory Fong19a7b692015-07-31 18:17:43 -0700227}
228
229/* Each UPG GIO block has one IRQ for all banks */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200230static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
Gregory Fong19a7b692015-07-31 18:17:43 -0700231{
232 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
233 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
234 struct irq_chip *chip = irq_desc_get_chip(desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800235 struct brcmstb_gpio_bank *bank;
Gregory Fong19a7b692015-07-31 18:17:43 -0700236
237 /* Interrupts weren't properly cleared during probe */
238 BUG_ON(!priv || !chip);
239
240 chained_irq_enter(chip, desc);
Axel Linb178e7e2016-02-20 09:50:37 +0800241 list_for_each_entry(bank, &priv->bank_list, node)
Gregory Fong19a7b692015-07-31 18:17:43 -0700242 brcmstb_gpio_irq_bank_handler(bank);
Gregory Fong19a7b692015-07-31 18:17:43 -0700243 chained_irq_exit(chip, desc);
244}
245
Gregory Fong3afa1292015-07-31 18:17:44 -0700246static int brcmstb_gpio_reboot(struct notifier_block *nb,
247 unsigned long action, void *data)
248{
249 struct brcmstb_gpio_priv *priv =
250 container_of(nb, struct brcmstb_gpio_priv, reboot_notifier);
251
252 /* Enable GPIO for S5 cold boot */
253 if (action == SYS_POWER_OFF)
254 brcmstb_gpio_priv_set_wake(priv, 1);
255
256 return NOTIFY_DONE;
257}
258
Gregory Fong3b0213d2015-05-28 19:14:05 -0700259/* Make sure that the number of banks matches up between properties */
260static int brcmstb_gpio_sanity_check_banks(struct device *dev,
261 struct device_node *np, struct resource *res)
262{
263 int res_num_banks = resource_size(res) / GIO_BANK_SIZE;
264 int num_banks =
265 of_property_count_u32_elems(np, "brcm,gpio-bank-widths");
266
267 if (res_num_banks != num_banks) {
268 dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n",
269 res_num_banks, num_banks);
270 return -EINVAL;
271 } else {
272 return 0;
273 }
274}
275
276static int brcmstb_gpio_remove(struct platform_device *pdev)
277{
278 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700279 struct brcmstb_gpio_bank *bank;
280 int ret = 0;
281
Gregory Fong22526072015-06-17 18:00:40 -0700282 if (!priv) {
283 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
284 return -EFAULT;
285 }
286
287 /*
288 * You can lose return values below, but we report all errors, and it's
289 * more important to actually perform all of the steps.
290 */
Axel Linb178e7e2016-02-20 09:50:37 +0800291 list_for_each_entry(bank, &priv->bank_list, node)
Linus Walleij0f4630f2015-12-04 14:02:58 +0100292 gpiochip_remove(&bank->gc);
Axel Linb178e7e2016-02-20 09:50:37 +0800293
Gregory Fong3afa1292015-07-31 18:17:44 -0700294 if (priv->reboot_notifier.notifier_call) {
295 ret = unregister_reboot_notifier(&priv->reboot_notifier);
296 if (ret)
297 dev_err(&pdev->dev,
298 "failed to unregister reboot notifier\n");
299 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700300 return ret;
301}
302
303static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
304 const struct of_phandle_args *gpiospec, u32 *flags)
305{
306 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100307 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700308 int offset;
309
310 if (gc->of_gpio_n_cells != 2) {
311 WARN_ON(1);
312 return -EINVAL;
313 }
314
315 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
316 return -EINVAL;
317
318 offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
Gregory Fong19a7b692015-07-31 18:17:43 -0700319 if (offset >= gc->ngpio || offset < 0)
Gregory Fong3b0213d2015-05-28 19:14:05 -0700320 return -EINVAL;
321
322 if (unlikely(offset >= bank->width)) {
323 dev_warn_ratelimited(&priv->pdev->dev,
324 "Received request for invalid GPIO offset %d\n",
325 gpiospec->args[0]);
326 }
327
328 if (flags)
329 *flags = gpiospec->args[1];
330
331 return offset;
332}
333
Gregory Fong19a7b692015-07-31 18:17:43 -0700334/* Before calling, must have bank->parent_irq set and gpiochip registered */
335static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
336 struct brcmstb_gpio_bank *bank)
337{
338 struct brcmstb_gpio_priv *priv = bank->parent_priv;
339 struct device *dev = &pdev->dev;
340 struct device_node *np = dev->of_node;
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900341 int err;
Gregory Fong19a7b692015-07-31 18:17:43 -0700342
343 bank->irq_chip.name = dev_name(dev);
344 bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
345 bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
346 bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
347
348 /* Ensures that all non-wakeup IRQs are disabled at suspend */
349 bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
350
Doug Berger0752df62017-10-24 12:54:46 -0700351 if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->parent_wake_irq &&
Gregory Fong19a7b692015-07-31 18:17:43 -0700352 of_property_read_bool(np, "wakeup-source")) {
353 priv->parent_wake_irq = platform_get_irq(pdev, 1);
354 if (priv->parent_wake_irq < 0) {
Doug Berger0752df62017-10-24 12:54:46 -0700355 priv->parent_wake_irq = 0;
Gregory Fong19a7b692015-07-31 18:17:43 -0700356 dev_warn(dev,
357 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
358 } else {
Gregory Fong3afa1292015-07-31 18:17:44 -0700359 /*
360 * Set wakeup capability before requesting wakeup
361 * interrupt, so we can process boot-time "wakeups"
362 * (e.g., from S5 cold boot)
363 */
364 device_set_wakeup_capable(dev, true);
365 device_wakeup_enable(dev);
366 err = devm_request_irq(dev, priv->parent_wake_irq,
Doug Berger0752df62017-10-24 12:54:46 -0700367 brcmstb_gpio_wake_irq_handler,
368 IRQF_SHARED,
369 "brcmstb-gpio-wake", priv);
Gregory Fong19a7b692015-07-31 18:17:43 -0700370
371 if (err < 0) {
372 dev_err(dev, "Couldn't request wake IRQ");
373 return err;
374 }
375
Gregory Fong3afa1292015-07-31 18:17:44 -0700376 priv->reboot_notifier.notifier_call =
377 brcmstb_gpio_reboot;
378 register_reboot_notifier(&priv->reboot_notifier);
Gregory Fong19a7b692015-07-31 18:17:43 -0700379 }
380 }
381
Doug Berger0752df62017-10-24 12:54:46 -0700382 if (priv->parent_wake_irq)
Gregory Fong19a7b692015-07-31 18:17:43 -0700383 bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
384
Masahiro Yamadaf89c6ea2017-08-10 07:51:27 +0900385 err = gpiochip_irqchip_add(&bank->gc, &bank->irq_chip, 0,
386 handle_simple_irq, IRQ_TYPE_NONE);
387 if (err)
388 return err;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100389 gpiochip_set_chained_irqchip(&bank->gc, &bank->irq_chip,
Gregory Fong19a7b692015-07-31 18:17:43 -0700390 priv->parent_irq, brcmstb_gpio_irq_handler);
391
392 return 0;
393}
394
Gregory Fong3b0213d2015-05-28 19:14:05 -0700395static int brcmstb_gpio_probe(struct platform_device *pdev)
396{
397 struct device *dev = &pdev->dev;
398 struct device_node *np = dev->of_node;
399 void __iomem *reg_base;
400 struct brcmstb_gpio_priv *priv;
401 struct resource *res;
402 struct property *prop;
403 const __be32 *p;
404 u32 bank_width;
Gregory Fong19a7b692015-07-31 18:17:43 -0700405 int num_banks = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700406 int err;
407 static int gpio_base;
Florian Fainellice5a7e82016-01-06 10:55:22 -0800408 unsigned long flags = 0;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700409
410 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
411 if (!priv)
412 return -ENOMEM;
Gregory Fong22526072015-06-17 18:00:40 -0700413 platform_set_drvdata(pdev, priv);
414 INIT_LIST_HEAD(&priv->bank_list);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700415
416 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
417 reg_base = devm_ioremap_resource(dev, res);
418 if (IS_ERR(reg_base))
419 return PTR_ERR(reg_base);
420
421 priv->gpio_base = gpio_base;
422 priv->reg_base = reg_base;
423 priv->pdev = pdev;
424
Gregory Fong19a7b692015-07-31 18:17:43 -0700425 if (of_property_read_bool(np, "interrupt-controller")) {
426 priv->parent_irq = platform_get_irq(pdev, 0);
427 if (priv->parent_irq <= 0) {
428 dev_err(dev, "Couldn't get IRQ");
429 return -ENOENT;
430 }
431 } else {
432 priv->parent_irq = -ENOENT;
433 }
434
Gregory Fong3b0213d2015-05-28 19:14:05 -0700435 if (brcmstb_gpio_sanity_check_banks(dev, np, res))
436 return -EINVAL;
437
Florian Fainellice5a7e82016-01-06 10:55:22 -0800438 /*
439 * MIPS endianness is configured by boot strap, which also reverses all
440 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
441 * endian I/O).
442 *
443 * Other architectures (e.g., ARM) either do not support big endian, or
444 * else leave I/O in little endian mode.
445 */
446#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
447 flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
448#endif
449
Gregory Fong3b0213d2015-05-28 19:14:05 -0700450 of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
451 bank_width) {
452 struct brcmstb_gpio_bank *bank;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700453 struct gpio_chip *gc;
454
455 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
456 if (!bank) {
457 err = -ENOMEM;
458 goto fail;
459 }
460
461 bank->parent_priv = priv;
Gregory Fong19a7b692015-07-31 18:17:43 -0700462 bank->id = num_banks;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700463 if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
464 dev_err(dev, "Invalid bank width %d\n", bank_width);
Axel Lin35b3fc882016-04-10 18:15:15 +0800465 err = -EINVAL;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700466 goto fail;
467 } else {
468 bank->width = bank_width;
469 }
470
471 /*
472 * Regs are 4 bytes wide, have data reg, no set/clear regs,
473 * and direction bits have 0 = output and 1 = input
474 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100475 gc = &bank->gc;
476 err = bgpio_init(gc, dev, 4,
Gregory Fong3b0213d2015-05-28 19:14:05 -0700477 reg_base + GIO_DATA(bank->id),
478 NULL, NULL, NULL,
Florian Fainellice5a7e82016-01-06 10:55:22 -0800479 reg_base + GIO_IODIR(bank->id), flags);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700480 if (err) {
481 dev_err(dev, "bgpio_init() failed\n");
482 goto fail;
483 }
484
Gregory Fong3b0213d2015-05-28 19:14:05 -0700485 gc->of_node = np;
486 gc->owner = THIS_MODULE;
Rob Herring7eb6ce22017-07-18 16:43:03 -0500487 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node);
Arvind Yadavba3e2172017-09-21 10:44:13 +0530488 if (!gc->label) {
489 err = -ENOMEM;
490 goto fail;
491 }
Gregory Fong3b0213d2015-05-28 19:14:05 -0700492 gc->base = gpio_base;
493 gc->of_gpio_n_cells = 2;
494 gc->of_xlate = brcmstb_gpio_of_xlate;
495 /* not all ngpio lines are valid, will use bank width later */
496 gc->ngpio = MAX_GPIO_PER_BANK;
497
Gregory Fong3afa1292015-07-31 18:17:44 -0700498 /*
499 * Mask all interrupts by default, since wakeup interrupts may
500 * be retained from S5 cold boot
501 */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100502 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
Gregory Fong3afa1292015-07-31 18:17:44 -0700503
Linus Walleij0f4630f2015-12-04 14:02:58 +0100504 err = gpiochip_add_data(gc, bank);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700505 if (err) {
506 dev_err(dev, "Could not add gpiochip for bank %d\n",
507 bank->id);
508 goto fail;
509 }
510 gpio_base += gc->ngpio;
Gregory Fong19a7b692015-07-31 18:17:43 -0700511
512 if (priv->parent_irq > 0) {
513 err = brcmstb_gpio_irq_setup(pdev, bank);
514 if (err)
515 goto fail;
516 }
517
Gregory Fong3b0213d2015-05-28 19:14:05 -0700518 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
519 gc->base, gc->ngpio, bank->width);
520
521 /* Everything looks good, so add bank to list */
522 list_add(&bank->node, &priv->bank_list);
523
Gregory Fong19a7b692015-07-31 18:17:43 -0700524 num_banks++;
Gregory Fong3b0213d2015-05-28 19:14:05 -0700525 }
526
527 dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
Gregory Fong19a7b692015-07-31 18:17:43 -0700528 num_banks, priv->gpio_base, gpio_base - 1);
Gregory Fong3b0213d2015-05-28 19:14:05 -0700529
Gregory Fong3b0213d2015-05-28 19:14:05 -0700530 return 0;
531
532fail:
533 (void) brcmstb_gpio_remove(pdev);
534 return err;
535}
536
537static const struct of_device_id brcmstb_gpio_of_match[] = {
538 { .compatible = "brcm,brcmstb-gpio" },
539 {},
540};
541
542MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
543
544static struct platform_driver brcmstb_gpio_driver = {
545 .driver = {
546 .name = "brcmstb-gpio",
547 .of_match_table = brcmstb_gpio_of_match,
548 },
549 .probe = brcmstb_gpio_probe,
550 .remove = brcmstb_gpio_remove,
551};
552module_platform_driver(brcmstb_gpio_driver);
553
554MODULE_AUTHOR("Gregory Fong");
555MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
556MODULE_LICENSE("GPL v2");