2 * Intel MID GPIO driver
4 * Copyright (c) 2008-2014 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 version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 * Moorestown platform Langwell chip.
18 * Medfield platform Penwell chip.
19 * Clovertrail platform Cloverview chip.
20 * Merrifield platform Tangier chip.
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/stddef.h>
29 #include <linux/interrupt.h>
30 #include <linux/init.h>
32 #include <linux/gpio/driver.h>
33 #include <linux/slab.h>
34 #include <linux/pm_runtime.h>
36 #define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
37 #define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
40 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
41 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
42 * registers to control them, so we only define the order here instead of a
43 * structure, to get a bit offset for a pin (use GPDR as an example):
48 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
50 * so the bit of reg_addr is to control pin offset's GPDR feature
54 GPLR
= 0, /* pin level read-only */
55 GPDR
, /* pin direction */
58 GRER
, /* rising edge detect */
59 GFER
, /* falling edge detect */
60 GEDR
, /* edge detect result */
61 GAFR
, /* alt function */
64 /* intel_mid gpio driver data */
65 struct intel_mid_gpio_ddata
{
66 u16 ngpio
; /* number of gpio pins */
67 u32 gplr_offset
; /* offset of first GPLR register from base */
68 u32 flis_base
; /* base address of FLIS registers */
69 u32 flis_len
; /* length of FLIS registers */
70 u32 (*get_flis_offset
)(int gpio
);
71 u32 chip_irq_type
; /* chip interrupt type */
74 struct intel_mid_gpio
{
75 struct gpio_chip chip
;
76 void __iomem
*reg_base
;
81 static void __iomem
*gpio_reg(struct gpio_chip
*chip
, unsigned offset
,
82 enum GPIO_REG reg_type
)
84 struct intel_mid_gpio
*priv
= gpiochip_get_data(chip
);
85 unsigned nreg
= chip
->ngpio
/ 32;
88 return priv
->reg_base
+ reg_type
* nreg
* 4 + reg
* 4;
91 static void __iomem
*gpio_reg_2bit(struct gpio_chip
*chip
, unsigned offset
,
92 enum GPIO_REG reg_type
)
94 struct intel_mid_gpio
*priv
= gpiochip_get_data(chip
);
95 unsigned nreg
= chip
->ngpio
/ 32;
98 return priv
->reg_base
+ reg_type
* nreg
* 4 + reg
* 4;
101 static int intel_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
103 void __iomem
*gafr
= gpio_reg_2bit(chip
, offset
, GAFR
);
104 u32 value
= readl(gafr
);
105 int shift
= (offset
% 16) << 1, af
= (value
>> shift
) & 3;
108 value
&= ~(3 << shift
);
114 static int intel_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
116 void __iomem
*gplr
= gpio_reg(chip
, offset
, GPLR
);
118 return !!(readl(gplr
) & BIT(offset
% 32));
121 static void intel_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
123 void __iomem
*gpsr
, *gpcr
;
126 gpsr
= gpio_reg(chip
, offset
, GPSR
);
127 writel(BIT(offset
% 32), gpsr
);
129 gpcr
= gpio_reg(chip
, offset
, GPCR
);
130 writel(BIT(offset
% 32), gpcr
);
134 static int intel_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
136 struct intel_mid_gpio
*priv
= gpiochip_get_data(chip
);
137 void __iomem
*gpdr
= gpio_reg(chip
, offset
, GPDR
);
142 pm_runtime_get(&priv
->pdev
->dev
);
144 spin_lock_irqsave(&priv
->lock
, flags
);
146 value
&= ~BIT(offset
% 32);
148 spin_unlock_irqrestore(&priv
->lock
, flags
);
151 pm_runtime_put(&priv
->pdev
->dev
);
156 static int intel_gpio_direction_output(struct gpio_chip
*chip
,
157 unsigned offset
, int value
)
159 struct intel_mid_gpio
*priv
= gpiochip_get_data(chip
);
160 void __iomem
*gpdr
= gpio_reg(chip
, offset
, GPDR
);
163 intel_gpio_set(chip
, offset
, value
);
166 pm_runtime_get(&priv
->pdev
->dev
);
168 spin_lock_irqsave(&priv
->lock
, flags
);
170 value
|= BIT(offset
% 32);
172 spin_unlock_irqrestore(&priv
->lock
, flags
);
175 pm_runtime_put(&priv
->pdev
->dev
);
180 static int intel_mid_irq_type(struct irq_data
*d
, unsigned type
)
182 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
183 struct intel_mid_gpio
*priv
= gpiochip_get_data(gc
);
184 u32 gpio
= irqd_to_hwirq(d
);
187 void __iomem
*grer
= gpio_reg(&priv
->chip
, gpio
, GRER
);
188 void __iomem
*gfer
= gpio_reg(&priv
->chip
, gpio
, GFER
);
190 if (gpio
>= priv
->chip
.ngpio
)
194 pm_runtime_get(&priv
->pdev
->dev
);
196 spin_lock_irqsave(&priv
->lock
, flags
);
197 if (type
& IRQ_TYPE_EDGE_RISING
)
198 value
= readl(grer
) | BIT(gpio
% 32);
200 value
= readl(grer
) & (~BIT(gpio
% 32));
203 if (type
& IRQ_TYPE_EDGE_FALLING
)
204 value
= readl(gfer
) | BIT(gpio
% 32);
206 value
= readl(gfer
) & (~BIT(gpio
% 32));
208 spin_unlock_irqrestore(&priv
->lock
, flags
);
211 pm_runtime_put(&priv
->pdev
->dev
);
216 static void intel_mid_irq_unmask(struct irq_data
*d
)
220 static void intel_mid_irq_mask(struct irq_data
*d
)
224 static struct irq_chip intel_mid_irqchip
= {
225 .name
= "INTEL_MID-GPIO",
226 .irq_mask
= intel_mid_irq_mask
,
227 .irq_unmask
= intel_mid_irq_unmask
,
228 .irq_set_type
= intel_mid_irq_type
,
231 static const struct intel_mid_gpio_ddata gpio_lincroft
= {
235 static const struct intel_mid_gpio_ddata gpio_penwell_aon
= {
237 .chip_irq_type
= INTEL_MID_IRQ_TYPE_EDGE
,
240 static const struct intel_mid_gpio_ddata gpio_penwell_core
= {
242 .chip_irq_type
= INTEL_MID_IRQ_TYPE_EDGE
,
245 static const struct intel_mid_gpio_ddata gpio_cloverview_aon
= {
247 .chip_irq_type
= INTEL_MID_IRQ_TYPE_EDGE
| INTEL_MID_IRQ_TYPE_LEVEL
,
250 static const struct intel_mid_gpio_ddata gpio_cloverview_core
= {
252 .chip_irq_type
= INTEL_MID_IRQ_TYPE_EDGE
,
255 static const struct intel_mid_gpio_ddata gpio_tangier
= {
258 .flis_base
= 0xff0c0000,
260 .get_flis_offset
= NULL
,
261 .chip_irq_type
= INTEL_MID_IRQ_TYPE_EDGE
,
264 static const struct pci_device_id intel_gpio_ids
[] = {
267 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x080f),
268 .driver_data
= (kernel_ulong_t
)&gpio_lincroft
,
272 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x081f),
273 .driver_data
= (kernel_ulong_t
)&gpio_penwell_aon
,
277 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x081a),
278 .driver_data
= (kernel_ulong_t
)&gpio_penwell_core
,
282 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x08eb),
283 .driver_data
= (kernel_ulong_t
)&gpio_cloverview_aon
,
286 /* Cloverview Core */
287 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x08f7),
288 .driver_data
= (kernel_ulong_t
)&gpio_cloverview_core
,
292 PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x1199),
293 .driver_data
= (kernel_ulong_t
)&gpio_tangier
,
297 MODULE_DEVICE_TABLE(pci
, intel_gpio_ids
);
299 static void intel_mid_irq_handler(struct irq_desc
*desc
)
301 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
302 struct intel_mid_gpio
*priv
= gpiochip_get_data(gc
);
303 struct irq_data
*data
= irq_desc_get_irq_data(desc
);
304 struct irq_chip
*chip
= irq_data_get_irq_chip(data
);
305 u32 base
, gpio
, mask
;
306 unsigned long pending
;
309 /* check GPIO controller to check which pin triggered the interrupt */
310 for (base
= 0; base
< priv
->chip
.ngpio
; base
+= 32) {
311 gedr
= gpio_reg(&priv
->chip
, base
, GEDR
);
312 while ((pending
= readl(gedr
))) {
313 gpio
= __ffs(pending
);
315 /* Clear before handling so we can't lose an edge */
317 generic_handle_irq(irq_find_mapping(gc
->irqdomain
,
325 static void intel_mid_irq_init_hw(struct intel_mid_gpio
*priv
)
330 for (base
= 0; base
< priv
->chip
.ngpio
; base
+= 32) {
331 /* Clear the rising-edge detect register */
332 reg
= gpio_reg(&priv
->chip
, base
, GRER
);
334 /* Clear the falling-edge detect register */
335 reg
= gpio_reg(&priv
->chip
, base
, GFER
);
337 /* Clear the edge detect status register */
338 reg
= gpio_reg(&priv
->chip
, base
, GEDR
);
343 static int intel_gpio_runtime_idle(struct device
*dev
)
345 int err
= pm_schedule_suspend(dev
, 500);
346 return err
?: -EBUSY
;
349 static const struct dev_pm_ops intel_gpio_pm_ops
= {
350 SET_RUNTIME_PM_OPS(NULL
, NULL
, intel_gpio_runtime_idle
)
353 static int intel_gpio_probe(struct pci_dev
*pdev
,
354 const struct pci_device_id
*id
)
357 struct intel_mid_gpio
*priv
;
361 struct intel_mid_gpio_ddata
*ddata
=
362 (struct intel_mid_gpio_ddata
*)id
->driver_data
;
364 retval
= pcim_enable_device(pdev
);
368 retval
= pcim_iomap_regions(pdev
, 1 << 0 | 1 << 1, pci_name(pdev
));
370 dev_err(&pdev
->dev
, "I/O memory mapping error\n");
374 base
= pcim_iomap_table(pdev
)[1];
376 irq_base
= readl(base
);
377 gpio_base
= readl(sizeof(u32
) + base
);
379 /* release the IO mapping, since we already get the info from bar1 */
380 pcim_iounmap_regions(pdev
, 1 << 1);
382 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
384 dev_err(&pdev
->dev
, "can't allocate chip data\n");
388 priv
->reg_base
= pcim_iomap_table(pdev
)[0];
389 priv
->chip
.label
= dev_name(&pdev
->dev
);
390 priv
->chip
.parent
= &pdev
->dev
;
391 priv
->chip
.request
= intel_gpio_request
;
392 priv
->chip
.direction_input
= intel_gpio_direction_input
;
393 priv
->chip
.direction_output
= intel_gpio_direction_output
;
394 priv
->chip
.get
= intel_gpio_get
;
395 priv
->chip
.set
= intel_gpio_set
;
396 priv
->chip
.base
= gpio_base
;
397 priv
->chip
.ngpio
= ddata
->ngpio
;
398 priv
->chip
.can_sleep
= false;
401 spin_lock_init(&priv
->lock
);
403 pci_set_drvdata(pdev
, priv
);
404 retval
= gpiochip_add_data(&priv
->chip
, priv
);
406 dev_err(&pdev
->dev
, "gpiochip_add error %d\n", retval
);
410 retval
= gpiochip_irqchip_add(&priv
->chip
,
417 "could not connect irqchip to gpiochip\n");
421 intel_mid_irq_init_hw(priv
);
423 gpiochip_set_chained_irqchip(&priv
->chip
,
426 intel_mid_irq_handler
);
428 pm_runtime_put_noidle(&pdev
->dev
);
429 pm_runtime_allow(&pdev
->dev
);
434 static struct pci_driver intel_gpio_driver
= {
435 .name
= "intel_mid_gpio",
436 .id_table
= intel_gpio_ids
,
437 .probe
= intel_gpio_probe
,
439 .pm
= &intel_gpio_pm_ops
,
443 static int __init
intel_gpio_init(void)
445 return pci_register_driver(&intel_gpio_driver
);
448 device_initcall(intel_gpio_init
);