blob: 4efb768551c95a480cf30d7456fc07f8fddad75a [file] [log] [blame]
Minkyu Kang1edf0f22010-02-12 18:17:52 +09001/*
2 * (C) Copyright 2009 Samsung Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20
21#include <common.h>
22#include <asm/io.h>
Joe Hershbergerf8928f12011-11-11 15:55:36 -060023#include <asm/gpio.h>
Minkyu Kang1edf0f22010-02-12 18:17:52 +090024
25#define CON_MASK(x) (0xf << ((x) << 2))
26#define CON_SFR(x, v) ((v) << ((x) << 2))
27
28#define DAT_MASK(x) (0x1 << (x))
29#define DAT_SET(x) (0x1 << (x))
30
31#define PULL_MASK(x) (0x3 << ((x) << 1))
32#define PULL_MODE(x, v) ((v) << ((x) << 1))
33
34#define DRV_MASK(x) (0x3 << ((x) << 1))
35#define DRV_SET(x, m) ((m) << ((x) << 1))
36#define RATE_MASK(x) (0x1 << (x + 16))
37#define RATE_SET(x) (0x1 << (x + 16))
38
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000039void s5p_gpio_cfg_pin(struct s5p_gpio_bank *bank, int gpio, int cfg)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090040{
41 unsigned int value;
42
43 value = readl(&bank->con);
44 value &= ~CON_MASK(gpio);
45 value |= CON_SFR(gpio, cfg);
46 writel(value, &bank->con);
47}
48
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000049void s5p_gpio_direction_output(struct s5p_gpio_bank *bank, int gpio, int en)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090050{
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000051 s5p_gpio_cfg_pin(bank, gpio, GPIO_OUTPUT);
Axel Lin7b885cf2013-06-15 22:19:45 +080052 s5p_gpio_set_value(bank, gpio, en);
Minkyu Kang1edf0f22010-02-12 18:17:52 +090053}
54
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000055void s5p_gpio_direction_input(struct s5p_gpio_bank *bank, int gpio)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090056{
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000057 s5p_gpio_cfg_pin(bank, gpio, GPIO_INPUT);
Minkyu Kang1edf0f22010-02-12 18:17:52 +090058}
59
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000060void s5p_gpio_set_value(struct s5p_gpio_bank *bank, int gpio, int en)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090061{
62 unsigned int value;
63
64 value = readl(&bank->dat);
65 value &= ~DAT_MASK(gpio);
66 if (en)
67 value |= DAT_SET(gpio);
68 writel(value, &bank->dat);
69}
70
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000071unsigned int s5p_gpio_get_value(struct s5p_gpio_bank *bank, int gpio)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090072{
73 unsigned int value;
74
75 value = readl(&bank->dat);
76 return !!(value & DAT_MASK(gpio));
77}
78
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000079void s5p_gpio_set_pull(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090080{
81 unsigned int value;
82
83 value = readl(&bank->pull);
84 value &= ~PULL_MASK(gpio);
85
86 switch (mode) {
87 case GPIO_PULL_DOWN:
88 case GPIO_PULL_UP:
89 value |= PULL_MODE(gpio, mode);
90 break;
91 default:
Minkyu Kangced528f2010-05-28 12:34:29 +090092 break;
Minkyu Kang1edf0f22010-02-12 18:17:52 +090093 }
94
95 writel(value, &bank->pull);
96}
97
Łukasz Majewski4d954cc2011-07-15 00:16:22 +000098void s5p_gpio_set_drv(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +090099{
100 unsigned int value;
101
102 value = readl(&bank->drv);
103 value &= ~DRV_MASK(gpio);
104
105 switch (mode) {
106 case GPIO_DRV_1X:
107 case GPIO_DRV_2X:
108 case GPIO_DRV_3X:
109 case GPIO_DRV_4X:
110 value |= DRV_SET(gpio, mode);
111 break;
112 default:
113 return;
114 }
115
116 writel(value, &bank->drv);
117}
118
Łukasz Majewski4d954cc2011-07-15 00:16:22 +0000119void s5p_gpio_set_rate(struct s5p_gpio_bank *bank, int gpio, int mode)
Minkyu Kang1edf0f22010-02-12 18:17:52 +0900120{
121 unsigned int value;
122
123 value = readl(&bank->drv);
124 value &= ~RATE_MASK(gpio);
125
126 switch (mode) {
127 case GPIO_DRV_FAST:
128 case GPIO_DRV_SLOW:
129 value |= RATE_SET(gpio);
130 break;
131 default:
132 return;
133 }
134
135 writel(value, &bank->drv);
136}
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000137
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600138struct s5p_gpio_bank *s5p_gpio_get_bank(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000139{
Łukasz Majewskid6614b72012-09-04 21:47:46 +0000140 int bank;
141 unsigned g = gpio - s5p_gpio_part_max(gpio);
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000142
Łukasz Majewskid6614b72012-09-04 21:47:46 +0000143 bank = g / GPIO_PER_BANK;
144 bank *= sizeof(struct s5p_gpio_bank);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600145 return (struct s5p_gpio_bank *) (s5p_gpio_base(gpio) + bank);
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000146}
147
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600148int s5p_gpio_get_pin(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000149{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600150 return gpio % GPIO_PER_BANK;
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000151}
152
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600153/* Common GPIO API */
154
155int gpio_request(unsigned gpio, const char *label)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000156{
157 return 0;
158}
159
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600160int gpio_free(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000161{
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000162 return 0;
163}
164
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600165int gpio_direction_input(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000166{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600167 s5p_gpio_direction_input(s5p_gpio_get_bank(gpio),
168 s5p_gpio_get_pin(gpio));
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000169 return 0;
170}
171
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600172int gpio_direction_output(unsigned gpio, int value)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000173{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600174 s5p_gpio_direction_output(s5p_gpio_get_bank(gpio),
175 s5p_gpio_get_pin(gpio), value);
176 return 0;
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000177}
178
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600179int gpio_get_value(unsigned gpio)
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000180{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600181 return (int) s5p_gpio_get_value(s5p_gpio_get_bank(gpio),
182 s5p_gpio_get_pin(gpio));
183}
184
185int gpio_set_value(unsigned gpio, int value)
186{
187 s5p_gpio_set_value(s5p_gpio_get_bank(gpio),
188 s5p_gpio_get_pin(gpio), value);
189
190 return 0;
Łukasz Majewski1e04cae2011-08-22 22:34:58 +0000191}