blob: 1aceafcdf58171479f2f28503898122abdfcf75f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Tom Rixdd2d1cc2009-05-15 23:48:36 +02002/*
3 * Copyright (c) 2009 Wind River Systems, Inc.
4 * Tom Rix <Tom.Rix@windriver.com>
5 *
Tom Rixdd2d1cc2009-05-15 23:48:36 +02006 * This work is derived from the linux 2.6.27 kernel source
7 * To fetch, use the kernel repository
8 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
9 * Use the v2.6.27 tag.
10 *
11 * Below is the original's header including its copyright
12 *
13 * linux/arch/arm/plat-omap/gpio.c
14 *
15 * Support functions for OMAP GPIO
16 *
17 * Copyright (C) 2003-2005 Nokia Corporation
18 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tom Rixdd2d1cc2009-05-15 23:48:36 +020019 */
Simon Glass1a96d7f2014-10-22 21:37:09 -060020#include <dm.h>
Tom Rini7bc2bca2015-07-31 19:55:09 -040021#include <fdtdec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Joe Hershbergerf8928f12011-11-11 15:55:36 -060023#include <asm/gpio.h>
Tom Rixdd2d1cc2009-05-15 23:48:36 +020024#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070025#include <dm/device-internal.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090026#include <linux/errno.h>
Tom Rini7bc2bca2015-07-31 19:55:09 -040027#include <malloc.h>
28
29DECLARE_GLOBAL_DATA_PTR;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020030
Sanjeev Premiea2cf552011-09-08 10:47:25 -040031#define OMAP_GPIO_DIR_OUT 0
32#define OMAP_GPIO_DIR_IN 1
33
Simon Glassfa4689a2019-12-06 21:41:35 -070034#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass1a96d7f2014-10-22 21:37:09 -060035
Simon Glass1a96d7f2014-10-22 21:37:09 -060036#define GPIO_PER_BANK 32
37
38struct gpio_bank {
Simon Glass1a96d7f2014-10-22 21:37:09 -060039 /* TODO(sjg@chromium.org): Can we use a struct here? */
40 void *base; /* address of registers in physical memory */
Simon Glass1a96d7f2014-10-22 21:37:09 -060041};
42
43#endif
44
Nikita Kiryanove3cb9c12012-11-27 22:40:57 +000045int gpio_is_valid(int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020046{
Axel Lin01a461f2013-06-21 18:54:25 +080047 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
Tom Rixdd2d1cc2009-05-15 23:48:36 +020048}
49
Aneesh V9a390882011-07-21 09:29:29 -040050static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
51 int is_input)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020052{
53 void *reg = bank->base;
54 u32 l;
55
Tom Rini7bc2bca2015-07-31 19:55:09 -040056 reg += OMAP_GPIO_OE;
57
Tom Rixdd2d1cc2009-05-15 23:48:36 +020058 l = __raw_readl(reg);
59 if (is_input)
60 l |= 1 << gpio;
61 else
62 l &= ~(1 << gpio);
63 __raw_writel(l, reg);
64}
65
Sanjeev Premiea2cf552011-09-08 10:47:25 -040066/**
67 * Get the direction of the GPIO by reading the GPIO_OE register
68 * corresponding to the specified bank.
69 */
70static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020071{
Sanjeev Premiea2cf552011-09-08 10:47:25 -040072 void *reg = bank->base;
73 u32 v;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020074
Tom Rini7bc2bca2015-07-31 19:55:09 -040075 reg += OMAP_GPIO_OE;
Sanjeev Premiea2cf552011-09-08 10:47:25 -040076
77 v = __raw_readl(reg);
78
79 if (v & (1 << gpio))
80 return OMAP_GPIO_DIR_IN;
81 else
82 return OMAP_GPIO_DIR_OUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020083}
84
Aneesh V9a390882011-07-21 09:29:29 -040085static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
86 int enable)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020087{
88 void *reg = bank->base;
89 u32 l = 0;
90
Tom Rini7bc2bca2015-07-31 19:55:09 -040091 if (enable)
92 reg += OMAP_GPIO_SETDATAOUT;
93 else
94 reg += OMAP_GPIO_CLEARDATAOUT;
95
96 l = 1 << gpio;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020097 __raw_writel(l, reg);
98}
99
Simon Glassba33a3f2014-10-22 21:37:08 -0600100static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
101{
102 void *reg = bank->base;
103 int input;
104
Tom Rini7bc2bca2015-07-31 19:55:09 -0400105 input = _get_gpio_direction(bank, gpio);
106 switch (input) {
107 case OMAP_GPIO_DIR_IN:
108 reg += OMAP_GPIO_DATAIN;
109 break;
110 case OMAP_GPIO_DIR_OUT:
111 reg += OMAP_GPIO_DATAOUT;
Simon Glassba33a3f2014-10-22 21:37:08 -0600112 break;
113 default:
114 return -1;
115 }
116
117 return (__raw_readl(reg) & (1 << gpio)) != 0;
118}
119
Simon Glassfa4689a2019-12-06 21:41:35 -0700120#if !CONFIG_IS_ENABLED(DM_GPIO)
Tom Rini74d6c9a2020-06-04 16:01:39 -0400121static inline int get_gpio_index(int gpio)
122{
123 return gpio & 0x1f;
124}
Simon Glass1a96d7f2014-10-22 21:37:09 -0600125
Simon Glassba33a3f2014-10-22 21:37:08 -0600126static inline const struct gpio_bank *get_gpio_bank(int gpio)
127{
128 return &omap_gpio_bank[gpio >> 5];
129}
130
131static int check_gpio(int gpio)
132{
133 if (!gpio_is_valid(gpio)) {
134 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
135 return -1;
136 }
137 return 0;
138}
139
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400140/**
141 * Set value of the specified gpio
142 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600143int gpio_set_value(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200144{
Aneesh V9a390882011-07-21 09:29:29 -0400145 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200146
147 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600148 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200149 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400150 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600151
152 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200153}
154
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400155/**
156 * Get value of the specified gpio
157 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600158int gpio_get_value(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200159{
Aneesh V9a390882011-07-21 09:29:29 -0400160 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200161
162 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600163 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200164 bank = get_gpio_bank(gpio);
Simon Glassba33a3f2014-10-22 21:37:08 -0600165
166 return _get_gpio_value(bank, get_gpio_index(gpio));
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200167}
168
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400169/**
170 * Set gpio direction as input
171 */
172int gpio_direction_input(unsigned gpio)
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500173{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400174 const struct gpio_bank *bank;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500175
176 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600177 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400178
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500179 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400180 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
181
182 return 0;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500183}
184
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400185/**
186 * Set gpio direction as output
187 */
188int gpio_direction_output(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200189{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400190 const struct gpio_bank *bank;
191
192 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600193 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400194
195 bank = get_gpio_bank(gpio);
196 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
197 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
198
199 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200200}
201
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400202/**
203 * Request a gpio before using it.
204 *
205 * NOTE: Argument 'label' is unused.
206 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600207int gpio_request(unsigned gpio, const char *label)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200208{
209 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600210 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200211
212 return 0;
213}
214
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400215/**
216 * Reset and free the gpio after using it.
217 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600218int gpio_free(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200219{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600220 return 0;
221}
222
223#else /* new driver model interface CONFIG_DM_GPIO */
224
Simon Glass1a96d7f2014-10-22 21:37:09 -0600225/* set GPIO pin 'gpio' as an input */
226static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
227{
228 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600229
230 /* Configure GPIO direction as input. */
231 _set_gpio_direction(bank, offset, 1);
232
233 return 0;
234}
235
236/* set GPIO pin 'gpio' as an output, with polarity 'value' */
237static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
238 int value)
239{
240 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600241
242 _set_gpio_dataout(bank, offset, value);
243 _set_gpio_direction(bank, offset, 0);
244
245 return 0;
246}
247
248/* read GPIO IN value of pin 'gpio' */
249static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
250{
251 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600252
253 return _get_gpio_value(bank, offset);
254}
255
256/* write GPIO OUT value to pin 'gpio' */
257static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
258 int value)
259{
260 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600261
262 _set_gpio_dataout(bank, offset, value);
263
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600264 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200265}
Simon Glass1a96d7f2014-10-22 21:37:09 -0600266
Simon Glass1a96d7f2014-10-22 21:37:09 -0600267static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
268{
269 struct gpio_bank *bank = dev_get_priv(dev);
270
Simon Glass1a96d7f2014-10-22 21:37:09 -0600271 /* GPIOF_FUNC is not implemented yet */
Axel Linbb4ab582015-01-31 22:23:38 +0800272 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
Simon Glass1a96d7f2014-10-22 21:37:09 -0600273 return GPIOF_OUTPUT;
274 else
275 return GPIOF_INPUT;
276}
277
278static const struct dm_gpio_ops gpio_omap_ops = {
Simon Glass1a96d7f2014-10-22 21:37:09 -0600279 .direction_input = omap_gpio_direction_input,
280 .direction_output = omap_gpio_direction_output,
281 .get_value = omap_gpio_get_value,
282 .set_value = omap_gpio_set_value,
283 .get_function = omap_gpio_get_function,
Simon Glass1a96d7f2014-10-22 21:37:09 -0600284};
285
286static int omap_gpio_probe(struct udevice *dev)
287{
288 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700289 struct omap_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700290 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Adam Ford22f2e932018-06-11 20:05:38 -0500291 char name[18], *str;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600292
Adam Forde387e452018-08-17 14:37:58 -0500293 sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
Adam Ford22f2e932018-06-11 20:05:38 -0500294 str = strdup(name);
295 if (!str)
296 return -ENOMEM;
297 uc_priv->bank_name = str;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600298 uc_priv->gpio_count = GPIO_PER_BANK;
299 bank->base = (void *)plat->base;
Tom Rini7bc2bca2015-07-31 19:55:09 -0400300 return 0;
301}
302
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500303#if !CONFIG_IS_ENABLED(OF_CONTROL)
Tom Rini7bc2bca2015-07-31 19:55:09 -0400304static int omap_gpio_bind(struct udevice *dev)
305{
Simon Glassb75b15b2020-12-03 16:55:23 -0700306 struct omap_gpio_plat *plat = dev_get_plat(dev);
Tom Rini7bc2bca2015-07-31 19:55:09 -0400307 fdt_addr_t base_addr;
308
309 if (plat)
310 return 0;
311
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900312 base_addr = dev_read_addr(dev);
Tom Rini7bc2bca2015-07-31 19:55:09 -0400313 if (base_addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600314 return -EINVAL;
Tom Rini7bc2bca2015-07-31 19:55:09 -0400315
316 /*
317 * TODO:
318 * When every board is converted to driver model and DT is
319 * supported, this can be done by auto-alloc feature, but
Simon Glass71fa5b42020-12-03 16:55:18 -0700320 * not using calloc to alloc memory for plat.
Simon Glass87f4cb72017-09-17 16:54:52 -0600321 *
322 * For example am33xx_gpio uses platform data rather than device tree.
323 *
324 * NOTE: DO NOT COPY this code if you are using device tree.
Tom Rini7bc2bca2015-07-31 19:55:09 -0400325 */
326 plat = calloc(1, sizeof(*plat));
327 if (!plat)
328 return -ENOMEM;
329
330 plat->base = base_addr;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700331 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
Simon Glass95588622020-12-22 19:30:28 -0700332 dev_set_plat(dev, plat);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600333
334 return 0;
335}
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500336#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600337
Simon Glass3580f6d2021-08-07 07:24:03 -0600338#if CONFIG_IS_ENABLED(OF_REAL)
Tom Rini7bc2bca2015-07-31 19:55:09 -0400339static const struct udevice_id omap_gpio_ids[] = {
340 { .compatible = "ti,omap3-gpio" },
341 { .compatible = "ti,omap4-gpio" },
342 { .compatible = "ti,am4372-gpio" },
343 { }
344};
345
Simon Glassaad29ae2020-12-03 16:55:21 -0700346static int omap_gpio_of_to_plat(struct udevice *dev)
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500347{
Simon Glassb75b15b2020-12-03 16:55:23 -0700348 struct omap_gpio_plat *plat = dev_get_plat(dev);
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500349 fdt_addr_t addr;
350
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900351 addr = dev_read_addr(dev);
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500352 if (addr == FDT_ADDR_T_NONE)
353 return -EINVAL;
354
355 plat->base = addr;
356 return 0;
357}
358#endif
359
Simon Glass1a96d7f2014-10-22 21:37:09 -0600360U_BOOT_DRIVER(gpio_omap) = {
361 .name = "gpio_omap",
362 .id = UCLASS_GPIO,
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500363#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass92882652021-08-07 07:24:04 -0600364#if CONFIG_IS_ENABLED(OF_REAL)
Adam Ford49b77762018-08-20 20:27:48 -0500365 .of_match = omap_gpio_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700366 .of_to_plat = of_match_ptr(omap_gpio_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700367 .plat_auto = sizeof(struct omap_gpio_plat),
Adam Ford49b77762018-08-20 20:27:48 -0500368#endif
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500369#else
370 .bind = omap_gpio_bind,
371#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600372 .ops = &gpio_omap_ops,
373 .probe = omap_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700374 .priv_auto = sizeof(struct gpio_bank),
Bin Mengb508ee52018-10-24 06:36:30 -0700375#if !CONFIG_IS_ENABLED(OF_CONTROL)
Faiz Abbasc16d7162018-03-15 21:11:34 +0530376 .flags = DM_FLAG_PRE_RELOC,
Bin Mengb508ee52018-10-24 06:36:30 -0700377#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600378};
379
Simon Glassfa4689a2019-12-06 21:41:35 -0700380#endif /* !DM_GPIO */