1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Jamie Iles
5 * All enquiries to support@picochip.com
7 #include <linux/acpi.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/ioport.h>
15 #include <linux/irq.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
25 #include "gpiolib-acpi.h"
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
40 #define GPIO_PORTA_DEBOUNCE 0x48
41 #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
47 #define DWAPB_DRIVER_NAME "gpio-dwapb"
48 #define DWAPB_MAX_PORTS 4
49 #define DWAPB_MAX_GPIOS 32
51 #define GPIO_EXT_PORT_STRIDE 0x04 /* register stride 32 bits */
52 #define GPIO_SWPORT_DR_STRIDE 0x0c /* register stride 3*32 bits */
53 #define GPIO_SWPORT_DDR_STRIDE 0x0c /* register stride 3*32 bits */
55 #define GPIO_REG_OFFSET_V1 0
56 #define GPIO_REG_OFFSET_V2 1
57 #define GPIO_REG_OFFSET_MASK BIT(0)
59 #define GPIO_INTMASK_V2 0x44
60 #define GPIO_INTTYPE_LEVEL_V2 0x34
61 #define GPIO_INT_POLARITY_V2 0x38
62 #define GPIO_INTSTATUS_V2 0x3c
63 #define GPIO_PORTA_EOI_V2 0x40
65 #define DWAPB_NR_CLOCKS 2
69 struct dwapb_port_property {
70 struct fwnode_handle *fwnode;
73 unsigned int gpio_base;
74 int irq[DWAPB_MAX_GPIOS];
77 struct dwapb_platform_data {
78 struct dwapb_port_property *properties;
82 #ifdef CONFIG_PM_SLEEP
83 /* Store GPIO context across system-wide suspend/resume transitions */
84 struct dwapb_context {
97 struct dwapb_gpio_port_irqchip {
99 unsigned int irq[DWAPB_MAX_GPIOS];
102 struct dwapb_gpio_port {
104 struct dwapb_gpio_port_irqchip *pirq;
105 struct dwapb_gpio *gpio;
106 #ifdef CONFIG_PM_SLEEP
107 struct dwapb_context *ctx;
111 #define to_dwapb_gpio(_gc) \
112 (container_of(_gc, struct dwapb_gpio_port, gc)->gpio)
117 struct dwapb_gpio_port *ports;
118 unsigned int nr_ports;
120 struct reset_control *rst;
121 struct clk_bulk_data clks[DWAPB_NR_CLOCKS];
124 static inline u32 gpio_reg_v2_convert(unsigned int offset)
128 return GPIO_INTMASK_V2;
129 case GPIO_INTTYPE_LEVEL:
130 return GPIO_INTTYPE_LEVEL_V2;
131 case GPIO_INT_POLARITY:
132 return GPIO_INT_POLARITY_V2;
134 return GPIO_INTSTATUS_V2;
136 return GPIO_PORTA_EOI_V2;
142 static inline u32 gpio_reg_convert(struct dwapb_gpio *gpio, unsigned int offset)
144 if ((gpio->flags & GPIO_REG_OFFSET_MASK) == GPIO_REG_OFFSET_V2)
145 return gpio_reg_v2_convert(offset);
150 static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
152 struct gpio_chip *gc = &gpio->ports[0].gc;
153 void __iomem *reg_base = gpio->regs;
155 return gc->read_reg(reg_base + gpio_reg_convert(gpio, offset));
158 static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
161 struct gpio_chip *gc = &gpio->ports[0].gc;
162 void __iomem *reg_base = gpio->regs;
164 gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
167 static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsigned int offs)
169 struct dwapb_gpio_port *port;
172 for (i = 0; i < gpio->nr_ports; i++) {
173 port = &gpio->ports[i];
174 if (port->idx == offs / DWAPB_MAX_GPIOS)
181 static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
183 struct dwapb_gpio_port *port = dwapb_offs_to_port(gpio, offs);
184 struct gpio_chip *gc;
192 pol = dwapb_read(gpio, GPIO_INT_POLARITY);
193 /* Just read the current value right out of the data register */
194 val = gc->get(gc, offs % DWAPB_MAX_GPIOS);
200 dwapb_write(gpio, GPIO_INT_POLARITY, pol);
203 static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
205 struct gpio_chip *gc = &gpio->ports[0].gc;
206 unsigned long irq_status;
207 irq_hw_number_t hwirq;
209 irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
210 for_each_set_bit(hwirq, &irq_status, DWAPB_MAX_GPIOS) {
211 int gpio_irq = irq_find_mapping(gc->irq.domain, hwirq);
212 u32 irq_type = irq_get_trigger_type(gpio_irq);
214 generic_handle_irq(gpio_irq);
216 if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
217 dwapb_toggle_trigger(gpio, hwirq);
223 static void dwapb_irq_handler(struct irq_desc *desc)
225 struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
226 struct irq_chip *chip = irq_desc_get_chip(desc);
228 chained_irq_enter(chip, desc);
230 chained_irq_exit(chip, desc);
233 static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
235 return IRQ_RETVAL(dwapb_do_irq(dev_id));
238 static void dwapb_irq_ack(struct irq_data *d)
240 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
241 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
242 u32 val = BIT(irqd_to_hwirq(d));
245 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
246 dwapb_write(gpio, GPIO_PORTA_EOI, val);
247 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
250 static void dwapb_irq_mask(struct irq_data *d)
252 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
253 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
254 irq_hw_number_t hwirq = irqd_to_hwirq(d);
258 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
259 val = dwapb_read(gpio, GPIO_INTMASK) | BIT(hwirq);
260 dwapb_write(gpio, GPIO_INTMASK, val);
261 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
263 gpiochip_disable_irq(gc, hwirq);
266 static void dwapb_irq_unmask(struct irq_data *d)
268 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
269 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
270 irq_hw_number_t hwirq = irqd_to_hwirq(d);
274 gpiochip_enable_irq(gc, hwirq);
276 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
277 val = dwapb_read(gpio, GPIO_INTMASK) & ~BIT(hwirq);
278 dwapb_write(gpio, GPIO_INTMASK, val);
279 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
282 static void dwapb_irq_enable(struct irq_data *d)
284 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
285 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
286 irq_hw_number_t hwirq = irqd_to_hwirq(d);
290 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
291 val = dwapb_read(gpio, GPIO_INTEN) | BIT(hwirq);
292 dwapb_write(gpio, GPIO_INTEN, val);
293 val = dwapb_read(gpio, GPIO_INTMASK) & ~BIT(hwirq);
294 dwapb_write(gpio, GPIO_INTMASK, val);
295 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
298 static void dwapb_irq_disable(struct irq_data *d)
300 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
301 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
302 irq_hw_number_t hwirq = irqd_to_hwirq(d);
306 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
307 val = dwapb_read(gpio, GPIO_INTMASK) | BIT(hwirq);
308 dwapb_write(gpio, GPIO_INTMASK, val);
309 val = dwapb_read(gpio, GPIO_INTEN) & ~BIT(hwirq);
310 dwapb_write(gpio, GPIO_INTEN, val);
311 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
314 static int dwapb_irq_set_type(struct irq_data *d, u32 type)
316 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
317 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
318 irq_hw_number_t bit = irqd_to_hwirq(d);
319 unsigned long level, polarity, flags;
321 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
322 level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
323 polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
326 case IRQ_TYPE_EDGE_BOTH:
328 dwapb_toggle_trigger(gpio, bit);
330 case IRQ_TYPE_EDGE_RISING:
332 polarity |= BIT(bit);
334 case IRQ_TYPE_EDGE_FALLING:
336 polarity &= ~BIT(bit);
338 case IRQ_TYPE_LEVEL_HIGH:
340 polarity |= BIT(bit);
342 case IRQ_TYPE_LEVEL_LOW:
344 polarity &= ~BIT(bit);
348 if (type & IRQ_TYPE_LEVEL_MASK)
349 irq_set_handler_locked(d, handle_level_irq);
350 else if (type & IRQ_TYPE_EDGE_BOTH)
351 irq_set_handler_locked(d, handle_edge_irq);
353 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
354 if (type != IRQ_TYPE_EDGE_BOTH)
355 dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
356 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
361 #ifdef CONFIG_PM_SLEEP
362 static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
364 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
365 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
366 struct dwapb_context *ctx = gpio->ports[0].ctx;
367 irq_hw_number_t bit = irqd_to_hwirq(d);
370 ctx->wake_en |= BIT(bit);
372 ctx->wake_en &= ~BIT(bit);
377 #define dwapb_irq_set_wake NULL
380 static const struct irq_chip dwapb_irq_chip = {
381 .name = DWAPB_DRIVER_NAME,
382 .irq_ack = dwapb_irq_ack,
383 .irq_mask = dwapb_irq_mask,
384 .irq_unmask = dwapb_irq_unmask,
385 .irq_set_type = dwapb_irq_set_type,
386 .irq_enable = dwapb_irq_enable,
387 .irq_disable = dwapb_irq_disable,
388 .irq_set_wake = dwapb_irq_set_wake,
389 .flags = IRQCHIP_IMMUTABLE,
390 GPIOCHIP_IRQ_RESOURCE_HELPERS,
393 static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
394 unsigned offset, unsigned debounce)
396 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
397 struct dwapb_gpio *gpio = port->gpio;
398 unsigned long flags, val_deb;
399 unsigned long mask = BIT(offset);
401 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
403 val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
408 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb);
410 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
415 static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
416 unsigned long config)
420 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
423 debounce = pinconf_to_config_argument(config);
424 return dwapb_gpio_set_debounce(gc, offset, debounce);
427 static int dwapb_convert_irqs(struct dwapb_gpio_port_irqchip *pirq,
428 struct dwapb_port_property *pp)
432 /* Group all available IRQs into an array of parental IRQs. */
433 for (i = 0; i < pp->ngpio; ++i) {
437 pirq->irq[pirq->nr_irqs++] = pp->irq[i];
440 return pirq->nr_irqs ? 0 : -ENOENT;
443 static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
444 struct dwapb_gpio_port *port,
445 struct dwapb_port_property *pp)
447 struct dwapb_gpio_port_irqchip *pirq;
448 struct gpio_chip *gc = &port->gc;
449 struct gpio_irq_chip *girq;
452 pirq = devm_kzalloc(gpio->dev, sizeof(*pirq), GFP_KERNEL);
456 if (dwapb_convert_irqs(pirq, pp)) {
457 dev_warn(gpio->dev, "no IRQ for port%d\n", pp->idx);
462 girq->handler = handle_bad_irq;
463 girq->default_type = IRQ_TYPE_NONE;
468 * Intel ACPI-based platforms mostly have the DesignWare APB GPIO
469 * IRQ lane shared between several devices. In that case the parental
470 * IRQ has to be handled in the shared way so to be properly delivered
471 * to all the connected devices.
473 if (has_acpi_companion(gpio->dev)) {
474 girq->num_parents = 0;
475 girq->parents = NULL;
476 girq->parent_handler = NULL;
478 err = devm_request_irq(gpio->dev, pp->irq[0],
479 dwapb_irq_handler_mfd,
480 IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
482 dev_err(gpio->dev, "error requesting IRQ\n");
486 girq->num_parents = pirq->nr_irqs;
487 girq->parents = pirq->irq;
488 girq->parent_handler_data = gpio;
489 girq->parent_handler = dwapb_irq_handler;
492 gpio_irq_chip_set_chip(girq, &dwapb_irq_chip);
497 devm_kfree(gpio->dev, pirq);
500 static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
501 struct dwapb_port_property *pp,
504 struct dwapb_gpio_port *port;
505 void __iomem *dat, *set, *dirout;
508 port = &gpio->ports[offs];
512 #ifdef CONFIG_PM_SLEEP
513 port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
518 dat = gpio->regs + GPIO_EXT_PORTA + pp->idx * GPIO_EXT_PORT_STRIDE;
519 set = gpio->regs + GPIO_SWPORTA_DR + pp->idx * GPIO_SWPORT_DR_STRIDE;
520 dirout = gpio->regs + GPIO_SWPORTA_DDR + pp->idx * GPIO_SWPORT_DDR_STRIDE;
522 /* This registers 32 GPIO lines per port */
523 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
526 dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
531 port->gc.fwnode = pp->fwnode;
532 port->gc.ngpio = pp->ngpio;
533 port->gc.base = pp->gpio_base;
535 /* Only port A support debounce */
537 port->gc.set_config = dwapb_gpio_set_config;
539 /* Only port A can provide interrupts in all configurations of the IP */
541 dwapb_configure_irqs(gpio, port, pp);
543 err = devm_gpiochip_add_data(gpio->dev, &port->gc, port);
545 dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
553 static void dwapb_get_irq(struct device *dev, struct fwnode_handle *fwnode,
554 struct dwapb_port_property *pp)
558 for (j = 0; j < pp->ngpio; j++) {
559 if (has_acpi_companion(dev))
560 irq = platform_get_irq_optional(to_platform_device(dev), j);
562 irq = fwnode_irq_get(fwnode, j);
568 static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
570 struct fwnode_handle *fwnode;
571 struct dwapb_platform_data *pdata;
572 struct dwapb_port_property *pp;
576 nports = device_get_child_node_count(dev);
578 return ERR_PTR(-ENODEV);
580 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
582 return ERR_PTR(-ENOMEM);
584 pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
585 if (!pdata->properties)
586 return ERR_PTR(-ENOMEM);
588 pdata->nports = nports;
591 device_for_each_child_node(dev, fwnode) {
592 pp = &pdata->properties[i++];
595 if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
596 pp->idx >= DWAPB_MAX_PORTS) {
598 "missing/invalid port index for port%d\n", i);
599 fwnode_handle_put(fwnode);
600 return ERR_PTR(-EINVAL);
603 if (fwnode_property_read_u32(fwnode, "ngpios", &pp->ngpio) &&
604 fwnode_property_read_u32(fwnode, "snps,nr-gpios", &pp->ngpio)) {
606 "failed to get number of gpios for port%d\n",
608 pp->ngpio = DWAPB_MAX_GPIOS;
613 /* For internal use only, new platforms mustn't exercise this */
614 if (is_software_node(fwnode))
615 fwnode_property_read_u32(fwnode, "gpio-base", &pp->gpio_base);
618 * Only port A can provide interrupts in all configurations of
622 dwapb_get_irq(dev, fwnode, pp);
628 static void dwapb_assert_reset(void *data)
630 struct dwapb_gpio *gpio = data;
632 reset_control_assert(gpio->rst);
635 static int dwapb_get_reset(struct dwapb_gpio *gpio)
639 gpio->rst = devm_reset_control_get_optional_shared(gpio->dev, NULL);
640 if (IS_ERR(gpio->rst))
641 return dev_err_probe(gpio->dev, PTR_ERR(gpio->rst),
642 "Cannot get reset descriptor\n");
644 err = reset_control_deassert(gpio->rst);
646 dev_err(gpio->dev, "Cannot deassert reset lane\n");
650 return devm_add_action_or_reset(gpio->dev, dwapb_assert_reset, gpio);
653 static void dwapb_disable_clks(void *data)
655 struct dwapb_gpio *gpio = data;
657 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
660 static int dwapb_get_clks(struct dwapb_gpio *gpio)
664 /* Optional bus and debounce clocks */
665 gpio->clks[0].id = "bus";
666 gpio->clks[1].id = "db";
667 err = devm_clk_bulk_get_optional(gpio->dev, DWAPB_NR_CLOCKS,
670 return dev_err_probe(gpio->dev, err,
671 "Cannot get APB/Debounce clocks\n");
673 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
675 dev_err(gpio->dev, "Cannot enable APB/Debounce clocks\n");
679 return devm_add_action_or_reset(gpio->dev, dwapb_disable_clks, gpio);
682 static const struct of_device_id dwapb_of_match[] = {
683 { .compatible = "snps,dw-apb-gpio", .data = (void *)GPIO_REG_OFFSET_V1},
684 { .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
687 MODULE_DEVICE_TABLE(of, dwapb_of_match);
689 static const struct acpi_device_id dwapb_acpi_match[] = {
690 {"HISI0181", GPIO_REG_OFFSET_V1},
691 {"APMC0D07", GPIO_REG_OFFSET_V1},
692 {"APMC0D81", GPIO_REG_OFFSET_V2},
695 MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
697 static int dwapb_gpio_probe(struct platform_device *pdev)
700 struct dwapb_gpio *gpio;
702 struct dwapb_platform_data *pdata;
703 struct device *dev = &pdev->dev;
705 pdata = dwapb_gpio_get_pdata(dev);
707 return PTR_ERR(pdata);
709 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
713 gpio->dev = &pdev->dev;
714 gpio->nr_ports = pdata->nports;
716 err = dwapb_get_reset(gpio);
720 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
721 sizeof(*gpio->ports), GFP_KERNEL);
725 gpio->regs = devm_platform_ioremap_resource(pdev, 0);
726 if (IS_ERR(gpio->regs))
727 return PTR_ERR(gpio->regs);
729 err = dwapb_get_clks(gpio);
733 gpio->flags = (uintptr_t)device_get_match_data(dev);
735 for (i = 0; i < gpio->nr_ports; i++) {
736 err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
741 platform_set_drvdata(pdev, gpio);
746 #ifdef CONFIG_PM_SLEEP
747 static int dwapb_gpio_suspend(struct device *dev)
749 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
750 struct gpio_chip *gc = &gpio->ports[0].gc;
754 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
755 for (i = 0; i < gpio->nr_ports; i++) {
757 unsigned int idx = gpio->ports[i].idx;
758 struct dwapb_context *ctx = gpio->ports[i].ctx;
760 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
761 ctx->dir = dwapb_read(gpio, offset);
763 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
764 ctx->data = dwapb_read(gpio, offset);
766 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
767 ctx->ext = dwapb_read(gpio, offset);
769 /* Only port A can provide interrupts */
771 ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
772 ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
773 ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
774 ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
775 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
777 /* Mask out interrupts */
778 dwapb_write(gpio, GPIO_INTMASK, ~ctx->wake_en);
781 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
783 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
788 static int dwapb_gpio_resume(struct device *dev)
790 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
791 struct gpio_chip *gc = &gpio->ports[0].gc;
795 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
797 dev_err(gpio->dev, "Cannot reenable APB/Debounce clocks\n");
801 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
802 for (i = 0; i < gpio->nr_ports; i++) {
804 unsigned int idx = gpio->ports[i].idx;
805 struct dwapb_context *ctx = gpio->ports[i].ctx;
807 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
808 dwapb_write(gpio, offset, ctx->data);
810 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
811 dwapb_write(gpio, offset, ctx->dir);
813 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
814 dwapb_write(gpio, offset, ctx->ext);
816 /* Only port A can provide interrupts */
818 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
819 dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
820 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
821 dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
822 dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
824 /* Clear out spurious interrupts */
825 dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
828 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
834 static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
837 static struct platform_driver dwapb_gpio_driver = {
839 .name = DWAPB_DRIVER_NAME,
840 .pm = &dwapb_gpio_pm_ops,
841 .of_match_table = dwapb_of_match,
842 .acpi_match_table = dwapb_acpi_match,
844 .probe = dwapb_gpio_probe,
847 module_platform_driver(dwapb_gpio_driver);
849 MODULE_LICENSE("GPL");
850 MODULE_AUTHOR("Jamie Iles");
851 MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");
852 MODULE_ALIAS("platform:" DWAPB_DRIVER_NAME);