4 * Copyright (c) 2008 Harris Corporation
5 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * Copyright (c) MontaVista Software, Inc. 2008.
8 * Author: Steve Falco <sfalco@harris.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
29 #include <linux/of_gpio.h>
30 #include <linux/gpio.h>
31 #include <linux/types.h>
33 #define GPIO_MASK(gpio) (0x80000000 >> (gpio))
34 #define GPIO_MASK2(gpio) (0xc0000000 >> ((gpio) * 2))
36 /* Physical GPIO register layout */
58 struct ppc4xx_gpio_chip
{
59 struct of_mm_gpio_chip mm_gc
;
64 * GPIO LIB API implementation for GPIOs
66 * There are a maximum of 32 gpios in each gpio controller.
69 static inline struct ppc4xx_gpio_chip
*
70 to_ppc4xx_gpiochip(struct of_mm_gpio_chip
*mm_gc
)
72 return container_of(mm_gc
, struct ppc4xx_gpio_chip
, mm_gc
);
75 static int ppc4xx_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
77 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
78 struct ppc4xx_gpio __iomem
*regs
= mm_gc
->regs
;
80 return in_be32(®s
->ir
) & GPIO_MASK(gpio
);
84 __ppc4xx_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
86 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
87 struct ppc4xx_gpio __iomem
*regs
= mm_gc
->regs
;
90 setbits32(®s
->or, GPIO_MASK(gpio
));
92 clrbits32(®s
->or, GPIO_MASK(gpio
));
96 ppc4xx_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
98 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
99 struct ppc4xx_gpio_chip
*chip
= to_ppc4xx_gpiochip(mm_gc
);
102 spin_lock_irqsave(&chip
->lock
, flags
);
104 __ppc4xx_gpio_set(gc
, gpio
, val
);
106 spin_unlock_irqrestore(&chip
->lock
, flags
);
108 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
111 static int ppc4xx_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
113 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
114 struct ppc4xx_gpio_chip
*chip
= to_ppc4xx_gpiochip(mm_gc
);
115 struct ppc4xx_gpio __iomem
*regs
= mm_gc
->regs
;
118 spin_lock_irqsave(&chip
->lock
, flags
);
120 /* Disable open-drain function */
121 clrbits32(®s
->odr
, GPIO_MASK(gpio
));
124 clrbits32(®s
->tcr
, GPIO_MASK(gpio
));
126 /* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
128 clrbits32(®s
->osrl
, GPIO_MASK2(gpio
));
129 clrbits32(®s
->tsrl
, GPIO_MASK2(gpio
));
131 clrbits32(®s
->osrh
, GPIO_MASK2(gpio
));
132 clrbits32(®s
->tsrh
, GPIO_MASK2(gpio
));
135 spin_unlock_irqrestore(&chip
->lock
, flags
);
141 ppc4xx_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
143 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
144 struct ppc4xx_gpio_chip
*chip
= to_ppc4xx_gpiochip(mm_gc
);
145 struct ppc4xx_gpio __iomem
*regs
= mm_gc
->regs
;
148 spin_lock_irqsave(&chip
->lock
, flags
);
150 /* First set initial value */
151 __ppc4xx_gpio_set(gc
, gpio
, val
);
153 /* Disable open-drain function */
154 clrbits32(®s
->odr
, GPIO_MASK(gpio
));
157 setbits32(®s
->tcr
, GPIO_MASK(gpio
));
159 /* Bits 0-15 use TSRL, bits 16-31 use TSRH */
161 clrbits32(®s
->osrl
, GPIO_MASK2(gpio
));
162 clrbits32(®s
->tsrl
, GPIO_MASK2(gpio
));
164 clrbits32(®s
->osrh
, GPIO_MASK2(gpio
));
165 clrbits32(®s
->tsrh
, GPIO_MASK2(gpio
));
168 spin_unlock_irqrestore(&chip
->lock
, flags
);
170 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
175 static int __init
ppc4xx_add_gpiochips(void)
177 struct device_node
*np
;
179 for_each_compatible_node(np
, NULL
, "ibm,ppc4xx-gpio") {
181 struct ppc4xx_gpio_chip
*ppc4xx_gc
;
182 struct of_mm_gpio_chip
*mm_gc
;
183 struct of_gpio_chip
*of_gc
;
184 struct gpio_chip
*gc
;
186 ppc4xx_gc
= kzalloc(sizeof(*ppc4xx_gc
), GFP_KERNEL
);
192 spin_lock_init(&ppc4xx_gc
->lock
);
194 mm_gc
= &ppc4xx_gc
->mm_gc
;
195 of_gc
= &mm_gc
->of_gc
;
198 of_gc
->gpio_cells
= 2;
200 gc
->direction_input
= ppc4xx_gpio_dir_in
;
201 gc
->direction_output
= ppc4xx_gpio_dir_out
;
202 gc
->get
= ppc4xx_gpio_get
;
203 gc
->set
= ppc4xx_gpio_set
;
205 ret
= of_mm_gpiochip_add(np
, mm_gc
);
210 pr_err("%s: registration failed with status %d\n",
213 /* try others anyway */
217 arch_initcall(ppc4xx_add_gpiochips
);