2 * GPIO interface for Intel Sodaville SoCs.
4 * Copyright (c) 2010, 2011 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License 2 as published
8 * by the Free Software Foundation.
12 #include <linux/errno.h>
13 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/gpio/driver.h>
24 #define DRV_NAME "sdv_gpio"
25 #define SDV_NUM_PUB_GPIOS 12
26 #define PCI_DEVICE_ID_SDV_GPIO 0x2e67
40 struct sdv_gpio_chip_data
{
42 void __iomem
*gpio_pub_base
;
43 struct irq_domain
*id
;
44 struct irq_chip_generic
*gc
;
45 struct gpio_chip chip
;
48 static int sdv_gpio_pub_set_type(struct irq_data
*d
, unsigned int type
)
50 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
51 struct sdv_gpio_chip_data
*sd
= gc
->private;
52 void __iomem
*type_reg
;
56 type_reg
= sd
->gpio_pub_base
+ GPIT1R0
;
58 type_reg
= sd
->gpio_pub_base
+ GPIT1R1
;
60 reg
= readl(type_reg
);
63 case IRQ_TYPE_LEVEL_HIGH
:
64 reg
&= ~BIT(4 * (d
->hwirq
% 8));
67 case IRQ_TYPE_LEVEL_LOW
:
68 reg
|= BIT(4 * (d
->hwirq
% 8));
75 writel(reg
, type_reg
);
79 static irqreturn_t
sdv_gpio_pub_irq_handler(int irq
, void *data
)
81 struct sdv_gpio_chip_data
*sd
= data
;
82 u32 irq_stat
= readl(sd
->gpio_pub_base
+ GPSTR
);
84 irq_stat
&= readl(sd
->gpio_pub_base
+ GPIO_INT
);
89 u32 irq_bit
= __fls(irq_stat
);
91 irq_stat
&= ~BIT(irq_bit
);
92 generic_handle_irq(irq_find_mapping(sd
->id
, irq_bit
));
98 static int sdv_xlate(struct irq_domain
*h
, struct device_node
*node
,
99 const u32
*intspec
, u32 intsize
, irq_hw_number_t
*out_hwirq
,
104 if (node
!= irq_domain_get_of_node(h
))
117 case IRQ_TYPE_LEVEL_LOW
:
118 case IRQ_TYPE_LEVEL_HIGH
:
127 static const struct irq_domain_ops irq_domain_sdv_ops
= {
131 static int sdv_register_irqsupport(struct sdv_gpio_chip_data
*sd
,
132 struct pci_dev
*pdev
)
134 struct irq_chip_type
*ct
;
137 sd
->irq_base
= irq_alloc_descs(-1, 0, SDV_NUM_PUB_GPIOS
, -1);
138 if (sd
->irq_base
< 0)
141 /* mask + ACK all interrupt sources */
142 writel(0, sd
->gpio_pub_base
+ GPIO_INT
);
143 writel((1 << 11) - 1, sd
->gpio_pub_base
+ GPSTR
);
145 ret
= request_irq(pdev
->irq
, sdv_gpio_pub_irq_handler
, IRQF_SHARED
,
151 * This gpio irq controller latches level irqs. Testing shows that if
152 * we unmask & ACK the IRQ before the source of the interrupt is gone
153 * then the interrupt is active again.
155 sd
->gc
= irq_alloc_generic_chip("sdv-gpio", 1, sd
->irq_base
,
156 sd
->gpio_pub_base
, handle_fasteoi_irq
);
162 sd
->gc
->private = sd
;
163 ct
= sd
->gc
->chip_types
;
164 ct
->type
= IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
;
165 ct
->regs
.eoi
= GPSTR
;
166 ct
->regs
.mask
= GPIO_INT
;
167 ct
->chip
.irq_mask
= irq_gc_mask_clr_bit
;
168 ct
->chip
.irq_unmask
= irq_gc_mask_set_bit
;
169 ct
->chip
.irq_eoi
= irq_gc_eoi
;
170 ct
->chip
.irq_set_type
= sdv_gpio_pub_set_type
;
172 irq_setup_generic_chip(sd
->gc
, IRQ_MSK(SDV_NUM_PUB_GPIOS
),
173 IRQ_GC_INIT_MASK_CACHE
, IRQ_NOREQUEST
,
174 IRQ_LEVEL
| IRQ_NOPROBE
);
176 sd
->id
= irq_domain_add_legacy(pdev
->dev
.of_node
, SDV_NUM_PUB_GPIOS
,
177 sd
->irq_base
, 0, &irq_domain_sdv_ops
, sd
);
184 free_irq(pdev
->irq
, sd
);
186 irq_free_descs(sd
->irq_base
, SDV_NUM_PUB_GPIOS
);
190 static int sdv_gpio_probe(struct pci_dev
*pdev
,
191 const struct pci_device_id
*pci_id
)
193 struct sdv_gpio_chip_data
*sd
;
200 sd
= kzalloc(sizeof(struct sdv_gpio_chip_data
), GFP_KERNEL
);
203 ret
= pci_enable_device(pdev
);
205 dev_err(&pdev
->dev
, "can't enable device.\n");
209 ret
= pci_request_region(pdev
, GPIO_BAR
, DRV_NAME
);
211 dev_err(&pdev
->dev
, "can't alloc PCI BAR #%d\n", GPIO_BAR
);
215 addr
= pci_resource_start(pdev
, GPIO_BAR
);
220 sd
->gpio_pub_base
= ioremap(addr
, pci_resource_len(pdev
, GPIO_BAR
));
222 prop
= of_get_property(pdev
->dev
.of_node
, "intel,muxctl", &len
);
223 if (prop
&& len
== 4) {
224 mux_val
= of_read_number(prop
, 1);
225 writel(mux_val
, sd
->gpio_pub_base
+ GPMUXCTL
);
228 ret
= bgpio_init(&sd
->chip
, &pdev
->dev
, 4,
229 sd
->gpio_pub_base
+ GPINR
, sd
->gpio_pub_base
+ GPOUTR
,
230 NULL
, sd
->gpio_pub_base
+ GPOER
, NULL
, 0);
233 sd
->chip
.ngpio
= SDV_NUM_PUB_GPIOS
;
235 ret
= gpiochip_add_data(&sd
->chip
, sd
);
237 dev_err(&pdev
->dev
, "gpiochip_add() failed.\n");
241 ret
= sdv_register_irqsupport(sd
, pdev
);
245 pci_set_drvdata(pdev
, sd
);
246 dev_info(&pdev
->dev
, "Sodaville GPIO driver registered.\n");
250 iounmap(sd
->gpio_pub_base
);
252 pci_release_region(pdev
, GPIO_BAR
);
254 pci_disable_device(pdev
);
260 static void sdv_gpio_remove(struct pci_dev
*pdev
)
262 struct sdv_gpio_chip_data
*sd
= pci_get_drvdata(pdev
);
264 free_irq(pdev
->irq
, sd
);
265 irq_free_descs(sd
->irq_base
, SDV_NUM_PUB_GPIOS
);
267 gpiochip_remove(&sd
->chip
);
268 pci_release_region(pdev
, GPIO_BAR
);
269 iounmap(sd
->gpio_pub_base
);
270 pci_disable_device(pdev
);
274 static const struct pci_device_id sdv_gpio_pci_ids
[] = {
275 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_SDV_GPIO
) },
279 static struct pci_driver sdv_gpio_driver
= {
281 .id_table
= sdv_gpio_pci_ids
,
282 .probe
= sdv_gpio_probe
,
283 .remove
= sdv_gpio_remove
,
286 module_pci_driver(sdv_gpio_driver
);
288 MODULE_AUTHOR("Hans J. Koch <hjk@linutronix.de>");
289 MODULE_DESCRIPTION("GPIO interface for Intel Sodaville SoCs");
290 MODULE_LICENSE("GPL v2");