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/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/ioport.h>
21 #include <linux/of_gpio.h>
22 #include <linux/gpio/driver.h>
23 #include <linux/slab.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 u8
u8_pin2mask(unsigned int pin
)
37 return 1 << (8 - 1 - pin
);
40 static int u8_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
42 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
44 return !!(in_8(mm_gc
->regs
) & u8_pin2mask(gpio
));
47 static void u8_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
49 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
50 struct u8_gpio_chip
*u8_gc
= gpiochip_get_data(gc
);
53 spin_lock_irqsave(&u8_gc
->lock
, flags
);
56 u8_gc
->data
|= u8_pin2mask(gpio
);
58 u8_gc
->data
&= ~u8_pin2mask(gpio
);
60 out_8(mm_gc
->regs
, u8_gc
->data
);
62 spin_unlock_irqrestore(&u8_gc
->lock
, flags
);
65 static int u8_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
70 static int u8_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
72 u8_gpio_set(gc
, gpio
, val
);
76 static void u8_gpio_save_regs(struct of_mm_gpio_chip
*mm_gc
)
78 struct u8_gpio_chip
*u8_gc
=
79 container_of(mm_gc
, struct u8_gpio_chip
, mm_gc
);
81 u8_gc
->data
= in_8(mm_gc
->regs
);
84 static int __init
u8_simple_gpiochip_add(struct device_node
*np
)
87 struct u8_gpio_chip
*u8_gc
;
88 struct of_mm_gpio_chip
*mm_gc
;
91 u8_gc
= kzalloc(sizeof(*u8_gc
), GFP_KERNEL
);
95 spin_lock_init(&u8_gc
->lock
);
97 mm_gc
= &u8_gc
->mm_gc
;
100 mm_gc
->save_regs
= u8_gpio_save_regs
;
102 gc
->direction_input
= u8_gpio_dir_in
;
103 gc
->direction_output
= u8_gpio_dir_out
;
104 gc
->get
= u8_gpio_get
;
105 gc
->set
= u8_gpio_set
;
107 ret
= of_mm_gpiochip_add_data(np
, mm_gc
, u8_gc
);
116 void __init
simple_gpiochip_init(const char *compatible
)
118 struct device_node
*np
;
120 for_each_compatible_node(np
, NULL
, compatible
) {
124 ret
= of_address_to_resource(np
, 0, &r
);
128 switch (resource_size(&r
)) {
130 ret
= u8_simple_gpiochip_add(np
);
136 * Whenever you need support for GPIO bank width > 1,
137 * please just turn u8_ code into huge macros, and
138 * construct needed uX_ code with it.
145 pr_err("%pOF: registration failed, status %d\n", np
, ret
);