2 * Simple Memory-Mapped GPIOs
4 * Copyright (c) MontaVista Software, Inc. 2008.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/ioport.h>
22 #include <linux/of_gpio.h>
23 #include <linux/gpio.h>
25 #include "simple_gpio.h"
28 struct of_mm_gpio_chip mm_gc
;
31 /* shadowed data register to clear/set bits safely */
35 static struct u8_gpio_chip
*to_u8_gpio_chip(struct of_mm_gpio_chip
*mm_gc
)
37 return container_of(mm_gc
, struct u8_gpio_chip
, mm_gc
);
40 static u8
u8_pin2mask(unsigned int pin
)
42 return 1 << (8 - 1 - pin
);
45 static int u8_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
47 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
49 return in_8(mm_gc
->regs
) & u8_pin2mask(gpio
);
52 static void u8_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
54 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
55 struct u8_gpio_chip
*u8_gc
= to_u8_gpio_chip(mm_gc
);
58 spin_lock_irqsave(&u8_gc
->lock
, flags
);
61 u8_gc
->data
|= u8_pin2mask(gpio
);
63 u8_gc
->data
&= ~u8_pin2mask(gpio
);
65 out_8(mm_gc
->regs
, u8_gc
->data
);
67 spin_unlock_irqrestore(&u8_gc
->lock
, flags
);
70 static int u8_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
75 static int u8_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
77 u8_gpio_set(gc
, gpio
, val
);
81 static void u8_gpio_save_regs(struct of_mm_gpio_chip
*mm_gc
)
83 struct u8_gpio_chip
*u8_gc
= to_u8_gpio_chip(mm_gc
);
85 u8_gc
->data
= in_8(mm_gc
->regs
);
88 static int __init
u8_simple_gpiochip_add(struct device_node
*np
)
91 struct u8_gpio_chip
*u8_gc
;
92 struct of_mm_gpio_chip
*mm_gc
;
93 struct of_gpio_chip
*of_gc
;
96 u8_gc
= kzalloc(sizeof(*u8_gc
), GFP_KERNEL
);
100 spin_lock_init(&u8_gc
->lock
);
102 mm_gc
= &u8_gc
->mm_gc
;
103 of_gc
= &mm_gc
->of_gc
;
106 mm_gc
->save_regs
= u8_gpio_save_regs
;
107 of_gc
->gpio_cells
= 2;
109 gc
->direction_input
= u8_gpio_dir_in
;
110 gc
->direction_output
= u8_gpio_dir_out
;
111 gc
->get
= u8_gpio_get
;
112 gc
->set
= u8_gpio_set
;
114 ret
= of_mm_gpiochip_add(np
, mm_gc
);
123 void __init
simple_gpiochip_init(const char *compatible
)
125 struct device_node
*np
;
127 for_each_compatible_node(np
, NULL
, compatible
) {
131 ret
= of_address_to_resource(np
, 0, &r
);
135 switch (resource_size(&r
)) {
137 ret
= u8_simple_gpiochip_add(np
);
143 * Whenever you need support for GPIO bank width > 1,
144 * please just turn u8_ code into huge macros, and
145 * construct needed uX_ code with it.
152 pr_err("%s: registration failed, status %d\n",