blob: f3a7ccb51e98f28ee1b9dd72906dac4b93e0c407 [file] [log] [blame]
Tom Rixdd2d1cc2009-05-15 23:48:36 +02001/*
2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
4 *
Wolfgang Denk5064ca42013-07-28 22:12:44 +02005 * SPDX-License-Identifier: GPL-2.0
Tom Rixdd2d1cc2009-05-15 23:48:36 +02006 *
7 * This work is derived from the linux 2.6.27 kernel source
8 * To fetch, use the kernel repository
9 * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
10 * Use the v2.6.27 tag.
11 *
12 * Below is the original's header including its copyright
13 *
14 * linux/arch/arm/plat-omap/gpio.c
15 *
16 * Support functions for OMAP GPIO
17 *
18 * Copyright (C) 2003-2005 Nokia Corporation
19 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tom Rixdd2d1cc2009-05-15 23:48:36 +020020 */
21#include <common.h>
Simon Glass1a96d7f2014-10-22 21:37:09 -060022#include <dm.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>
25#include <asm/errno.h>
26
Sanjeev Premiea2cf552011-09-08 10:47:25 -040027#define OMAP_GPIO_DIR_OUT 0
28#define OMAP_GPIO_DIR_IN 1
29
Simon Glass1a96d7f2014-10-22 21:37:09 -060030#ifdef CONFIG_DM_GPIO
31
Simon Glass1a96d7f2014-10-22 21:37:09 -060032#define GPIO_PER_BANK 32
33
34struct gpio_bank {
Simon Glass1a96d7f2014-10-22 21:37:09 -060035 /* TODO(sjg@chromium.org): Can we use a struct here? */
36 void *base; /* address of registers in physical memory */
37 enum gpio_method method;
38};
39
40#endif
41
Tom Rixdd2d1cc2009-05-15 23:48:36 +020042static inline int get_gpio_index(int gpio)
43{
44 return gpio & 0x1f;
45}
46
Nikita Kiryanove3cb9c12012-11-27 22:40:57 +000047int gpio_is_valid(int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020048{
Axel Lin01a461f2013-06-21 18:54:25 +080049 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
Tom Rixdd2d1cc2009-05-15 23:48:36 +020050}
51
Aneesh V9a390882011-07-21 09:29:29 -040052static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
53 int is_input)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020054{
55 void *reg = bank->base;
56 u32 l;
57
58 switch (bank->method) {
59 case METHOD_GPIO_24XX:
Aneesh V9a390882011-07-21 09:29:29 -040060 reg += OMAP_GPIO_OE;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020061 break;
62 default:
63 return;
64 }
65 l = __raw_readl(reg);
66 if (is_input)
67 l |= 1 << gpio;
68 else
69 l &= ~(1 << gpio);
70 __raw_writel(l, reg);
71}
72
Sanjeev Premiea2cf552011-09-08 10:47:25 -040073/**
74 * Get the direction of the GPIO by reading the GPIO_OE register
75 * corresponding to the specified bank.
76 */
77static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020078{
Sanjeev Premiea2cf552011-09-08 10:47:25 -040079 void *reg = bank->base;
80 u32 v;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020081
Sanjeev Premiea2cf552011-09-08 10:47:25 -040082 switch (bank->method) {
83 case METHOD_GPIO_24XX:
84 reg += OMAP_GPIO_OE;
85 break;
86 default:
Joe Hershbergerf8928f12011-11-11 15:55:36 -060087 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -040088 }
89
90 v = __raw_readl(reg);
91
92 if (v & (1 << gpio))
93 return OMAP_GPIO_DIR_IN;
94 else
95 return OMAP_GPIO_DIR_OUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020096}
97
Aneesh V9a390882011-07-21 09:29:29 -040098static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
99 int enable)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200100{
101 void *reg = bank->base;
102 u32 l = 0;
103
104 switch (bank->method) {
105 case METHOD_GPIO_24XX:
106 if (enable)
Aneesh V9a390882011-07-21 09:29:29 -0400107 reg += OMAP_GPIO_SETDATAOUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200108 else
Aneesh V9a390882011-07-21 09:29:29 -0400109 reg += OMAP_GPIO_CLEARDATAOUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200110 l = 1 << gpio;
111 break;
112 default:
113 printf("omap3-gpio unknown bank method %s %d\n",
114 __FILE__, __LINE__);
115 return;
116 }
117 __raw_writel(l, reg);
118}
119
Simon Glassba33a3f2014-10-22 21:37:08 -0600120static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
121{
122 void *reg = bank->base;
123 int input;
124
125 switch (bank->method) {
126 case METHOD_GPIO_24XX:
127 input = _get_gpio_direction(bank, gpio);
128 switch (input) {
129 case OMAP_GPIO_DIR_IN:
130 reg += OMAP_GPIO_DATAIN;
131 break;
132 case OMAP_GPIO_DIR_OUT:
133 reg += OMAP_GPIO_DATAOUT;
134 break;
135 default:
136 return -1;
137 }
138 break;
139 default:
140 return -1;
141 }
142
143 return (__raw_readl(reg) & (1 << gpio)) != 0;
144}
145
Simon Glass1a96d7f2014-10-22 21:37:09 -0600146#ifndef CONFIG_DM_GPIO
147
Simon Glassba33a3f2014-10-22 21:37:08 -0600148static inline const struct gpio_bank *get_gpio_bank(int gpio)
149{
150 return &omap_gpio_bank[gpio >> 5];
151}
152
153static int check_gpio(int gpio)
154{
155 if (!gpio_is_valid(gpio)) {
156 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
157 return -1;
158 }
159 return 0;
160}
161
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400162/**
163 * Set value of the specified gpio
164 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600165int gpio_set_value(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200166{
Aneesh V9a390882011-07-21 09:29:29 -0400167 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200168
169 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600170 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200171 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400172 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600173
174 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200175}
176
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400177/**
178 * Get value of the specified gpio
179 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600180int gpio_get_value(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200181{
Aneesh V9a390882011-07-21 09:29:29 -0400182 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200183
184 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600185 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200186 bank = get_gpio_bank(gpio);
Simon Glassba33a3f2014-10-22 21:37:08 -0600187
188 return _get_gpio_value(bank, get_gpio_index(gpio));
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200189}
190
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400191/**
192 * Set gpio direction as input
193 */
194int gpio_direction_input(unsigned gpio)
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500195{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400196 const struct gpio_bank *bank;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500197
198 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600199 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400200
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500201 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400202 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
203
204 return 0;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500205}
206
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400207/**
208 * Set gpio direction as output
209 */
210int gpio_direction_output(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200211{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400212 const struct gpio_bank *bank;
213
214 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600215 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400216
217 bank = get_gpio_bank(gpio);
218 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
219 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
220
221 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200222}
223
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400224/**
225 * Request a gpio before using it.
226 *
227 * NOTE: Argument 'label' is unused.
228 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600229int gpio_request(unsigned gpio, const char *label)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200230{
231 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600232 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200233
234 return 0;
235}
236
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400237/**
238 * Reset and free the gpio after using it.
239 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600240int gpio_free(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200241{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600242 return 0;
243}
244
245#else /* new driver model interface CONFIG_DM_GPIO */
246
Simon Glass1a96d7f2014-10-22 21:37:09 -0600247/* set GPIO pin 'gpio' as an input */
248static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
249{
250 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600251
252 /* Configure GPIO direction as input. */
253 _set_gpio_direction(bank, offset, 1);
254
255 return 0;
256}
257
258/* set GPIO pin 'gpio' as an output, with polarity 'value' */
259static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
260 int value)
261{
262 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600263
264 _set_gpio_dataout(bank, offset, value);
265 _set_gpio_direction(bank, offset, 0);
266
267 return 0;
268}
269
270/* read GPIO IN value of pin 'gpio' */
271static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
272{
273 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600274
275 return _get_gpio_value(bank, offset);
276}
277
278/* write GPIO OUT value to pin 'gpio' */
279static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
280 int value)
281{
282 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600283
284 _set_gpio_dataout(bank, offset, value);
285
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600286 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200287}
Simon Glass1a96d7f2014-10-22 21:37:09 -0600288
Simon Glass1a96d7f2014-10-22 21:37:09 -0600289static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
290{
291 struct gpio_bank *bank = dev_get_priv(dev);
292
Simon Glass1a96d7f2014-10-22 21:37:09 -0600293 /* GPIOF_FUNC is not implemented yet */
Simon Glass46715a82014-10-04 11:29:53 -0600294 if (_get_gpio_direction(bank->base, offset) == OMAP_GPIO_DIR_OUT)
Simon Glass1a96d7f2014-10-22 21:37:09 -0600295 return GPIOF_OUTPUT;
296 else
297 return GPIOF_INPUT;
298}
299
300static const struct dm_gpio_ops gpio_omap_ops = {
Simon Glass1a96d7f2014-10-22 21:37:09 -0600301 .direction_input = omap_gpio_direction_input,
302 .direction_output = omap_gpio_direction_output,
303 .get_value = omap_gpio_get_value,
304 .set_value = omap_gpio_set_value,
305 .get_function = omap_gpio_get_function,
Simon Glass1a96d7f2014-10-22 21:37:09 -0600306};
307
308static int omap_gpio_probe(struct udevice *dev)
309{
310 struct gpio_bank *bank = dev_get_priv(dev);
311 struct omap_gpio_platdata *plat = dev_get_platdata(dev);
312 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
313 char name[18], *str;
314
315 sprintf(name, "GPIO%d_", plat->bank_index);
316 str = strdup(name);
317 if (!str)
318 return -ENOMEM;
319 uc_priv->bank_name = str;
320 uc_priv->gpio_count = GPIO_PER_BANK;
321 bank->base = (void *)plat->base;
322 bank->method = plat->method;
323
324 return 0;
325}
326
327U_BOOT_DRIVER(gpio_omap) = {
328 .name = "gpio_omap",
329 .id = UCLASS_GPIO,
330 .ops = &gpio_omap_ops,
331 .probe = omap_gpio_probe,
332 .priv_auto_alloc_size = sizeof(struct gpio_bank),
333};
334
335#endif /* CONFIG_DM_GPIO */