2 * vf610 GPIO support through PORT and GPIO module
4 * Copyright (c) 2014 Toradex AG.
6 * Author: Stefan Agner <stefan@agner.ch>.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/gpio.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/irq.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
29 #include <linux/of_device.h>
30 #include <linux/of_irq.h>
32 #define VF610_GPIO_PER_PORT 32
34 struct vf610_gpio_port
{
37 void __iomem
*gpio_base
;
38 u8 irqc
[VF610_GPIO_PER_PORT
];
42 #define GPIO_PDOR 0x00
43 #define GPIO_PSOR 0x04
44 #define GPIO_PCOR 0x08
45 #define GPIO_PTOR 0x0c
46 #define GPIO_PDIR 0x10
48 #define PORT_PCR(n) ((n) * 0x4)
49 #define PORT_PCR_IRQC_OFFSET 16
51 #define PORT_ISFR 0xa0
52 #define PORT_DFER 0xc0
53 #define PORT_DFCR 0xc4
54 #define PORT_DFWR 0xc8
56 #define PORT_INT_OFF 0x0
57 #define PORT_INT_LOGIC_ZERO 0x8
58 #define PORT_INT_RISING_EDGE 0x9
59 #define PORT_INT_FALLING_EDGE 0xa
60 #define PORT_INT_EITHER_EDGE 0xb
61 #define PORT_INT_LOGIC_ONE 0xc
63 static struct irq_chip vf610_gpio_irq_chip
;
65 static const struct of_device_id vf610_gpio_dt_ids
[] = {
66 { .compatible
= "fsl,vf610-gpio" },
70 static inline void vf610_gpio_writel(u32 val
, void __iomem
*reg
)
72 writel_relaxed(val
, reg
);
75 static inline u32
vf610_gpio_readl(void __iomem
*reg
)
77 return readl_relaxed(reg
);
80 static int vf610_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
82 struct vf610_gpio_port
*port
= gpiochip_get_data(gc
);
84 return !!(vf610_gpio_readl(port
->gpio_base
+ GPIO_PDIR
) & BIT(gpio
));
87 static void vf610_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
89 struct vf610_gpio_port
*port
= gpiochip_get_data(gc
);
90 unsigned long mask
= BIT(gpio
);
93 vf610_gpio_writel(mask
, port
->gpio_base
+ GPIO_PSOR
);
95 vf610_gpio_writel(mask
, port
->gpio_base
+ GPIO_PCOR
);
98 static int vf610_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
100 return pinctrl_gpio_direction_input(chip
->base
+ gpio
);
103 static int vf610_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
106 vf610_gpio_set(chip
, gpio
, value
);
108 return pinctrl_gpio_direction_output(chip
->base
+ gpio
);
111 static void vf610_gpio_irq_handler(struct irq_desc
*desc
)
113 struct vf610_gpio_port
*port
=
114 gpiochip_get_data(irq_desc_get_handler_data(desc
));
115 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
117 unsigned long irq_isfr
;
119 chained_irq_enter(chip
, desc
);
121 irq_isfr
= vf610_gpio_readl(port
->base
+ PORT_ISFR
);
123 for_each_set_bit(pin
, &irq_isfr
, VF610_GPIO_PER_PORT
) {
124 vf610_gpio_writel(BIT(pin
), port
->base
+ PORT_ISFR
);
126 generic_handle_irq(irq_find_mapping(port
->gc
.irqdomain
, pin
));
129 chained_irq_exit(chip
, desc
);
132 static void vf610_gpio_irq_ack(struct irq_data
*d
)
134 struct vf610_gpio_port
*port
=
135 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
138 vf610_gpio_writel(BIT(gpio
), port
->base
+ PORT_ISFR
);
141 static int vf610_gpio_irq_set_type(struct irq_data
*d
, u32 type
)
143 struct vf610_gpio_port
*port
=
144 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
148 case IRQ_TYPE_EDGE_RISING
:
149 irqc
= PORT_INT_RISING_EDGE
;
151 case IRQ_TYPE_EDGE_FALLING
:
152 irqc
= PORT_INT_FALLING_EDGE
;
154 case IRQ_TYPE_EDGE_BOTH
:
155 irqc
= PORT_INT_EITHER_EDGE
;
157 case IRQ_TYPE_LEVEL_LOW
:
158 irqc
= PORT_INT_LOGIC_ZERO
;
160 case IRQ_TYPE_LEVEL_HIGH
:
161 irqc
= PORT_INT_LOGIC_ONE
;
167 port
->irqc
[d
->hwirq
] = irqc
;
169 if (type
& IRQ_TYPE_LEVEL_MASK
)
170 irq_set_handler_locked(d
, handle_level_irq
);
172 irq_set_handler_locked(d
, handle_edge_irq
);
177 static void vf610_gpio_irq_mask(struct irq_data
*d
)
179 struct vf610_gpio_port
*port
=
180 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
181 void __iomem
*pcr_base
= port
->base
+ PORT_PCR(d
->hwirq
);
183 vf610_gpio_writel(0, pcr_base
);
186 static void vf610_gpio_irq_unmask(struct irq_data
*d
)
188 struct vf610_gpio_port
*port
=
189 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
190 void __iomem
*pcr_base
= port
->base
+ PORT_PCR(d
->hwirq
);
192 vf610_gpio_writel(port
->irqc
[d
->hwirq
] << PORT_PCR_IRQC_OFFSET
,
196 static int vf610_gpio_irq_set_wake(struct irq_data
*d
, u32 enable
)
198 struct vf610_gpio_port
*port
=
199 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
202 enable_irq_wake(port
->irq
);
204 disable_irq_wake(port
->irq
);
209 static struct irq_chip vf610_gpio_irq_chip
= {
210 .name
= "gpio-vf610",
211 .irq_ack
= vf610_gpio_irq_ack
,
212 .irq_mask
= vf610_gpio_irq_mask
,
213 .irq_unmask
= vf610_gpio_irq_unmask
,
214 .irq_set_type
= vf610_gpio_irq_set_type
,
215 .irq_set_wake
= vf610_gpio_irq_set_wake
,
218 static int vf610_gpio_probe(struct platform_device
*pdev
)
220 struct device
*dev
= &pdev
->dev
;
221 struct device_node
*np
= dev
->of_node
;
222 struct vf610_gpio_port
*port
;
223 struct resource
*iores
;
224 struct gpio_chip
*gc
;
227 port
= devm_kzalloc(&pdev
->dev
, sizeof(*port
), GFP_KERNEL
);
231 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
232 port
->base
= devm_ioremap_resource(dev
, iores
);
233 if (IS_ERR(port
->base
))
234 return PTR_ERR(port
->base
);
236 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
237 port
->gpio_base
= devm_ioremap_resource(dev
, iores
);
238 if (IS_ERR(port
->gpio_base
))
239 return PTR_ERR(port
->gpio_base
);
241 port
->irq
= platform_get_irq(pdev
, 0);
248 gc
->label
= "vf610-gpio";
249 gc
->ngpio
= VF610_GPIO_PER_PORT
;
250 gc
->base
= of_alias_get_id(np
, "gpio") * VF610_GPIO_PER_PORT
;
252 gc
->request
= gpiochip_generic_request
;
253 gc
->free
= gpiochip_generic_free
;
254 gc
->direction_input
= vf610_gpio_direction_input
;
255 gc
->get
= vf610_gpio_get
;
256 gc
->direction_output
= vf610_gpio_direction_output
;
257 gc
->set
= vf610_gpio_set
;
259 ret
= gpiochip_add_data(gc
, port
);
263 /* Clear the interrupt status register for all GPIO's */
264 vf610_gpio_writel(~0, port
->base
+ PORT_ISFR
);
266 ret
= gpiochip_irqchip_add(gc
, &vf610_gpio_irq_chip
, 0,
267 handle_edge_irq
, IRQ_TYPE_NONE
);
269 dev_err(dev
, "failed to add irqchip\n");
273 gpiochip_set_chained_irqchip(gc
, &vf610_gpio_irq_chip
, port
->irq
,
274 vf610_gpio_irq_handler
);
279 static struct platform_driver vf610_gpio_driver
= {
281 .name
= "gpio-vf610",
282 .of_match_table
= vf610_gpio_dt_ids
,
284 .probe
= vf610_gpio_probe
,
287 static int __init
gpio_vf610_init(void)
289 return platform_driver_register(&vf610_gpio_driver
);
291 device_initcall(gpio_vf610_init
);
293 MODULE_AUTHOR("Stefan Agner <stefan@agner.ch>");
294 MODULE_DESCRIPTION("Freescale VF610 GPIO");
295 MODULE_LICENSE("GPL v2");