2 * Renesas R-Car GPIO Support
4 * Copyright (C) 2013 Magnus Damm
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
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.
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
21 #include <linux/ioport.h>
22 #include <linux/irq.h>
23 #include <linux/irqdomain.h>
24 #include <linux/module.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/platform_data/gpio-rcar.h>
27 #include <linux/platform_device.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
31 struct gpio_rcar_priv
{
34 struct gpio_rcar_config config
;
35 struct platform_device
*pdev
;
36 struct gpio_chip gpio_chip
;
37 struct irq_chip irq_chip
;
38 struct irq_domain
*irq_domain
;
53 static inline u32
gpio_rcar_read(struct gpio_rcar_priv
*p
, int offs
)
55 return ioread32(p
->base
+ offs
);
58 static inline void gpio_rcar_write(struct gpio_rcar_priv
*p
, int offs
,
61 iowrite32(value
, p
->base
+ offs
);
64 static void gpio_rcar_modify_bit(struct gpio_rcar_priv
*p
, int offs
,
67 u32 tmp
= gpio_rcar_read(p
, offs
);
74 gpio_rcar_write(p
, offs
, tmp
);
77 static void gpio_rcar_irq_disable(struct irq_data
*d
)
79 struct gpio_rcar_priv
*p
= irq_data_get_irq_chip_data(d
);
81 gpio_rcar_write(p
, INTMSK
, ~BIT(irqd_to_hwirq(d
)));
84 static void gpio_rcar_irq_enable(struct irq_data
*d
)
86 struct gpio_rcar_priv
*p
= irq_data_get_irq_chip_data(d
);
88 gpio_rcar_write(p
, MSKCLR
, BIT(irqd_to_hwirq(d
)));
91 static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv
*p
,
93 bool active_high_rising_edge
,
98 /* follow steps in the GPIO documentation for
99 * "Setting Edge-Sensitive Interrupt Input Mode" and
100 * "Setting Level-Sensitive Interrupt Input Mode"
103 spin_lock_irqsave(&p
->lock
, flags
);
105 /* Configure postive or negative logic in POSNEG */
106 gpio_rcar_modify_bit(p
, POSNEG
, hwirq
, !active_high_rising_edge
);
108 /* Configure edge or level trigger in EDGLEVEL */
109 gpio_rcar_modify_bit(p
, EDGLEVEL
, hwirq
, !level_trigger
);
111 /* Select "Interrupt Input Mode" in IOINTSEL */
112 gpio_rcar_modify_bit(p
, IOINTSEL
, hwirq
, true);
114 /* Write INTCLR in case of edge trigger */
116 gpio_rcar_write(p
, INTCLR
, BIT(hwirq
));
118 spin_unlock_irqrestore(&p
->lock
, flags
);
121 static int gpio_rcar_irq_set_type(struct irq_data
*d
, unsigned int type
)
123 struct gpio_rcar_priv
*p
= irq_data_get_irq_chip_data(d
);
124 unsigned int hwirq
= irqd_to_hwirq(d
);
126 dev_dbg(&p
->pdev
->dev
, "sense irq = %d, type = %d\n", hwirq
, type
);
128 switch (type
& IRQ_TYPE_SENSE_MASK
) {
129 case IRQ_TYPE_LEVEL_HIGH
:
130 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, true, true);
132 case IRQ_TYPE_LEVEL_LOW
:
133 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, false, true);
135 case IRQ_TYPE_EDGE_RISING
:
136 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, true, false);
138 case IRQ_TYPE_EDGE_FALLING
:
139 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, false, false);
147 static irqreturn_t
gpio_rcar_irq_handler(int irq
, void *dev_id
)
149 struct gpio_rcar_priv
*p
= dev_id
;
151 unsigned int offset
, irqs_handled
= 0;
153 while ((pending
= gpio_rcar_read(p
, INTDT
))) {
154 offset
= __ffs(pending
);
155 gpio_rcar_write(p
, INTCLR
, BIT(offset
));
156 generic_handle_irq(irq_find_mapping(p
->irq_domain
, offset
));
160 return irqs_handled
? IRQ_HANDLED
: IRQ_NONE
;
163 static inline struct gpio_rcar_priv
*gpio_to_priv(struct gpio_chip
*chip
)
165 return container_of(chip
, struct gpio_rcar_priv
, gpio_chip
);
168 static void gpio_rcar_config_general_input_output_mode(struct gpio_chip
*chip
,
172 struct gpio_rcar_priv
*p
= gpio_to_priv(chip
);
175 /* follow steps in the GPIO documentation for
176 * "Setting General Output Mode" and
177 * "Setting General Input Mode"
180 spin_lock_irqsave(&p
->lock
, flags
);
182 /* Configure postive logic in POSNEG */
183 gpio_rcar_modify_bit(p
, POSNEG
, gpio
, false);
185 /* Select "General Input/Output Mode" in IOINTSEL */
186 gpio_rcar_modify_bit(p
, IOINTSEL
, gpio
, false);
188 /* Select Input Mode or Output Mode in INOUTSEL */
189 gpio_rcar_modify_bit(p
, INOUTSEL
, gpio
, output
);
191 spin_unlock_irqrestore(&p
->lock
, flags
);
194 static int gpio_rcar_request(struct gpio_chip
*chip
, unsigned offset
)
196 return pinctrl_request_gpio(chip
->base
+ offset
);
199 static void gpio_rcar_free(struct gpio_chip
*chip
, unsigned offset
)
201 pinctrl_free_gpio(chip
->base
+ offset
);
203 /* Set the GPIO as an input to ensure that the next GPIO request won't
204 * drive the GPIO pin as an output.
206 gpio_rcar_config_general_input_output_mode(chip
, offset
, false);
209 static int gpio_rcar_direction_input(struct gpio_chip
*chip
, unsigned offset
)
211 gpio_rcar_config_general_input_output_mode(chip
, offset
, false);
215 static int gpio_rcar_get(struct gpio_chip
*chip
, unsigned offset
)
217 return (int)(gpio_rcar_read(gpio_to_priv(chip
), INDT
) & BIT(offset
));
220 static void gpio_rcar_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
222 struct gpio_rcar_priv
*p
= gpio_to_priv(chip
);
225 spin_lock_irqsave(&p
->lock
, flags
);
226 gpio_rcar_modify_bit(p
, OUTDT
, offset
, value
);
227 spin_unlock_irqrestore(&p
->lock
, flags
);
230 static int gpio_rcar_direction_output(struct gpio_chip
*chip
, unsigned offset
,
233 /* write GPIO value to output before selecting output mode of pin */
234 gpio_rcar_set(chip
, offset
, value
);
235 gpio_rcar_config_general_input_output_mode(chip
, offset
, true);
239 static int gpio_rcar_to_irq(struct gpio_chip
*chip
, unsigned offset
)
241 return irq_create_mapping(gpio_to_priv(chip
)->irq_domain
, offset
);
244 static int gpio_rcar_irq_domain_map(struct irq_domain
*h
, unsigned int virq
,
247 struct gpio_rcar_priv
*p
= h
->host_data
;
249 dev_dbg(&p
->pdev
->dev
, "map hw irq = %d, virq = %d\n", (int)hw
, virq
);
251 irq_set_chip_data(virq
, h
->host_data
);
252 irq_set_chip_and_handler(virq
, &p
->irq_chip
, handle_level_irq
);
253 set_irq_flags(virq
, IRQF_VALID
); /* kill me now */
257 static struct irq_domain_ops gpio_rcar_irq_domain_ops
= {
258 .map
= gpio_rcar_irq_domain_map
,
261 static int gpio_rcar_probe(struct platform_device
*pdev
)
263 struct gpio_rcar_config
*pdata
= pdev
->dev
.platform_data
;
264 struct gpio_rcar_priv
*p
;
265 struct resource
*io
, *irq
;
266 struct gpio_chip
*gpio_chip
;
267 struct irq_chip
*irq_chip
;
268 const char *name
= dev_name(&pdev
->dev
);
271 p
= devm_kzalloc(&pdev
->dev
, sizeof(*p
), GFP_KERNEL
);
273 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
278 /* deal with driver instance configuration */
283 platform_set_drvdata(pdev
, p
);
284 spin_lock_init(&p
->lock
);
286 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
287 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
290 dev_err(&pdev
->dev
, "missing IRQ or IOMEM\n");
295 p
->base
= devm_ioremap_nocache(&pdev
->dev
, io
->start
,
298 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
303 gpio_chip
= &p
->gpio_chip
;
304 gpio_chip
->request
= gpio_rcar_request
;
305 gpio_chip
->free
= gpio_rcar_free
;
306 gpio_chip
->direction_input
= gpio_rcar_direction_input
;
307 gpio_chip
->get
= gpio_rcar_get
;
308 gpio_chip
->direction_output
= gpio_rcar_direction_output
;
309 gpio_chip
->set
= gpio_rcar_set
;
310 gpio_chip
->to_irq
= gpio_rcar_to_irq
;
311 gpio_chip
->label
= name
;
312 gpio_chip
->owner
= THIS_MODULE
;
313 gpio_chip
->base
= p
->config
.gpio_base
;
314 gpio_chip
->ngpio
= p
->config
.number_of_pins
;
316 irq_chip
= &p
->irq_chip
;
317 irq_chip
->name
= name
;
318 irq_chip
->irq_mask
= gpio_rcar_irq_disable
;
319 irq_chip
->irq_unmask
= gpio_rcar_irq_enable
;
320 irq_chip
->irq_enable
= gpio_rcar_irq_enable
;
321 irq_chip
->irq_disable
= gpio_rcar_irq_disable
;
322 irq_chip
->irq_set_type
= gpio_rcar_irq_set_type
;
323 irq_chip
->flags
= IRQCHIP_SKIP_SET_WAKE
| IRQCHIP_SET_TYPE_MASKED
;
325 p
->irq_domain
= irq_domain_add_simple(pdev
->dev
.of_node
,
326 p
->config
.number_of_pins
,
328 &gpio_rcar_irq_domain_ops
, p
);
329 if (!p
->irq_domain
) {
331 dev_err(&pdev
->dev
, "cannot initialize irq domain\n");
335 if (devm_request_irq(&pdev
->dev
, irq
->start
,
336 gpio_rcar_irq_handler
, 0, name
, p
)) {
337 dev_err(&pdev
->dev
, "failed to request IRQ\n");
342 ret
= gpiochip_add(gpio_chip
);
344 dev_err(&pdev
->dev
, "failed to add GPIO controller\n");
348 dev_info(&pdev
->dev
, "driving %d GPIOs\n", p
->config
.number_of_pins
);
350 /* warn in case of mismatch if irq base is specified */
351 if (p
->config
.irq_base
) {
352 ret
= irq_find_mapping(p
->irq_domain
, 0);
353 if (p
->config
.irq_base
!= ret
)
354 dev_warn(&pdev
->dev
, "irq base mismatch (%u/%u)\n",
355 p
->config
.irq_base
, ret
);
358 ret
= gpiochip_add_pin_range(gpio_chip
, p
->config
.pctl_name
, 0,
359 gpio_chip
->base
, gpio_chip
->ngpio
);
361 dev_warn(&pdev
->dev
, "failed to add pin range\n");
366 irq_domain_remove(p
->irq_domain
);
371 static int gpio_rcar_remove(struct platform_device
*pdev
)
373 struct gpio_rcar_priv
*p
= platform_get_drvdata(pdev
);
376 ret
= gpiochip_remove(&p
->gpio_chip
);
380 irq_domain_remove(p
->irq_domain
);
384 static struct platform_driver gpio_rcar_device_driver
= {
385 .probe
= gpio_rcar_probe
,
386 .remove
= gpio_rcar_remove
,
392 module_platform_driver(gpio_rcar_device_driver
);
394 MODULE_AUTHOR("Magnus Damm");
395 MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
396 MODULE_LICENSE("GPL v2");