blob: b43cd84b61f161302d592dd2d0cc29161586c0fa [file] [log] [blame]
Jamie Iles7779b342014-02-25 17:01:01 -06001/*
2 * Copyright (c) 2011 Jamie Iles
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * All enquiries to support@picochip.com
9 */
10#include <linux/basic_mmio_gpio.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/ioport.h>
16#include <linux/irq.h>
17#include <linux/irqdomain.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_irq.h>
22#include <linux/platform_device.h>
23#include <linux/spinlock.h>
Weike Chen3d2613c2014-09-17 09:18:39 -070024#include <linux/platform_data/gpio-dwapb.h>
25#include <linux/slab.h>
Jamie Iles7779b342014-02-25 17:01:01 -060026
27#define GPIO_SWPORTA_DR 0x00
28#define GPIO_SWPORTA_DDR 0x04
29#define GPIO_SWPORTB_DR 0x0c
30#define GPIO_SWPORTB_DDR 0x10
31#define GPIO_SWPORTC_DR 0x18
32#define GPIO_SWPORTC_DDR 0x1c
33#define GPIO_SWPORTD_DR 0x24
34#define GPIO_SWPORTD_DDR 0x28
35#define GPIO_INTEN 0x30
36#define GPIO_INTMASK 0x34
37#define GPIO_INTTYPE_LEVEL 0x38
38#define GPIO_INT_POLARITY 0x3c
39#define GPIO_INTSTATUS 0x40
Weike Chen5d60d9e2014-09-17 09:18:41 -070040#define GPIO_PORTA_DEBOUNCE 0x48
Jamie Iles7779b342014-02-25 17:01:01 -060041#define GPIO_PORTA_EOI 0x4c
42#define GPIO_EXT_PORTA 0x50
43#define GPIO_EXT_PORTB 0x54
44#define GPIO_EXT_PORTC 0x58
45#define GPIO_EXT_PORTD 0x5c
46
47#define DWAPB_MAX_PORTS 4
48#define GPIO_EXT_PORT_SIZE (GPIO_EXT_PORTB - GPIO_EXT_PORTA)
49#define GPIO_SWPORT_DR_SIZE (GPIO_SWPORTB_DR - GPIO_SWPORTA_DR)
50#define GPIO_SWPORT_DDR_SIZE (GPIO_SWPORTB_DDR - GPIO_SWPORTA_DDR)
51
52struct dwapb_gpio;
53
Weike Chen1e960db2014-09-17 09:18:42 -070054#ifdef CONFIG_PM_SLEEP
55/* Store GPIO context across system-wide suspend/resume transitions */
56struct dwapb_context {
57 u32 data;
58 u32 dir;
59 u32 ext;
60 u32 int_en;
61 u32 int_mask;
62 u32 int_type;
63 u32 int_pol;
64 u32 int_deb;
65};
66#endif
67
Jamie Iles7779b342014-02-25 17:01:01 -060068struct dwapb_gpio_port {
69 struct bgpio_chip bgc;
70 bool is_registered;
71 struct dwapb_gpio *gpio;
Weike Chen1e960db2014-09-17 09:18:42 -070072#ifdef CONFIG_PM_SLEEP
73 struct dwapb_context *ctx;
74#endif
75 unsigned int idx;
Jamie Iles7779b342014-02-25 17:01:01 -060076};
77
78struct dwapb_gpio {
79 struct device *dev;
80 void __iomem *regs;
81 struct dwapb_gpio_port *ports;
82 unsigned int nr_ports;
83 struct irq_domain *domain;
84};
85
Weike Chen5d60d9e2014-09-17 09:18:41 -070086static inline struct dwapb_gpio_port *
87to_dwapb_gpio_port(struct bgpio_chip *bgc)
88{
89 return container_of(bgc, struct dwapb_gpio_port, bgc);
90}
91
Weike Chen67809b92014-09-17 09:18:40 -070092static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
93{
94 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
95 void __iomem *reg_base = gpio->regs;
96
97 return bgc->read_reg(reg_base + offset);
98}
99
100static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
101 u32 val)
102{
103 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
104 void __iomem *reg_base = gpio->regs;
105
106 bgc->write_reg(reg_base + offset, val);
107}
108
Jamie Iles7779b342014-02-25 17:01:01 -0600109static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
110{
111 struct bgpio_chip *bgc = to_bgpio_chip(gc);
Weike Chen5d60d9e2014-09-17 09:18:41 -0700112 struct dwapb_gpio_port *port = to_dwapb_gpio_port(bgc);
Jamie Iles7779b342014-02-25 17:01:01 -0600113 struct dwapb_gpio *gpio = port->gpio;
114
115 return irq_find_mapping(gpio->domain, offset);
116}
117
118static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
119{
Weike Chen67809b92014-09-17 09:18:40 -0700120 u32 v = dwapb_read(gpio, GPIO_INT_POLARITY);
Jamie Iles7779b342014-02-25 17:01:01 -0600121
122 if (gpio_get_value(gpio->ports[0].bgc.gc.base + offs))
123 v &= ~BIT(offs);
124 else
125 v |= BIT(offs);
126
Weike Chen67809b92014-09-17 09:18:40 -0700127 dwapb_write(gpio, GPIO_INT_POLARITY, v);
Jamie Iles7779b342014-02-25 17:01:01 -0600128}
129
Weike Chen3d2613c2014-09-17 09:18:39 -0700130static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
Jamie Iles7779b342014-02-25 17:01:01 -0600131{
Jamie Iles7779b342014-02-25 17:01:01 -0600132 u32 irq_status = readl_relaxed(gpio->regs + GPIO_INTSTATUS);
Weike Chen3d2613c2014-09-17 09:18:39 -0700133 u32 ret = irq_status;
Jamie Iles7779b342014-02-25 17:01:01 -0600134
135 while (irq_status) {
136 int hwirq = fls(irq_status) - 1;
137 int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
138
139 generic_handle_irq(gpio_irq);
140 irq_status &= ~BIT(hwirq);
141
142 if ((irq_get_trigger_type(gpio_irq) & IRQ_TYPE_SENSE_MASK)
143 == IRQ_TYPE_EDGE_BOTH)
144 dwapb_toggle_trigger(gpio, hwirq);
145 }
146
Weike Chen3d2613c2014-09-17 09:18:39 -0700147 return ret;
148}
149
150static void dwapb_irq_handler(u32 irq, struct irq_desc *desc)
151{
152 struct dwapb_gpio *gpio = irq_get_handler_data(irq);
153 struct irq_chip *chip = irq_desc_get_chip(desc);
154
155 dwapb_do_irq(gpio);
156
Jamie Iles7779b342014-02-25 17:01:01 -0600157 if (chip->irq_eoi)
158 chip->irq_eoi(irq_desc_get_irq_data(desc));
159}
160
161static void dwapb_irq_enable(struct irq_data *d)
162{
163 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
164 struct dwapb_gpio *gpio = igc->private;
165 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
166 unsigned long flags;
167 u32 val;
168
169 spin_lock_irqsave(&bgc->lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700170 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b342014-02-25 17:01:01 -0600171 val |= BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700172 dwapb_write(gpio, GPIO_INTEN, val);
Jamie Iles7779b342014-02-25 17:01:01 -0600173 spin_unlock_irqrestore(&bgc->lock, flags);
174}
175
176static void dwapb_irq_disable(struct irq_data *d)
177{
178 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
179 struct dwapb_gpio *gpio = igc->private;
180 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
181 unsigned long flags;
182 u32 val;
183
184 spin_lock_irqsave(&bgc->lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700185 val = dwapb_read(gpio, GPIO_INTEN);
Jamie Iles7779b342014-02-25 17:01:01 -0600186 val &= ~BIT(d->hwirq);
Weike Chen67809b92014-09-17 09:18:40 -0700187 dwapb_write(gpio, GPIO_INTEN, val);
Jamie Iles7779b342014-02-25 17:01:01 -0600188 spin_unlock_irqrestore(&bgc->lock, flags);
189}
190
Linus Walleij57ef0422014-03-14 18:16:20 +0100191static int dwapb_irq_reqres(struct irq_data *d)
Jamie Iles7779b342014-02-25 17:01:01 -0600192{
193 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
194 struct dwapb_gpio *gpio = igc->private;
195 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
196
Linus Walleij57ef0422014-03-14 18:16:20 +0100197 if (gpio_lock_as_irq(&bgc->gc, irqd_to_hwirq(d))) {
Jamie Iles7779b342014-02-25 17:01:01 -0600198 dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
199 irqd_to_hwirq(d));
Linus Walleij57ef0422014-03-14 18:16:20 +0100200 return -EINVAL;
201 }
Jamie Iles7779b342014-02-25 17:01:01 -0600202 return 0;
203}
204
Linus Walleij57ef0422014-03-14 18:16:20 +0100205static void dwapb_irq_relres(struct irq_data *d)
Jamie Iles7779b342014-02-25 17:01:01 -0600206{
207 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
208 struct dwapb_gpio *gpio = igc->private;
209 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
210
Jamie Iles7779b342014-02-25 17:01:01 -0600211 gpio_unlock_as_irq(&bgc->gc, irqd_to_hwirq(d));
212}
213
214static int dwapb_irq_set_type(struct irq_data *d, u32 type)
215{
216 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
217 struct dwapb_gpio *gpio = igc->private;
218 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
219 int bit = d->hwirq;
220 unsigned long level, polarity, flags;
221
222 if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
223 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
224 return -EINVAL;
225
226 spin_lock_irqsave(&bgc->lock, flags);
Weike Chen67809b92014-09-17 09:18:40 -0700227 level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
228 polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
Jamie Iles7779b342014-02-25 17:01:01 -0600229
230 switch (type) {
231 case IRQ_TYPE_EDGE_BOTH:
232 level |= BIT(bit);
233 dwapb_toggle_trigger(gpio, bit);
234 break;
235 case IRQ_TYPE_EDGE_RISING:
236 level |= BIT(bit);
237 polarity |= BIT(bit);
238 break;
239 case IRQ_TYPE_EDGE_FALLING:
240 level |= BIT(bit);
241 polarity &= ~BIT(bit);
242 break;
243 case IRQ_TYPE_LEVEL_HIGH:
244 level &= ~BIT(bit);
245 polarity |= BIT(bit);
246 break;
247 case IRQ_TYPE_LEVEL_LOW:
248 level &= ~BIT(bit);
249 polarity &= ~BIT(bit);
250 break;
251 }
252
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200253 irq_setup_alt_chip(d, type);
254
Weike Chen67809b92014-09-17 09:18:40 -0700255 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
256 dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
Jamie Iles7779b342014-02-25 17:01:01 -0600257 spin_unlock_irqrestore(&bgc->lock, flags);
258
259 return 0;
260}
261
Weike Chen5d60d9e2014-09-17 09:18:41 -0700262static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
263 unsigned offset, unsigned debounce)
264{
265 struct bgpio_chip *bgc = to_bgpio_chip(gc);
266 struct dwapb_gpio_port *port = to_dwapb_gpio_port(bgc);
267 struct dwapb_gpio *gpio = port->gpio;
268 unsigned long flags, val_deb;
269 unsigned long mask = bgc->pin2mask(bgc, offset);
270
271 spin_lock_irqsave(&bgc->lock, flags);
272
273 val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
274 if (debounce)
275 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb | mask);
276 else
277 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb & ~mask);
278
279 spin_unlock_irqrestore(&bgc->lock, flags);
280
281 return 0;
282}
283
Weike Chen3d2613c2014-09-17 09:18:39 -0700284static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
285{
286 u32 worked;
287 struct dwapb_gpio *gpio = dev_id;
288
289 worked = dwapb_do_irq(gpio);
290
291 return worked ? IRQ_HANDLED : IRQ_NONE;
292}
293
Jamie Iles7779b342014-02-25 17:01:01 -0600294static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700295 struct dwapb_gpio_port *port,
296 struct dwapb_port_property *pp)
Jamie Iles7779b342014-02-25 17:01:01 -0600297{
298 struct gpio_chip *gc = &port->bgc.gc;
Weike Chen3d2613c2014-09-17 09:18:39 -0700299 struct device_node *node = pp->node;
300 struct irq_chip_generic *irq_gc = NULL;
Jamie Iles7779b342014-02-25 17:01:01 -0600301 unsigned int hwirq, ngpio = gc->ngpio;
302 struct irq_chip_type *ct;
Weike Chen3d2613c2014-09-17 09:18:39 -0700303 int err, i;
Jamie Iles7779b342014-02-25 17:01:01 -0600304
305 gpio->domain = irq_domain_add_linear(node, ngpio,
306 &irq_generic_chip_ops, gpio);
307 if (!gpio->domain)
308 return;
309
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200310 err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
Jamie Iles7779b342014-02-25 17:01:01 -0600311 "gpio-dwapb", handle_level_irq,
312 IRQ_NOREQUEST, 0,
313 IRQ_GC_INIT_NESTED_LOCK);
314 if (err) {
315 dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
316 irq_domain_remove(gpio->domain);
317 gpio->domain = NULL;
318 return;
319 }
320
321 irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
322 if (!irq_gc) {
323 irq_domain_remove(gpio->domain);
324 gpio->domain = NULL;
325 return;
326 }
327
328 irq_gc->reg_base = gpio->regs;
329 irq_gc->private = gpio;
330
Sebastian Andrzej Siewior6a2f4b72014-05-26 22:58:14 +0200331 for (i = 0; i < 2; i++) {
332 ct = &irq_gc->chip_types[i];
333 ct->chip.irq_ack = irq_gc_ack_set_bit;
334 ct->chip.irq_mask = irq_gc_mask_set_bit;
335 ct->chip.irq_unmask = irq_gc_mask_clr_bit;
336 ct->chip.irq_set_type = dwapb_irq_set_type;
337 ct->chip.irq_enable = dwapb_irq_enable;
338 ct->chip.irq_disable = dwapb_irq_disable;
339 ct->chip.irq_request_resources = dwapb_irq_reqres;
340 ct->chip.irq_release_resources = dwapb_irq_relres;
341 ct->regs.ack = GPIO_PORTA_EOI;
342 ct->regs.mask = GPIO_INTMASK;
343 ct->type = IRQ_TYPE_LEVEL_MASK;
344 }
345
346 irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
347 irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
348 irq_gc->chip_types[1].handler = handle_edge_irq;
Jamie Iles7779b342014-02-25 17:01:01 -0600349
Weike Chen3d2613c2014-09-17 09:18:39 -0700350 if (!pp->irq_shared) {
351 irq_set_chained_handler(pp->irq, dwapb_irq_handler);
352 irq_set_handler_data(pp->irq, gpio);
353 } else {
354 /*
355 * Request a shared IRQ since where MFD would have devices
356 * using the same irq pin
357 */
358 err = devm_request_irq(gpio->dev, pp->irq,
359 dwapb_irq_handler_mfd,
360 IRQF_SHARED, "gpio-dwapb-mfd", gpio);
361 if (err) {
362 dev_err(gpio->dev, "error requesting IRQ\n");
363 irq_domain_remove(gpio->domain);
364 gpio->domain = NULL;
365 return;
366 }
367 }
Jamie Iles7779b342014-02-25 17:01:01 -0600368
369 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
370 irq_create_mapping(gpio->domain, hwirq);
371
372 port->bgc.gc.to_irq = dwapb_gpio_to_irq;
373}
374
375static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
376{
377 struct dwapb_gpio_port *port = &gpio->ports[0];
378 struct gpio_chip *gc = &port->bgc.gc;
379 unsigned int ngpio = gc->ngpio;
380 irq_hw_number_t hwirq;
381
382 if (!gpio->domain)
383 return;
384
385 for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
386 irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
387
388 irq_domain_remove(gpio->domain);
389 gpio->domain = NULL;
390}
391
392static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
Weike Chen3d2613c2014-09-17 09:18:39 -0700393 struct dwapb_port_property *pp,
Jamie Iles7779b342014-02-25 17:01:01 -0600394 unsigned int offs)
395{
396 struct dwapb_gpio_port *port;
Jamie Iles7779b342014-02-25 17:01:01 -0600397 void __iomem *dat, *set, *dirout;
398 int err;
399
Jamie Iles7779b342014-02-25 17:01:01 -0600400 port = &gpio->ports[offs];
401 port->gpio = gpio;
Weike Chen1e960db2014-09-17 09:18:42 -0700402 port->idx = pp->idx;
403
404#ifdef CONFIG_PM_SLEEP
405 port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
406 if (!port->ctx)
407 return -ENOMEM;
408#endif
Jamie Iles7779b342014-02-25 17:01:01 -0600409
Weike Chen3d2613c2014-09-17 09:18:39 -0700410 dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_SIZE);
411 set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_SIZE);
Jamie Iles7779b342014-02-25 17:01:01 -0600412 dirout = gpio->regs + GPIO_SWPORTA_DDR +
Weike Chen3d2613c2014-09-17 09:18:39 -0700413 (pp->idx * GPIO_SWPORT_DDR_SIZE);
Jamie Iles7779b342014-02-25 17:01:01 -0600414
415 err = bgpio_init(&port->bgc, gpio->dev, 4, dat, set, NULL, dirout,
416 NULL, false);
417 if (err) {
418 dev_err(gpio->dev, "failed to init gpio chip for %s\n",
Weike Chen3d2613c2014-09-17 09:18:39 -0700419 pp->name);
Jamie Iles7779b342014-02-25 17:01:01 -0600420 return err;
421 }
422
Weike Chen3d2613c2014-09-17 09:18:39 -0700423#ifdef CONFIG_OF_GPIO
424 port->bgc.gc.of_node = pp->node;
425#endif
426 port->bgc.gc.ngpio = pp->ngpio;
427 port->bgc.gc.base = pp->gpio_base;
Jamie Iles7779b342014-02-25 17:01:01 -0600428
Weike Chen5d60d9e2014-09-17 09:18:41 -0700429 /* Only port A support debounce */
430 if (pp->idx == 0)
431 port->bgc.gc.set_debounce = dwapb_gpio_set_debounce;
432
Weike Chen3d2613c2014-09-17 09:18:39 -0700433 if (pp->irq)
434 dwapb_configure_irqs(gpio, port, pp);
Jamie Iles7779b342014-02-25 17:01:01 -0600435
436 err = gpiochip_add(&port->bgc.gc);
437 if (err)
438 dev_err(gpio->dev, "failed to register gpiochip for %s\n",
Weike Chen3d2613c2014-09-17 09:18:39 -0700439 pp->name);
Jamie Iles7779b342014-02-25 17:01:01 -0600440 else
441 port->is_registered = true;
442
443 return err;
444}
445
446static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
447{
448 unsigned int m;
449
450 for (m = 0; m < gpio->nr_ports; ++m)
451 if (gpio->ports[m].is_registered)
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200452 gpiochip_remove(&gpio->ports[m].bgc.gc);
Jamie Iles7779b342014-02-25 17:01:01 -0600453}
454
Weike Chen3d2613c2014-09-17 09:18:39 -0700455static struct dwapb_platform_data *
456dwapb_gpio_get_pdata_of(struct device *dev)
457{
458 struct device_node *node, *port_np;
459 struct dwapb_platform_data *pdata;
460 struct dwapb_port_property *pp;
461 int nports;
462 int i;
463
464 node = dev->of_node;
465 if (!IS_ENABLED(CONFIG_OF_GPIO) || !node)
466 return ERR_PTR(-ENODEV);
467
468 nports = of_get_child_count(node);
469 if (nports == 0)
470 return ERR_PTR(-ENODEV);
471
472 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
473 if (!pdata)
474 return ERR_PTR(-ENOMEM);
475
476 pdata->properties = kcalloc(nports, sizeof(*pp), GFP_KERNEL);
477 if (!pdata->properties) {
478 kfree(pdata);
479 return ERR_PTR(-ENOMEM);
480 }
481
482 pdata->nports = nports;
483
484 i = 0;
485 for_each_child_of_node(node, port_np) {
486 pp = &pdata->properties[i++];
487 pp->node = port_np;
488
489 if (of_property_read_u32(port_np, "reg", &pp->idx) ||
490 pp->idx >= DWAPB_MAX_PORTS) {
491 dev_err(dev, "missing/invalid port index for %s\n",
492 port_np->full_name);
493 kfree(pdata->properties);
494 kfree(pdata);
495 return ERR_PTR(-EINVAL);
496 }
497
498 if (of_property_read_u32(port_np, "snps,nr-gpios",
499 &pp->ngpio)) {
500 dev_info(dev, "failed to get number of gpios for %s\n",
501 port_np->full_name);
502 pp->ngpio = 32;
503 }
504
505 /*
506 * Only port A can provide interrupts in all configurations of
507 * the IP.
508 */
509 if (pp->idx == 0 &&
510 of_property_read_bool(port_np, "interrupt-controller")) {
511 pp->irq = irq_of_parse_and_map(port_np, 0);
512 if (!pp->irq) {
513 dev_warn(dev, "no irq for bank %s\n",
514 port_np->full_name);
515 }
516 }
517
518 pp->irq_shared = false;
519 pp->gpio_base = -1;
520 pp->name = port_np->full_name;
521 }
522
523 return pdata;
524}
525
526static inline void dwapb_free_pdata_of(struct dwapb_platform_data *pdata)
527{
528 if (!IS_ENABLED(CONFIG_OF_GPIO) || !pdata)
529 return;
530
531 kfree(pdata->properties);
532 kfree(pdata);
533}
534
Jamie Iles7779b342014-02-25 17:01:01 -0600535static int dwapb_gpio_probe(struct platform_device *pdev)
536{
Weike Chen3d2613c2014-09-17 09:18:39 -0700537 unsigned int i;
Jamie Iles7779b342014-02-25 17:01:01 -0600538 struct resource *res;
539 struct dwapb_gpio *gpio;
Jamie Iles7779b342014-02-25 17:01:01 -0600540 int err;
Weike Chen3d2613c2014-09-17 09:18:39 -0700541 struct device *dev = &pdev->dev;
542 struct dwapb_platform_data *pdata = dev_get_platdata(dev);
543 bool is_pdata_alloc = !pdata;
Jamie Iles7779b342014-02-25 17:01:01 -0600544
Weike Chen3d2613c2014-09-17 09:18:39 -0700545 if (is_pdata_alloc) {
546 pdata = dwapb_gpio_get_pdata_of(dev);
547 if (IS_ERR(pdata))
548 return PTR_ERR(pdata);
549 }
Jamie Iles7779b342014-02-25 17:01:01 -0600550
Weike Chen3d2613c2014-09-17 09:18:39 -0700551 if (!pdata->nports) {
552 err = -ENODEV;
Jamie Iles7779b342014-02-25 17:01:01 -0600553 goto out_err;
554 }
Weike Chen3d2613c2014-09-17 09:18:39 -0700555
556 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
557 if (!gpio) {
558 err = -ENOMEM;
559 goto out_err;
560 }
561 gpio->dev = &pdev->dev;
562 gpio->nr_ports = pdata->nports;
563
564 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
Jamie Iles7779b342014-02-25 17:01:01 -0600565 sizeof(*gpio->ports), GFP_KERNEL);
566 if (!gpio->ports) {
567 err = -ENOMEM;
568 goto out_err;
569 }
570
571 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
572 gpio->regs = devm_ioremap_resource(&pdev->dev, res);
573 if (IS_ERR(gpio->regs)) {
574 err = PTR_ERR(gpio->regs);
575 goto out_err;
576 }
577
Weike Chen3d2613c2014-09-17 09:18:39 -0700578 for (i = 0; i < gpio->nr_ports; i++) {
579 err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
Jamie Iles7779b342014-02-25 17:01:01 -0600580 if (err)
581 goto out_unregister;
582 }
583 platform_set_drvdata(pdev, gpio);
584
Weike Chen3d2613c2014-09-17 09:18:39 -0700585 goto out_err;
Jamie Iles7779b342014-02-25 17:01:01 -0600586
587out_unregister:
588 dwapb_gpio_unregister(gpio);
589 dwapb_irq_teardown(gpio);
590
591out_err:
Weike Chen3d2613c2014-09-17 09:18:39 -0700592 if (is_pdata_alloc)
593 dwapb_free_pdata_of(pdata);
594
Jamie Iles7779b342014-02-25 17:01:01 -0600595 return err;
596}
597
598static int dwapb_gpio_remove(struct platform_device *pdev)
599{
600 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
601
602 dwapb_gpio_unregister(gpio);
603 dwapb_irq_teardown(gpio);
604
605 return 0;
606}
607
608static const struct of_device_id dwapb_of_match[] = {
609 { .compatible = "snps,dw-apb-gpio" },
610 { /* Sentinel */ }
611};
612MODULE_DEVICE_TABLE(of, dwapb_of_match);
613
Weike Chen1e960db2014-09-17 09:18:42 -0700614#ifdef CONFIG_PM_SLEEP
615static int dwapb_gpio_suspend(struct device *dev)
616{
617 struct platform_device *pdev = to_platform_device(dev);
618 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
619 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
620 unsigned long flags;
621 int i;
622
623 spin_lock_irqsave(&bgc->lock, flags);
624 for (i = 0; i < gpio->nr_ports; i++) {
625 unsigned int offset;
626 unsigned int idx = gpio->ports[i].idx;
627 struct dwapb_context *ctx = gpio->ports[i].ctx;
628
Linus Walleij58a3b922014-09-24 13:30:24 +0200629 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700630
631 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
632 ctx->dir = dwapb_read(gpio, offset);
633
634 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
635 ctx->data = dwapb_read(gpio, offset);
636
637 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
638 ctx->ext = dwapb_read(gpio, offset);
639
640 /* Only port A can provide interrupts */
641 if (idx == 0) {
642 ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
643 ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
644 ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
645 ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
646 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
647
648 /* Mask out interrupts */
649 dwapb_write(gpio, GPIO_INTMASK, 0xffffffff);
650 }
651 }
652 spin_unlock_irqrestore(&bgc->lock, flags);
653
654 return 0;
655}
656
657static int dwapb_gpio_resume(struct device *dev)
658{
659 struct platform_device *pdev = to_platform_device(dev);
660 struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
661 struct bgpio_chip *bgc = &gpio->ports[0].bgc;
662 unsigned long flags;
663 int i;
664
665 spin_lock_irqsave(&bgc->lock, flags);
666 for (i = 0; i < gpio->nr_ports; i++) {
667 unsigned int offset;
668 unsigned int idx = gpio->ports[i].idx;
669 struct dwapb_context *ctx = gpio->ports[i].ctx;
670
Linus Walleij58a3b922014-09-24 13:30:24 +0200671 BUG_ON(!ctx);
Weike Chen1e960db2014-09-17 09:18:42 -0700672
673 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
674 dwapb_write(gpio, offset, ctx->data);
675
676 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
677 dwapb_write(gpio, offset, ctx->dir);
678
679 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
680 dwapb_write(gpio, offset, ctx->ext);
681
682 /* Only port A can provide interrupts */
683 if (idx == 0) {
684 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
685 dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
686 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
687 dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
688 dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
689
690 /* Clear out spurious interrupts */
691 dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
692 }
693 }
694 spin_unlock_irqrestore(&bgc->lock, flags);
695
696 return 0;
697}
698#endif
699
700static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
701 dwapb_gpio_resume);
702
Jamie Iles7779b342014-02-25 17:01:01 -0600703static struct platform_driver dwapb_gpio_driver = {
704 .driver = {
705 .name = "gpio-dwapb",
706 .owner = THIS_MODULE,
Weike Chen1e960db2014-09-17 09:18:42 -0700707 .pm = &dwapb_gpio_pm_ops,
Jamie Iles7779b342014-02-25 17:01:01 -0600708 .of_match_table = of_match_ptr(dwapb_of_match),
709 },
710 .probe = dwapb_gpio_probe,
711 .remove = dwapb_gpio_remove,
712};
713
714module_platform_driver(dwapb_gpio_driver);
715
716MODULE_LICENSE("GPL");
717MODULE_AUTHOR("Jamie Iles");
718MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");