dm:gpio:mxc add a bank_index entry in platdata
[oweals/u-boot.git] / drivers / gpio / mxc_gpio.c
1 /*
2  * Copyright (C) 2009
3  * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4  *
5  * Copyright (C) 2011
6  * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10 #include <common.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <malloc.h>
14 #include <asm/arch/imx-regs.h>
15 #include <asm/gpio.h>
16 #include <asm/io.h>
17
18 enum mxc_gpio_direction {
19         MXC_GPIO_DIRECTION_IN,
20         MXC_GPIO_DIRECTION_OUT,
21 };
22
23 #define GPIO_PER_BANK                   32
24
25 struct mxc_gpio_plat {
26         int bank_index;
27         struct gpio_regs *regs;
28 };
29
30 struct mxc_bank_info {
31         struct gpio_regs *regs;
32 };
33
34 #ifndef CONFIG_DM_GPIO
35 #define GPIO_TO_PORT(n)         (n / 32)
36
37 /* GPIO port description */
38 static unsigned long gpio_ports[] = {
39         [0] = GPIO1_BASE_ADDR,
40         [1] = GPIO2_BASE_ADDR,
41         [2] = GPIO3_BASE_ADDR,
42 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
43                 defined(CONFIG_MX53) || defined(CONFIG_MX6)
44         [3] = GPIO4_BASE_ADDR,
45 #endif
46 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
47         [4] = GPIO5_BASE_ADDR,
48         [5] = GPIO6_BASE_ADDR,
49 #endif
50 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
51         [6] = GPIO7_BASE_ADDR,
52 #endif
53 };
54
55 static int mxc_gpio_direction(unsigned int gpio,
56         enum mxc_gpio_direction direction)
57 {
58         unsigned int port = GPIO_TO_PORT(gpio);
59         struct gpio_regs *regs;
60         u32 l;
61
62         if (port >= ARRAY_SIZE(gpio_ports))
63                 return -1;
64
65         gpio &= 0x1f;
66
67         regs = (struct gpio_regs *)gpio_ports[port];
68
69         l = readl(&regs->gpio_dir);
70
71         switch (direction) {
72         case MXC_GPIO_DIRECTION_OUT:
73                 l |= 1 << gpio;
74                 break;
75         case MXC_GPIO_DIRECTION_IN:
76                 l &= ~(1 << gpio);
77         }
78         writel(l, &regs->gpio_dir);
79
80         return 0;
81 }
82
83 int gpio_set_value(unsigned gpio, int value)
84 {
85         unsigned int port = GPIO_TO_PORT(gpio);
86         struct gpio_regs *regs;
87         u32 l;
88
89         if (port >= ARRAY_SIZE(gpio_ports))
90                 return -1;
91
92         gpio &= 0x1f;
93
94         regs = (struct gpio_regs *)gpio_ports[port];
95
96         l = readl(&regs->gpio_dr);
97         if (value)
98                 l |= 1 << gpio;
99         else
100                 l &= ~(1 << gpio);
101         writel(l, &regs->gpio_dr);
102
103         return 0;
104 }
105
106 int gpio_get_value(unsigned gpio)
107 {
108         unsigned int port = GPIO_TO_PORT(gpio);
109         struct gpio_regs *regs;
110         u32 val;
111
112         if (port >= ARRAY_SIZE(gpio_ports))
113                 return -1;
114
115         gpio &= 0x1f;
116
117         regs = (struct gpio_regs *)gpio_ports[port];
118
119         val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
120
121         return val;
122 }
123
124 int gpio_request(unsigned gpio, const char *label)
125 {
126         unsigned int port = GPIO_TO_PORT(gpio);
127         if (port >= ARRAY_SIZE(gpio_ports))
128                 return -1;
129         return 0;
130 }
131
132 int gpio_free(unsigned gpio)
133 {
134         return 0;
135 }
136
137 int gpio_direction_input(unsigned gpio)
138 {
139         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
140 }
141
142 int gpio_direction_output(unsigned gpio, int value)
143 {
144         int ret = gpio_set_value(gpio, value);
145
146         if (ret < 0)
147                 return ret;
148
149         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
150 }
151 #endif
152
153 #ifdef CONFIG_DM_GPIO
154 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
155 {
156         u32 val;
157
158         val = readl(&regs->gpio_dir);
159
160         return val & (1 << offset) ? 1 : 0;
161 }
162
163 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
164                                     enum mxc_gpio_direction direction)
165 {
166         u32 l;
167
168         l = readl(&regs->gpio_dir);
169
170         switch (direction) {
171         case MXC_GPIO_DIRECTION_OUT:
172                 l |= 1 << offset;
173                 break;
174         case MXC_GPIO_DIRECTION_IN:
175                 l &= ~(1 << offset);
176         }
177         writel(l, &regs->gpio_dir);
178 }
179
180 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
181                                     int value)
182 {
183         u32 l;
184
185         l = readl(&regs->gpio_dr);
186         if (value)
187                 l |= 1 << offset;
188         else
189                 l &= ~(1 << offset);
190         writel(l, &regs->gpio_dr);
191 }
192
193 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
194 {
195         return (readl(&regs->gpio_psr) >> offset) & 0x01;
196 }
197
198 /* set GPIO pin 'gpio' as an input */
199 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
200 {
201         struct mxc_bank_info *bank = dev_get_priv(dev);
202
203         /* Configure GPIO direction as input. */
204         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
205
206         return 0;
207 }
208
209 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
210 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
211                                        int value)
212 {
213         struct mxc_bank_info *bank = dev_get_priv(dev);
214
215         /* Configure GPIO output value. */
216         mxc_gpio_bank_set_value(bank->regs, offset, value);
217
218         /* Configure GPIO direction as output. */
219         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
220
221         return 0;
222 }
223
224 /* read GPIO IN value of pin 'gpio' */
225 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
226 {
227         struct mxc_bank_info *bank = dev_get_priv(dev);
228
229         return mxc_gpio_bank_get_value(bank->regs, offset);
230 }
231
232 /* write GPIO OUT value to pin 'gpio' */
233 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
234                                  int value)
235 {
236         struct mxc_bank_info *bank = dev_get_priv(dev);
237
238         mxc_gpio_bank_set_value(bank->regs, offset, value);
239
240         return 0;
241 }
242
243 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
244 {
245         struct mxc_bank_info *bank = dev_get_priv(dev);
246
247         /* GPIOF_FUNC is not implemented yet */
248         if (mxc_gpio_is_output(bank->regs, offset))
249                 return GPIOF_OUTPUT;
250         else
251                 return GPIOF_INPUT;
252 }
253
254 static const struct dm_gpio_ops gpio_mxc_ops = {
255         .direction_input        = mxc_gpio_direction_input,
256         .direction_output       = mxc_gpio_direction_output,
257         .get_value              = mxc_gpio_get_value,
258         .set_value              = mxc_gpio_set_value,
259         .get_function           = mxc_gpio_get_function,
260 };
261
262 static const struct mxc_gpio_plat mxc_plat[] = {
263         { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
264         { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
265         { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
266 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
267                 defined(CONFIG_MX53) || defined(CONFIG_MX6)
268         { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
269 #endif
270 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
271         { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
272         { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
273 #endif
274 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
275         { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
276 #endif
277 };
278
279 static int mxc_gpio_probe(struct udevice *dev)
280 {
281         struct mxc_bank_info *bank = dev_get_priv(dev);
282         struct mxc_gpio_plat *plat = dev_get_platdata(dev);
283         struct gpio_dev_priv *uc_priv = dev->uclass_priv;
284         int banknum;
285         char name[18], *str;
286
287         banknum = plat->bank_index;
288         sprintf(name, "GPIO%d_", banknum + 1);
289         str = strdup(name);
290         if (!str)
291                 return -ENOMEM;
292         uc_priv->bank_name = str;
293         uc_priv->gpio_count = GPIO_PER_BANK;
294         bank->regs = plat->regs;
295
296         return 0;
297 }
298
299 U_BOOT_DRIVER(gpio_mxc) = {
300         .name   = "gpio_mxc",
301         .id     = UCLASS_GPIO,
302         .ops    = &gpio_mxc_ops,
303         .probe  = mxc_gpio_probe,
304         .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
305 };
306
307 U_BOOT_DEVICES(mxc_gpios) = {
308         { "gpio_mxc", &mxc_plat[0] },
309         { "gpio_mxc", &mxc_plat[1] },
310         { "gpio_mxc", &mxc_plat[2] },
311 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
312                 defined(CONFIG_MX53) || defined(CONFIG_MX6)
313         { "gpio_mxc", &mxc_plat[3] },
314 #endif
315 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
316         { "gpio_mxc", &mxc_plat[4] },
317         { "gpio_mxc", &mxc_plat[5] },
318 #endif
319 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
320         { "gpio_mxc", &mxc_plat[6] },
321 #endif
322 };
323 #endif