]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/gpio/mxc_gpio.c
dm:gpio:mxc add a bank_index entry in platdata
[karo-tx-uboot.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_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
43                 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
44         [3] = GPIO4_BASE_ADDR,
45 #endif
46 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
47         [4] = GPIO5_BASE_ADDR,
48         [5] = GPIO6_BASE_ADDR,
49 #endif
50 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_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                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
64                 return -1;
65         }
66
67         gpio &= 0x1f;
68
69         regs = (struct gpio_regs *)gpio_ports[port];
70
71         l = readl(&regs->gpio_dir);
72
73         switch (direction) {
74         case MXC_GPIO_DIRECTION_OUT:
75                 l |= 1 << gpio;
76                 break;
77         case MXC_GPIO_DIRECTION_IN:
78                 l &= ~(1 << gpio);
79         }
80         writel(l, &regs->gpio_dir);
81
82         return 0;
83 }
84
85 int gpio_set_value(unsigned gpio, int value)
86 {
87         unsigned int port = GPIO_TO_PORT(gpio);
88         struct gpio_regs *regs;
89         u32 l;
90
91         if (port >= ARRAY_SIZE(gpio_ports)) {
92                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
93                 return -1;
94         }
95
96         gpio &= 0x1f;
97
98         regs = (struct gpio_regs *)gpio_ports[port];
99
100         l = readl(&regs->gpio_dr);
101         if (value)
102                 l |= 1 << gpio;
103         else
104                 l &= ~(1 << gpio);
105         writel(l, &regs->gpio_dr);
106
107         return 0;
108 }
109
110 int gpio_get_value(unsigned gpio)
111 {
112         unsigned int port = GPIO_TO_PORT(gpio);
113         struct gpio_regs *regs;
114         u32 val;
115
116         if (port >= ARRAY_SIZE(gpio_ports)) {
117                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
118                 return -1;
119         }
120
121         gpio &= 0x1f;
122
123         regs = (struct gpio_regs *)gpio_ports[port];
124
125         if (readl(&regs->gpio_dir) & (1 << gpio)) {
126                 printf("WARNING: Reading status of output GPIO_%d_%d\n",
127                         port - GPIO_TO_PORT(0), gpio);
128                 val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
129         } else {
130                 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
131         }
132         return val;
133 }
134
135 int gpio_request(unsigned gpio, const char *label)
136 {
137         unsigned int port = GPIO_TO_PORT(gpio);
138         if (port >= ARRAY_SIZE(gpio_ports)) {
139                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
140                 return -1;
141         }
142         return 0;
143 }
144
145 int gpio_free(unsigned gpio)
146 {
147         unsigned int port = GPIO_TO_PORT(gpio);
148         if (port >= ARRAY_SIZE(gpio_ports)) {
149                 printf("%s: Invalid GPIO %d\n", __func__, gpio);
150                 return -1;
151         }
152         return 0;
153 }
154
155 int gpio_direction_input(unsigned gpio)
156 {
157         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
158 }
159
160 int gpio_direction_output(unsigned gpio, int value)
161 {
162         int ret = gpio_set_value(gpio, value);
163
164         if (ret < 0)
165                 return ret;
166
167         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
168 }
169 #endif
170
171 #ifdef CONFIG_DM_GPIO
172 #include <fdtdec.h>
173 DECLARE_GLOBAL_DATA_PTR;
174
175 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
176 {
177         u32 val;
178
179         val = readl(&regs->gpio_dir);
180
181         return val & (1 << offset) ? 1 : 0;
182 }
183
184 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
185                                     enum mxc_gpio_direction direction)
186 {
187         u32 l;
188
189         l = readl(&regs->gpio_dir);
190
191         switch (direction) {
192         case MXC_GPIO_DIRECTION_OUT:
193                 l |= 1 << offset;
194                 break;
195         case MXC_GPIO_DIRECTION_IN:
196                 l &= ~(1 << offset);
197         }
198         writel(l, &regs->gpio_dir);
199 }
200
201 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
202                                     int value)
203 {
204         u32 l;
205
206         l = readl(&regs->gpio_dr);
207         if (value)
208                 l |= 1 << offset;
209         else
210                 l &= ~(1 << offset);
211         writel(l, &regs->gpio_dr);
212 }
213
214 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
215 {
216         return (readl(&regs->gpio_psr) >> offset) & 0x01;
217 }
218
219 /* set GPIO pin 'gpio' as an input */
220 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
221 {
222         struct mxc_bank_info *bank = dev_get_priv(dev);
223
224         /* Configure GPIO direction as input. */
225         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
226
227         return 0;
228 }
229
230 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
231 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
232                                        int value)
233 {
234         struct mxc_bank_info *bank = dev_get_priv(dev);
235
236         /* Configure GPIO output value. */
237         mxc_gpio_bank_set_value(bank->regs, offset, value);
238
239         /* Configure GPIO direction as output. */
240         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
241
242         return 0;
243 }
244
245 /* read GPIO IN value of pin 'gpio' */
246 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
247 {
248         struct mxc_bank_info *bank = dev_get_priv(dev);
249
250         return mxc_gpio_bank_get_value(bank->regs, offset);
251 }
252
253 /* write GPIO OUT value to pin 'gpio' */
254 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
255                                  int value)
256 {
257         struct mxc_bank_info *bank = dev_get_priv(dev);
258
259         mxc_gpio_bank_set_value(bank->regs, offset, value);
260
261         return 0;
262 }
263
264 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
265 {
266         struct mxc_bank_info *bank = dev_get_priv(dev);
267
268         /* GPIOF_FUNC is not implemented yet */
269         if (mxc_gpio_is_output(bank->regs, offset))
270                 return GPIOF_OUTPUT;
271         else
272                 return GPIOF_INPUT;
273 }
274
275 static const struct dm_gpio_ops gpio_mxc_ops = {
276         .direction_input        = mxc_gpio_direction_input,
277         .direction_output       = mxc_gpio_direction_output,
278         .get_value              = mxc_gpio_get_value,
279         .set_value              = mxc_gpio_set_value,
280         .get_function           = mxc_gpio_get_function,
281 };
282
283 static const struct mxc_gpio_plat mxc_plat[] = {
284         { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
285         { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
286         { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
287 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
288                 defined(CONFIG_MX53) || defined(CONFIG_MX6)
289         { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
290 #endif
291 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
292         { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
293         { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
294 #endif
295 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
296         { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
297 #endif
298 };
299
300 static int mxc_gpio_probe(struct udevice *dev)
301 {
302         struct mxc_bank_info *bank = dev_get_priv(dev);
303         struct mxc_gpio_plat *plat = dev_get_platdata(dev);
304         struct gpio_dev_priv *uc_priv = dev->uclass_priv;
305         int banknum;
306         char name[18], *str;
307
308         banknum = plat->bank_index;
309         sprintf(name, "GPIO%d_", banknum + 1);
310         str = strdup(name);
311         if (!str)
312                 return -ENOMEM;
313         uc_priv->bank_name = str;
314         uc_priv->gpio_count = GPIO_PER_BANK;
315         bank->regs = plat->regs;
316
317         return 0;
318 }
319
320 static int mxc_gpio_bind(struct udevice *dev)
321 {
322         struct mxc_gpio_plat *plat = dev->platdata;
323         fdt_addr_t addr;
324
325         /*
326          * If platdata already exsits, directly return.
327          * Actually only when DT is not supported, platdata
328          * is statically initialized in U_BOOT_DEVICES.Here
329          * will return.
330          */
331         if (plat)
332                 return 0;
333
334         addr = dev_get_addr(dev);
335         if (addr == FDT_ADDR_T_NONE)
336                 return -ENODEV;
337
338         /*
339          * TODO:
340          * When every board is converted to driver model and DT is supported,
341          * this can be done by auto-alloc feature, but not using calloc
342          * to alloc memory for platdata.
343          */
344         plat = calloc(1, sizeof(*plat));
345         if (!plat)
346                 return -ENOMEM;
347
348         plat->regs = (struct gpio_regs *)addr;
349         plat->bank_index = dev->req_seq;
350         dev->platdata = plat;
351
352         return 0;
353 }
354
355 static const struct udevice_id mxc_gpio_ids[] = {
356         { .compatible = "fsl,imx35-gpio" },
357         { }
358 };
359
360 U_BOOT_DRIVER(gpio_mxc) = {
361         .name   = "gpio_mxc",
362         .id     = UCLASS_GPIO,
363         .ops    = &gpio_mxc_ops,
364         .probe  = mxc_gpio_probe,
365         .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
366         .of_match = mxc_gpio_ids,
367         .bind   = mxc_gpio_bind,
368 };
369
370 #ifndef CONFIG_OF_CONTROL
371 static const struct mxc_gpio_plat mxc_plat[] = {
372         { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
373         { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
374         { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
375 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
376                 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
377         { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
378 #endif
379 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
380         { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
381         { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
382 #endif
383 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
384         { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
385 #endif
386 };
387
388 U_BOOT_DEVICES(mxc_gpios) = {
389         { "gpio_mxc", &mxc_plat[0] },
390         { "gpio_mxc", &mxc_plat[1] },
391         { "gpio_mxc", &mxc_plat[2] },
392 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
393                 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
394         { "gpio_mxc", &mxc_plat[3] },
395 #endif
396 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
397         { "gpio_mxc", &mxc_plat[4] },
398         { "gpio_mxc", &mxc_plat[5] },
399 #endif
400 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
401         { "gpio_mxc", &mxc_plat[6] },
402 #endif
403 };
404 #endif
405 #endif