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);
289 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
290 val = dwapb_read(gpio, GPIO_INTEN);
291 val |= BIT(irqd_to_hwirq(d));
292 dwapb_write(gpio, GPIO_INTEN, val);
293 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
296 static void dwapb_irq_disable(struct irq_data *d)
298 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
299 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
303 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
304 val = dwapb_read(gpio, GPIO_INTEN);
305 val &= ~BIT(irqd_to_hwirq(d));
306 dwapb_write(gpio, GPIO_INTEN, val);
307 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
310 static int dwapb_irq_set_type(struct irq_data *d, u32 type)
312 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
313 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
314 irq_hw_number_t bit = irqd_to_hwirq(d);
315 unsigned long level, polarity, flags;
317 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
318 level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
319 polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
322 case IRQ_TYPE_EDGE_BOTH:
324 dwapb_toggle_trigger(gpio, bit);
326 case IRQ_TYPE_EDGE_RISING:
328 polarity |= BIT(bit);
330 case IRQ_TYPE_EDGE_FALLING:
332 polarity &= ~BIT(bit);
334 case IRQ_TYPE_LEVEL_HIGH:
336 polarity |= BIT(bit);
338 case IRQ_TYPE_LEVEL_LOW:
340 polarity &= ~BIT(bit);
344 if (type & IRQ_TYPE_LEVEL_MASK)
345 irq_set_handler_locked(d, handle_level_irq);
346 else if (type & IRQ_TYPE_EDGE_BOTH)
347 irq_set_handler_locked(d, handle_edge_irq);
349 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
350 if (type != IRQ_TYPE_EDGE_BOTH)
351 dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
352 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
357 #ifdef CONFIG_PM_SLEEP
358 static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
360 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
361 struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
362 struct dwapb_context *ctx = gpio->ports[0].ctx;
363 irq_hw_number_t bit = irqd_to_hwirq(d);
366 ctx->wake_en |= BIT(bit);
368 ctx->wake_en &= ~BIT(bit);
373 #define dwapb_irq_set_wake NULL
376 static const struct irq_chip dwapb_irq_chip = {
377 .name = DWAPB_DRIVER_NAME,
378 .irq_ack = dwapb_irq_ack,
379 .irq_mask = dwapb_irq_mask,
380 .irq_unmask = dwapb_irq_unmask,
381 .irq_set_type = dwapb_irq_set_type,
382 .irq_enable = dwapb_irq_enable,
383 .irq_disable = dwapb_irq_disable,
384 .irq_set_wake = dwapb_irq_set_wake,
385 .flags = IRQCHIP_IMMUTABLE,
386 GPIOCHIP_IRQ_RESOURCE_HELPERS,
389 static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
390 unsigned offset, unsigned debounce)
392 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
393 struct dwapb_gpio *gpio = port->gpio;
394 unsigned long flags, val_deb;
395 unsigned long mask = BIT(offset);
397 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
399 val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
404 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb);
406 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
411 static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
412 unsigned long config)
416 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
419 debounce = pinconf_to_config_argument(config);
420 return dwapb_gpio_set_debounce(gc, offset, debounce);
423 static int dwapb_convert_irqs(struct dwapb_gpio_port_irqchip *pirq,
424 struct dwapb_port_property *pp)
428 /* Group all available IRQs into an array of parental IRQs. */
429 for (i = 0; i < pp->ngpio; ++i) {
433 pirq->irq[pirq->nr_irqs++] = pp->irq[i];
436 return pirq->nr_irqs ? 0 : -ENOENT;
439 static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
440 struct dwapb_gpio_port *port,
441 struct dwapb_port_property *pp)
443 struct dwapb_gpio_port_irqchip *pirq;
444 struct gpio_chip *gc = &port->gc;
445 struct gpio_irq_chip *girq;
448 pirq = devm_kzalloc(gpio->dev, sizeof(*pirq), GFP_KERNEL);
452 if (dwapb_convert_irqs(pirq, pp)) {
453 dev_warn(gpio->dev, "no IRQ for port%d\n", pp->idx);
458 girq->handler = handle_bad_irq;
459 girq->default_type = IRQ_TYPE_NONE;
464 * Intel ACPI-based platforms mostly have the DesignWare APB GPIO
465 * IRQ lane shared between several devices. In that case the parental
466 * IRQ has to be handled in the shared way so to be properly delivered
467 * to all the connected devices.
469 if (has_acpi_companion(gpio->dev)) {
470 girq->num_parents = 0;
471 girq->parents = NULL;
472 girq->parent_handler = NULL;
474 err = devm_request_irq(gpio->dev, pp->irq[0],
475 dwapb_irq_handler_mfd,
476 IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
478 dev_err(gpio->dev, "error requesting IRQ\n");
482 girq->num_parents = pirq->nr_irqs;
483 girq->parents = pirq->irq;
484 girq->parent_handler_data = gpio;
485 girq->parent_handler = dwapb_irq_handler;
488 gpio_irq_chip_set_chip(girq, &dwapb_irq_chip);
493 devm_kfree(gpio->dev, pirq);
496 static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
497 struct dwapb_port_property *pp,
500 struct dwapb_gpio_port *port;
501 void __iomem *dat, *set, *dirout;
504 port = &gpio->ports[offs];
508 #ifdef CONFIG_PM_SLEEP
509 port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
514 dat = gpio->regs + GPIO_EXT_PORTA + pp->idx * GPIO_EXT_PORT_STRIDE;
515 set = gpio->regs + GPIO_SWPORTA_DR + pp->idx * GPIO_SWPORT_DR_STRIDE;
516 dirout = gpio->regs + GPIO_SWPORTA_DDR + pp->idx * GPIO_SWPORT_DDR_STRIDE;
518 /* This registers 32 GPIO lines per port */
519 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
522 dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
527 port->gc.fwnode = pp->fwnode;
528 port->gc.ngpio = pp->ngpio;
529 port->gc.base = pp->gpio_base;
531 /* Only port A support debounce */
533 port->gc.set_config = dwapb_gpio_set_config;
535 /* Only port A can provide interrupts in all configurations of the IP */
537 dwapb_configure_irqs(gpio, port, pp);
539 err = devm_gpiochip_add_data(gpio->dev, &port->gc, port);
541 dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
549 static void dwapb_get_irq(struct device *dev, struct fwnode_handle *fwnode,
550 struct dwapb_port_property *pp)
554 for (j = 0; j < pp->ngpio; j++) {
555 if (has_acpi_companion(dev))
556 irq = platform_get_irq_optional(to_platform_device(dev), j);
558 irq = fwnode_irq_get(fwnode, j);
564 static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
566 struct fwnode_handle *fwnode;
567 struct dwapb_platform_data *pdata;
568 struct dwapb_port_property *pp;
572 nports = device_get_child_node_count(dev);
574 return ERR_PTR(-ENODEV);
576 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
578 return ERR_PTR(-ENOMEM);
580 pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
581 if (!pdata->properties)
582 return ERR_PTR(-ENOMEM);
584 pdata->nports = nports;
587 device_for_each_child_node(dev, fwnode) {
588 pp = &pdata->properties[i++];
591 if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
592 pp->idx >= DWAPB_MAX_PORTS) {
594 "missing/invalid port index for port%d\n", i);
595 fwnode_handle_put(fwnode);
596 return ERR_PTR(-EINVAL);
599 if (fwnode_property_read_u32(fwnode, "ngpios", &pp->ngpio) &&
600 fwnode_property_read_u32(fwnode, "snps,nr-gpios", &pp->ngpio)) {
602 "failed to get number of gpios for port%d\n",
604 pp->ngpio = DWAPB_MAX_GPIOS;
609 /* For internal use only, new platforms mustn't exercise this */
610 if (is_software_node(fwnode))
611 fwnode_property_read_u32(fwnode, "gpio-base", &pp->gpio_base);
614 * Only port A can provide interrupts in all configurations of
618 dwapb_get_irq(dev, fwnode, pp);
624 static void dwapb_assert_reset(void *data)
626 struct dwapb_gpio *gpio = data;
628 reset_control_assert(gpio->rst);
631 static int dwapb_get_reset(struct dwapb_gpio *gpio)
635 gpio->rst = devm_reset_control_get_optional_shared(gpio->dev, NULL);
636 if (IS_ERR(gpio->rst))
637 return dev_err_probe(gpio->dev, PTR_ERR(gpio->rst),
638 "Cannot get reset descriptor\n");
640 err = reset_control_deassert(gpio->rst);
642 dev_err(gpio->dev, "Cannot deassert reset lane\n");
646 return devm_add_action_or_reset(gpio->dev, dwapb_assert_reset, gpio);
649 static void dwapb_disable_clks(void *data)
651 struct dwapb_gpio *gpio = data;
653 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
656 static int dwapb_get_clks(struct dwapb_gpio *gpio)
660 /* Optional bus and debounce clocks */
661 gpio->clks[0].id = "bus";
662 gpio->clks[1].id = "db";
663 err = devm_clk_bulk_get_optional(gpio->dev, DWAPB_NR_CLOCKS,
666 return dev_err_probe(gpio->dev, err,
667 "Cannot get APB/Debounce clocks\n");
669 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
671 dev_err(gpio->dev, "Cannot enable APB/Debounce clocks\n");
675 return devm_add_action_or_reset(gpio->dev, dwapb_disable_clks, gpio);
678 static const struct of_device_id dwapb_of_match[] = {
679 { .compatible = "snps,dw-apb-gpio", .data = (void *)GPIO_REG_OFFSET_V1},
680 { .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
683 MODULE_DEVICE_TABLE(of, dwapb_of_match);
685 static const struct acpi_device_id dwapb_acpi_match[] = {
686 {"HISI0181", GPIO_REG_OFFSET_V1},
687 {"APMC0D07", GPIO_REG_OFFSET_V1},
688 {"APMC0D81", GPIO_REG_OFFSET_V2},
691 MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
693 static int dwapb_gpio_probe(struct platform_device *pdev)
696 struct dwapb_gpio *gpio;
698 struct dwapb_platform_data *pdata;
699 struct device *dev = &pdev->dev;
701 pdata = dwapb_gpio_get_pdata(dev);
703 return PTR_ERR(pdata);
705 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
709 gpio->dev = &pdev->dev;
710 gpio->nr_ports = pdata->nports;
712 err = dwapb_get_reset(gpio);
716 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
717 sizeof(*gpio->ports), GFP_KERNEL);
721 gpio->regs = devm_platform_ioremap_resource(pdev, 0);
722 if (IS_ERR(gpio->regs))
723 return PTR_ERR(gpio->regs);
725 err = dwapb_get_clks(gpio);
729 gpio->flags = (uintptr_t)device_get_match_data(dev);
731 for (i = 0; i < gpio->nr_ports; i++) {
732 err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
737 platform_set_drvdata(pdev, gpio);
742 #ifdef CONFIG_PM_SLEEP
743 static int dwapb_gpio_suspend(struct device *dev)
745 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
746 struct gpio_chip *gc = &gpio->ports[0].gc;
750 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
751 for (i = 0; i < gpio->nr_ports; i++) {
753 unsigned int idx = gpio->ports[i].idx;
754 struct dwapb_context *ctx = gpio->ports[i].ctx;
756 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
757 ctx->dir = dwapb_read(gpio, offset);
759 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
760 ctx->data = dwapb_read(gpio, offset);
762 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
763 ctx->ext = dwapb_read(gpio, offset);
765 /* Only port A can provide interrupts */
767 ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
768 ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
769 ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
770 ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
771 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
773 /* Mask out interrupts */
774 dwapb_write(gpio, GPIO_INTMASK, ~ctx->wake_en);
777 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
779 clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
784 static int dwapb_gpio_resume(struct device *dev)
786 struct dwapb_gpio *gpio = dev_get_drvdata(dev);
787 struct gpio_chip *gc = &gpio->ports[0].gc;
791 err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
793 dev_err(gpio->dev, "Cannot reenable APB/Debounce clocks\n");
797 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
798 for (i = 0; i < gpio->nr_ports; i++) {
800 unsigned int idx = gpio->ports[i].idx;
801 struct dwapb_context *ctx = gpio->ports[i].ctx;
803 offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
804 dwapb_write(gpio, offset, ctx->data);
806 offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
807 dwapb_write(gpio, offset, ctx->dir);
809 offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
810 dwapb_write(gpio, offset, ctx->ext);
812 /* Only port A can provide interrupts */
814 dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
815 dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
816 dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
817 dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
818 dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
820 /* Clear out spurious interrupts */
821 dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
824 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
830 static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
833 static struct platform_driver dwapb_gpio_driver = {
835 .name = DWAPB_DRIVER_NAME,
836 .pm = &dwapb_gpio_pm_ops,
837 .of_match_table = dwapb_of_match,
838 .acpi_match_table = dwapb_acpi_match,
840 .probe = dwapb_gpio_probe,
843 module_platform_driver(dwapb_gpio_driver);
845 MODULE_LICENSE("GPL");
846 MODULE_AUTHOR("Jamie Iles");
847 MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");
848 MODULE_ALIAS("platform:" DWAPB_DRIVER_NAME);