xref: /openbmc/u-boot/drivers/gpio/mvgpio.c (revision 26ddff2d)
1 /*
2  * (C) Copyright 2011
3  * eInfochips Ltd. <www.einfochips.com>
4  * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
5  *
6  * (C) Copyright 2010
7  * Marvell Semiconductor <www.marvell.com>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25  * MA 02110-1301 USA
26  */
27 
28 #include <common.h>
29 #include <asm/io.h>
30 #include <asm/errno.h>
31 #include "mvgpio.h"
32 #include <asm/gpio.h>
33 
34 #ifndef MV_MAX_GPIO
35 #define MV_MAX_GPIO	128
36 #endif
37 
38 int gpio_request(int gp, const char *label)
39 {
40 	if (gp >= MV_MAX_GPIO) {
41 		printf("%s: Invalid GPIO requested %d\n", __func__, gp);
42 		return -EINVAL;
43 	}
44 	return 0;
45 }
46 
47 void gpio_free(int gp)
48 {
49 }
50 
51 void gpio_toggle_value(int gp)
52 {
53 	gpio_set_value(gp, !gpio_get_value(gp));
54 }
55 
56 int gpio_direction_input(int gp)
57 {
58 	struct gpio_reg *gpio_reg_bank;
59 
60 	if (gp >= MV_MAX_GPIO) {
61 		printf("%s: Invalid GPIO %d\n", __func__, gp);
62 		return -EINVAL;
63 	}
64 
65 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gp));
66 	writel(GPIO_TO_BIT(gp), &gpio_reg_bank->gcdr);
67 	return 0;
68 }
69 
70 int gpio_direction_output(int gp, int value)
71 {
72 	struct gpio_reg *gpio_reg_bank;
73 
74 	if (gp >= MV_MAX_GPIO) {
75 		printf("%s: Invalid GPIO %d\n", __func__, gp);
76 		return -EINVAL;
77 	}
78 
79 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gp));
80 	writel(GPIO_TO_BIT(gp), &gpio_reg_bank->gsdr);
81 	gpio_set_value(gp, value);
82 	return 0;
83 }
84 
85 int gpio_get_value(int gp)
86 {
87 	struct gpio_reg *gpio_reg_bank;
88 	u32 gp_val;
89 
90 	if (gp >= MV_MAX_GPIO) {
91 		printf("%s: Invalid GPIO %d\n", __func__, gp);
92 		return -EINVAL;
93 	}
94 
95 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gp));
96 	gp_val = readl(&gpio_reg_bank->gplr);
97 
98 	return GPIO_VAL(gp, gp_val);
99 }
100 
101 void gpio_set_value(int gp, int value)
102 {
103 	struct gpio_reg *gpio_reg_bank;
104 
105 	if (gp >= MV_MAX_GPIO) {
106 		printf("%s: Invalid GPIO %d\n", __func__, gp);
107 		return;
108 	}
109 
110 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gp));
111 	if (value)
112 		writel(GPIO_TO_BIT(gp),	&gpio_reg_bank->gpsr);
113 	else
114 		writel(GPIO_TO_BIT(gp),	&gpio_reg_bank->gpcr);
115 }
116