]> Git Repo - J-u-boot.git/blob - drivers/gpio/mxc_gpio.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-u-boot.git] / drivers / gpio / mxc_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2009
4  * Guennadi Liakhovetski, DENX Software Engineering, <[email protected]>
5  *
6  * Copyright (C) 2011
7  * Stefano Babic, DENX Software Engineering, <[email protected]>
8  */
9 #include <errno.h>
10 #include <dm.h>
11 #include <malloc.h>
12 #include <asm/arch/imx-regs.h>
13 #include <asm/gpio.h>
14 #include <asm/io.h>
15 #include <dt-structs.h>
16 #include <mapmem.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 #if CONFIG_IS_ENABLED(OF_PLATDATA)
27         /* Put this first since driver model will copy the data here */
28         struct dtd_gpio_mxc dtplat;
29 #endif
30         int bank_index;
31         struct gpio_regs *regs;
32 };
33
34 struct mxc_bank_info {
35         struct gpio_regs *regs;
36 };
37
38 #if !CONFIG_IS_ENABLED(DM_GPIO)
39 #define GPIO_TO_PORT(n)         ((n) / 32)
40
41 /* GPIO port description */
42 static unsigned long gpio_ports[] = {
43         [0] = GPIO1_BASE_ADDR,
44         [1] = GPIO2_BASE_ADDR,
45         [2] = GPIO3_BASE_ADDR,
46 #if defined(CONFIG_MX51) || \
47                 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
48                 defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \
49                 defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050)
50         [3] = GPIO4_BASE_ADDR,
51 #endif
52 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
53                 defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \
54                 defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050)
55         [4] = GPIO5_BASE_ADDR,
56 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || \
57                 defined(CONFIG_IMX8M) || defined(CONFIG_IMXRT1050))
58         [5] = GPIO6_BASE_ADDR,
59 #endif
60 #endif
61 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
62                 defined(CONFIG_ARCH_IMX8)
63 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL))
64         [6] = GPIO7_BASE_ADDR,
65 #endif
66 #endif
67 #if defined(CONFIG_ARCH_IMX8)
68         [7] = GPIO8_BASE_ADDR,
69 #endif
70 };
71
72 static int mxc_gpio_direction(unsigned int gpio,
73         enum mxc_gpio_direction direction)
74 {
75         unsigned int port = GPIO_TO_PORT(gpio);
76         struct gpio_regs *regs;
77         u32 l;
78
79         if (port >= ARRAY_SIZE(gpio_ports))
80                 return -1;
81
82         gpio &= 0x1f;
83
84         regs = (struct gpio_regs *)gpio_ports[port];
85
86         l = readl(&regs->gpio_dir);
87
88         switch (direction) {
89         case MXC_GPIO_DIRECTION_OUT:
90                 l |= 1 << gpio;
91                 break;
92         case MXC_GPIO_DIRECTION_IN:
93                 l &= ~(1 << gpio);
94         }
95         writel(l, &regs->gpio_dir);
96
97         return 0;
98 }
99
100 int gpio_set_value(unsigned gpio, int value)
101 {
102         unsigned int port = GPIO_TO_PORT(gpio);
103         struct gpio_regs *regs;
104         u32 l;
105
106         if (port >= ARRAY_SIZE(gpio_ports))
107                 return -1;
108
109         gpio &= 0x1f;
110
111         regs = (struct gpio_regs *)gpio_ports[port];
112
113         l = readl(&regs->gpio_dr);
114         if (value)
115                 l |= 1 << gpio;
116         else
117                 l &= ~(1 << gpio);
118         writel(l, &regs->gpio_dr);
119
120         return 0;
121 }
122
123 int gpio_get_value(unsigned gpio)
124 {
125         unsigned int port = GPIO_TO_PORT(gpio);
126         struct gpio_regs *regs;
127         u32 val;
128
129         if (port >= ARRAY_SIZE(gpio_ports))
130                 return -1;
131
132         gpio &= 0x1f;
133
134         regs = (struct gpio_regs *)gpio_ports[port];
135
136         if ((readl(&regs->gpio_dir) >> gpio) & 0x01)
137                 val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
138         else
139                 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
140
141         return val;
142 }
143
144 int gpio_request(unsigned gpio, const char *label)
145 {
146         unsigned int port = GPIO_TO_PORT(gpio);
147         if (port >= ARRAY_SIZE(gpio_ports))
148                 return -1;
149         return 0;
150 }
151
152 int gpio_free(unsigned gpio)
153 {
154         return 0;
155 }
156
157 int gpio_direction_input(unsigned gpio)
158 {
159         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
160 }
161
162 int gpio_direction_output(unsigned gpio, int value)
163 {
164         int ret = gpio_set_value(gpio, value);
165
166         if (ret < 0)
167                 return ret;
168
169         return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
170 }
171 #endif
172
173 #if CONFIG_IS_ENABLED(DM_GPIO)
174 #include <fdtdec.h>
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         if ((readl(&regs->gpio_dir) >> offset) & 0x01)
217                 return (readl(&regs->gpio_dr) >> offset) & 0x01;
218         else
219                 return (readl(&regs->gpio_psr) >> offset) & 0x01;
220 }
221
222 /* set GPIO pin 'gpio' as an input */
223 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
224 {
225         struct mxc_bank_info *bank = dev_get_priv(dev);
226
227         /* Configure GPIO direction as input. */
228         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
229
230         return 0;
231 }
232
233 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
234 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
235                                        int value)
236 {
237         struct mxc_bank_info *bank = dev_get_priv(dev);
238
239         /* Configure GPIO output value. */
240         mxc_gpio_bank_set_value(bank->regs, offset, value);
241
242         /* Configure GPIO direction as output. */
243         mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
244
245         return 0;
246 }
247
248 /* read GPIO IN value of pin 'gpio' */
249 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
250 {
251         struct mxc_bank_info *bank = dev_get_priv(dev);
252
253         return mxc_gpio_bank_get_value(bank->regs, offset);
254 }
255
256 /* write GPIO OUT value to pin 'gpio' */
257 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
258                                  int value)
259 {
260         struct mxc_bank_info *bank = dev_get_priv(dev);
261
262         mxc_gpio_bank_set_value(bank->regs, offset, value);
263
264         return 0;
265 }
266
267 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
268 {
269         struct mxc_bank_info *bank = dev_get_priv(dev);
270
271         /* GPIOF_FUNC is not implemented yet */
272         if (mxc_gpio_is_output(bank->regs, offset))
273                 return GPIOF_OUTPUT;
274         else
275                 return GPIOF_INPUT;
276 }
277
278 static const struct dm_gpio_ops gpio_mxc_ops = {
279         .direction_input        = mxc_gpio_direction_input,
280         .direction_output       = mxc_gpio_direction_output,
281         .get_value              = mxc_gpio_get_value,
282         .set_value              = mxc_gpio_set_value,
283         .get_function           = mxc_gpio_get_function,
284 };
285
286 static int mxc_gpio_probe(struct udevice *dev)
287 {
288         struct mxc_bank_info *bank = dev_get_priv(dev);
289         struct mxc_gpio_plat *plat = dev_get_plat(dev);
290         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
291         int banknum;
292         char name[18], *str;
293
294 #if CONFIG_IS_ENABLED(OF_PLATDATA)
295         struct dtd_gpio_mxc *dtplat = &plat->dtplat;
296
297         plat->regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
298 #endif
299
300         banknum = plat->bank_index;
301         if (IS_ENABLED(CONFIG_ARCH_IMX8))
302                 sprintf(name, "GPIO%d_", banknum);
303         else
304                 sprintf(name, "GPIO%d_", banknum + 1);
305         str = strdup(name);
306         if (!str)
307                 return -ENOMEM;
308         uc_priv->bank_name = str;
309         uc_priv->gpio_count = GPIO_PER_BANK;
310         bank->regs = plat->regs;
311
312         return 0;
313 }
314
315 static int mxc_gpio_of_to_plat(struct udevice *dev)
316 {
317         struct mxc_gpio_plat *plat = dev_get_plat(dev);
318         if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
319                 fdt_addr_t addr;
320                 addr = dev_read_addr(dev);
321                 if (addr == FDT_ADDR_T_NONE)
322                         return -EINVAL;
323
324                 plat->regs = (struct gpio_regs *)addr;
325         }
326         plat->bank_index = dev_seq(dev);
327
328         return 0;
329 }
330
331 static int mxc_gpio_bind(struct udevice *dev)
332 {
333         return 0;
334 }
335
336 static const struct udevice_id mxc_gpio_ids[] = {
337         { .compatible = "fsl,imx35-gpio" },
338         { }
339 };
340
341 U_BOOT_DRIVER(gpio_mxc) = {
342         .name   = "gpio_mxc",
343         .id     = UCLASS_GPIO,
344         .ops    = &gpio_mxc_ops,
345         .probe  = mxc_gpio_probe,
346         .of_to_plat = mxc_gpio_of_to_plat,
347         .plat_auto      = sizeof(struct mxc_gpio_plat),
348         .priv_auto      = sizeof(struct mxc_bank_info),
349         .of_match = mxc_gpio_ids,
350         .bind   = mxc_gpio_bind,
351 };
352
353 DM_DRIVER_ALIAS(gpio_mxc, fsl_imx6q_gpio)
354
355 #if !CONFIG_IS_ENABLED(OF_CONTROL)
356 static const struct mxc_gpio_plat mxc_plat[] = {
357         { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
358         { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
359         { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
360 #if defined(CONFIG_MX51) || \
361                 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
362                 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
363         { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
364 #endif
365 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
366                 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
367         { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
368 #ifndef CONFIG_IMX8M
369         { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
370 #endif
371 #endif
372 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8)
373         { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
374 #endif
375 #if defined(CONFIG_ARCH_IMX8)
376         { 7, (struct gpio_regs *)GPIO8_BASE_ADDR },
377 #endif
378 };
379
380 U_BOOT_DRVINFOS(mxc_gpios) = {
381         { "gpio_mxc", &mxc_plat[0] },
382         { "gpio_mxc", &mxc_plat[1] },
383         { "gpio_mxc", &mxc_plat[2] },
384 #if defined(CONFIG_MX51) || \
385                 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
386                 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
387         { "gpio_mxc", &mxc_plat[3] },
388 #endif
389 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
390                 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
391         { "gpio_mxc", &mxc_plat[4] },
392 #ifndef CONFIG_IMX8M
393         { "gpio_mxc", &mxc_plat[5] },
394 #endif
395 #endif
396 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8)
397         { "gpio_mxc", &mxc_plat[6] },
398 #endif
399 #if defined(CONFIG_ARCH_IMX8)
400         { "gpio_mxc", &mxc_plat[7] },
401 #endif
402 };
403 #endif
404 #endif
This page took 0.047597 seconds and 4 git commands to generate.