1 /* langwell_gpio.c Moorestown platform Langwell chip GPIO driver
2 * Copyright (c) 2008 - 2009, Intel Corporation.
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Moorestown platform Langwell chip.
20 * Medfield platform Penwell chip.
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/stddef.h>
28 #include <linux/interrupt.h>
29 #include <linux/init.h>
30 #include <linux/irq.h>
32 #include <linux/gpio.h>
33 #include <linux/slab.h>
36 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
37 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
38 * registers to control them, so we only define the order here instead of a
39 * structure, to get a bit offset for a pin (use GPDR as an example):
44 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
46 * so the bit of reg_addr is to control pin offset's GPDR feature
50 GPLR
= 0, /* pin level read-only */
51 GPDR
, /* pin direction */
54 GRER
, /* rising edge detect */
55 GFER
, /* falling edge detect */
56 GEDR
, /* edge detect result */
60 struct gpio_chip chip
;
66 static void __iomem
*gpio_reg(struct gpio_chip
*chip
, unsigned offset
,
67 enum GPIO_REG reg_type
)
69 struct lnw_gpio
*lnw
= container_of(chip
, struct lnw_gpio
, chip
);
70 unsigned nreg
= chip
->ngpio
/ 32;
74 ptr
= (void __iomem
*)(lnw
->reg_base
+ reg_type
* nreg
* 4 + reg
* 4);
78 static int lnw_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
80 void __iomem
*gplr
= gpio_reg(chip
, offset
, GPLR
);
82 return readl(gplr
) & BIT(offset
% 32);
85 static void lnw_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
87 void __iomem
*gpsr
, *gpcr
;
90 gpsr
= gpio_reg(chip
, offset
, GPSR
);
91 writel(BIT(offset
% 32), gpsr
);
93 gpcr
= gpio_reg(chip
, offset
, GPCR
);
94 writel(BIT(offset
% 32), gpcr
);
98 static int lnw_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
100 struct lnw_gpio
*lnw
= container_of(chip
, struct lnw_gpio
, chip
);
101 void __iomem
*gpdr
= gpio_reg(chip
, offset
, GPDR
);
105 spin_lock_irqsave(&lnw
->lock
, flags
);
107 value
&= ~BIT(offset
% 32);
109 spin_unlock_irqrestore(&lnw
->lock
, flags
);
113 static int lnw_gpio_direction_output(struct gpio_chip
*chip
,
114 unsigned offset
, int value
)
116 struct lnw_gpio
*lnw
= container_of(chip
, struct lnw_gpio
, chip
);
117 void __iomem
*gpdr
= gpio_reg(chip
, offset
, GPDR
);
120 lnw_gpio_set(chip
, offset
, value
);
121 spin_lock_irqsave(&lnw
->lock
, flags
);
123 value
|= BIT(offset
% 32);;
125 spin_unlock_irqrestore(&lnw
->lock
, flags
);
129 static int lnw_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
131 struct lnw_gpio
*lnw
= container_of(chip
, struct lnw_gpio
, chip
);
132 return lnw
->irq_base
+ offset
;
135 static int lnw_irq_type(unsigned irq
, unsigned type
)
137 struct lnw_gpio
*lnw
= get_irq_chip_data(irq
);
138 u32 gpio
= irq
- lnw
->irq_base
;
141 void __iomem
*grer
= gpio_reg(&lnw
->chip
, gpio
, GRER
);
142 void __iomem
*gfer
= gpio_reg(&lnw
->chip
, gpio
, GFER
);
144 if (gpio
>= lnw
->chip
.ngpio
)
146 spin_lock_irqsave(&lnw
->lock
, flags
);
147 if (type
& IRQ_TYPE_EDGE_RISING
)
148 value
= readl(grer
) | BIT(gpio
% 32);
150 value
= readl(grer
) & (~BIT(gpio
% 32));
153 if (type
& IRQ_TYPE_EDGE_FALLING
)
154 value
= readl(gfer
) | BIT(gpio
% 32);
156 value
= readl(gfer
) & (~BIT(gpio
% 32));
158 spin_unlock_irqrestore(&lnw
->lock
, flags
);
163 static void lnw_irq_unmask(unsigned irq
)
167 static void lnw_irq_mask(unsigned irq
)
171 static struct irq_chip lnw_irqchip
= {
173 .mask
= lnw_irq_mask
,
174 .unmask
= lnw_irq_unmask
,
175 .set_type
= lnw_irq_type
,
178 static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids
) = { /* pin number */
179 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x080f), .driver_data
= 64 },
180 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x081f), .driver_data
= 96 },
181 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x081a), .driver_data
= 96 },
184 MODULE_DEVICE_TABLE(pci
, lnw_gpio_ids
);
186 static void lnw_irq_handler(unsigned irq
, struct irq_desc
*desc
)
188 struct lnw_gpio
*lnw
= (struct lnw_gpio
*)get_irq_data(irq
);
193 /* check GPIO controller to check which pin triggered the interrupt */
194 for (base
= 0; base
< lnw
->chip
.ngpio
; base
+= 32) {
195 gedr
= gpio_reg(&lnw
->chip
, base
, GEDR
);
196 gedr_v
= readl(gedr
);
199 for (gpio
= base
; gpio
< base
+ 32; gpio
++)
200 if (gedr_v
& BIT(gpio
% 32)) {
201 pr_debug("pin %d triggered\n", gpio
);
202 generic_handle_irq(lnw
->irq_base
+ gpio
);
204 /* clear the edge detect status bit */
205 writel(gedr_v
, gedr
);
207 desc
->chip
->eoi(irq
);
210 static int __devinit
lnw_gpio_probe(struct pci_dev
*pdev
,
211 const struct pci_device_id
*id
)
215 resource_size_t start
, len
;
216 struct lnw_gpio
*lnw
;
221 retval
= pci_enable_device(pdev
);
225 retval
= pci_request_regions(pdev
, "langwell_gpio");
227 dev_err(&pdev
->dev
, "error requesting resources\n");
230 /* get the irq_base from bar1 */
231 start
= pci_resource_start(pdev
, 1);
232 len
= pci_resource_len(pdev
, 1);
233 base
= ioremap_nocache(start
, len
);
235 dev_err(&pdev
->dev
, "error mapping bar1\n");
238 irq_base
= *(u32
*)base
;
239 gpio_base
= *((u32
*)base
+ 1);
240 /* release the IO mapping, since we already get the info from bar1 */
242 /* get the register base from bar0 */
243 start
= pci_resource_start(pdev
, 0);
244 len
= pci_resource_len(pdev
, 0);
245 base
= ioremap_nocache(start
, len
);
247 dev_err(&pdev
->dev
, "error mapping bar0\n");
252 lnw
= kzalloc(sizeof(struct lnw_gpio
), GFP_KERNEL
);
254 dev_err(&pdev
->dev
, "can't allocate langwell_gpio chip data\n");
258 lnw
->reg_base
= base
;
259 lnw
->irq_base
= irq_base
;
260 lnw
->chip
.label
= dev_name(&pdev
->dev
);
261 lnw
->chip
.direction_input
= lnw_gpio_direction_input
;
262 lnw
->chip
.direction_output
= lnw_gpio_direction_output
;
263 lnw
->chip
.get
= lnw_gpio_get
;
264 lnw
->chip
.set
= lnw_gpio_set
;
265 lnw
->chip
.to_irq
= lnw_gpio_to_irq
;
266 lnw
->chip
.base
= gpio_base
;
267 lnw
->chip
.ngpio
= id
->driver_data
;
268 lnw
->chip
.can_sleep
= 0;
269 pci_set_drvdata(pdev
, lnw
);
270 retval
= gpiochip_add(&lnw
->chip
);
272 dev_err(&pdev
->dev
, "langwell gpiochip_add error %d\n", retval
);
275 set_irq_data(pdev
->irq
, lnw
);
276 set_irq_chained_handler(pdev
->irq
, lnw_irq_handler
);
277 for (i
= 0; i
< lnw
->chip
.ngpio
; i
++) {
278 set_irq_chip_and_handler_name(i
+ lnw
->irq_base
, &lnw_irqchip
,
279 handle_simple_irq
, "demux");
280 set_irq_chip_data(i
+ lnw
->irq_base
, lnw
);
283 spin_lock_init(&lnw
->lock
);
290 pci_release_regions(pdev
);
292 pci_disable_device(pdev
);
297 static struct pci_driver lnw_gpio_driver
= {
298 .name
= "langwell_gpio",
299 .id_table
= lnw_gpio_ids
,
300 .probe
= lnw_gpio_probe
,
303 static int __init
lnw_gpio_init(void)
305 return pci_register_driver(&lnw_gpio_driver
);
308 device_initcall(lnw_gpio_init
);