blob: 50c4f75ddf5db592bebe735c0bb73e75b444e28e [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 */
20#include <common.h>
Simon Glass1a96d7f2014-10-22 21:37:09 -060021#include <dm.h>
Tom Rini7bc2bca2015-07-31 19:55:09 -040022#include <fdtdec.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Joe Hershbergerf8928f12011-11-11 15:55:36 -060024#include <asm/gpio.h>
Tom Rixdd2d1cc2009-05-15 23:48:36 +020025#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070026#include <dm/device-internal.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090027#include <linux/errno.h>
Tom Rini7bc2bca2015-07-31 19:55:09 -040028#include <malloc.h>
29
30DECLARE_GLOBAL_DATA_PTR;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020031
Sanjeev Premiea2cf552011-09-08 10:47:25 -040032#define OMAP_GPIO_DIR_OUT 0
33#define OMAP_GPIO_DIR_IN 1
34
Simon Glassfa4689a2019-12-06 21:41:35 -070035#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass1a96d7f2014-10-22 21:37:09 -060036
Simon Glass1a96d7f2014-10-22 21:37:09 -060037#define GPIO_PER_BANK 32
38
39struct gpio_bank {
Simon Glass1a96d7f2014-10-22 21:37:09 -060040 /* TODO(sjg@chromium.org): Can we use a struct here? */
41 void *base; /* address of registers in physical memory */
Simon Glass1a96d7f2014-10-22 21:37:09 -060042};
43
44#endif
45
Nikita Kiryanove3cb9c12012-11-27 22:40:57 +000046int gpio_is_valid(int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020047{
Axel Lin01a461f2013-06-21 18:54:25 +080048 return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
Tom Rixdd2d1cc2009-05-15 23:48:36 +020049}
50
Aneesh V9a390882011-07-21 09:29:29 -040051static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
52 int is_input)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020053{
54 void *reg = bank->base;
55 u32 l;
56
Tom Rini7bc2bca2015-07-31 19:55:09 -040057 reg += OMAP_GPIO_OE;
58
Tom Rixdd2d1cc2009-05-15 23:48:36 +020059 l = __raw_readl(reg);
60 if (is_input)
61 l |= 1 << gpio;
62 else
63 l &= ~(1 << gpio);
64 __raw_writel(l, reg);
65}
66
Sanjeev Premiea2cf552011-09-08 10:47:25 -040067/**
68 * Get the direction of the GPIO by reading the GPIO_OE register
69 * corresponding to the specified bank.
70 */
71static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020072{
Sanjeev Premiea2cf552011-09-08 10:47:25 -040073 void *reg = bank->base;
74 u32 v;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020075
Tom Rini7bc2bca2015-07-31 19:55:09 -040076 reg += OMAP_GPIO_OE;
Sanjeev Premiea2cf552011-09-08 10:47:25 -040077
78 v = __raw_readl(reg);
79
80 if (v & (1 << gpio))
81 return OMAP_GPIO_DIR_IN;
82 else
83 return OMAP_GPIO_DIR_OUT;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020084}
85
Aneesh V9a390882011-07-21 09:29:29 -040086static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
87 int enable)
Tom Rixdd2d1cc2009-05-15 23:48:36 +020088{
89 void *reg = bank->base;
90 u32 l = 0;
91
Tom Rini7bc2bca2015-07-31 19:55:09 -040092 if (enable)
93 reg += OMAP_GPIO_SETDATAOUT;
94 else
95 reg += OMAP_GPIO_CLEARDATAOUT;
96
97 l = 1 << gpio;
Tom Rixdd2d1cc2009-05-15 23:48:36 +020098 __raw_writel(l, reg);
99}
100
Simon Glassba33a3f2014-10-22 21:37:08 -0600101static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
102{
103 void *reg = bank->base;
104 int input;
105
Tom Rini7bc2bca2015-07-31 19:55:09 -0400106 input = _get_gpio_direction(bank, gpio);
107 switch (input) {
108 case OMAP_GPIO_DIR_IN:
109 reg += OMAP_GPIO_DATAIN;
110 break;
111 case OMAP_GPIO_DIR_OUT:
112 reg += OMAP_GPIO_DATAOUT;
Simon Glassba33a3f2014-10-22 21:37:08 -0600113 break;
114 default:
115 return -1;
116 }
117
118 return (__raw_readl(reg) & (1 << gpio)) != 0;
119}
120
Simon Glassfa4689a2019-12-06 21:41:35 -0700121#if !CONFIG_IS_ENABLED(DM_GPIO)
Tom Rini74d6c9a2020-06-04 16:01:39 -0400122static inline int get_gpio_index(int gpio)
123{
124 return gpio & 0x1f;
125}
Simon Glass1a96d7f2014-10-22 21:37:09 -0600126
Simon Glassba33a3f2014-10-22 21:37:08 -0600127static inline const struct gpio_bank *get_gpio_bank(int gpio)
128{
129 return &omap_gpio_bank[gpio >> 5];
130}
131
132static int check_gpio(int gpio)
133{
134 if (!gpio_is_valid(gpio)) {
135 printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
136 return -1;
137 }
138 return 0;
139}
140
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400141/**
142 * Set value of the specified gpio
143 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600144int gpio_set_value(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200145{
Aneesh V9a390882011-07-21 09:29:29 -0400146 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200147
148 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600149 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200150 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400151 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600152
153 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200154}
155
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400156/**
157 * Get value of the specified gpio
158 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600159int gpio_get_value(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200160{
Aneesh V9a390882011-07-21 09:29:29 -0400161 const struct gpio_bank *bank;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200162
163 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600164 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200165 bank = get_gpio_bank(gpio);
Simon Glassba33a3f2014-10-22 21:37:08 -0600166
167 return _get_gpio_value(bank, get_gpio_index(gpio));
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200168}
169
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400170/**
171 * Set gpio direction as input
172 */
173int gpio_direction_input(unsigned gpio)
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500174{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400175 const struct gpio_bank *bank;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500176
177 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600178 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400179
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500180 bank = get_gpio_bank(gpio);
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400181 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
182
183 return 0;
Joel A Fernandes46b9c202011-09-04 11:10:03 -0500184}
185
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400186/**
187 * Set gpio direction as output
188 */
189int gpio_direction_output(unsigned gpio, int value)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200190{
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400191 const struct gpio_bank *bank;
192
193 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600194 return -1;
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400195
196 bank = get_gpio_bank(gpio);
197 _set_gpio_dataout(bank, get_gpio_index(gpio), value);
198 _set_gpio_direction(bank, get_gpio_index(gpio), 0);
199
200 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200201}
202
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400203/**
204 * Request a gpio before using it.
205 *
206 * NOTE: Argument 'label' is unused.
207 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600208int gpio_request(unsigned gpio, const char *label)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200209{
210 if (check_gpio(gpio) < 0)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600211 return -1;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200212
213 return 0;
214}
215
Sanjeev Premiea2cf552011-09-08 10:47:25 -0400216/**
217 * Reset and free the gpio after using it.
218 */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600219int gpio_free(unsigned gpio)
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200220{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600221 return 0;
222}
223
224#else /* new driver model interface CONFIG_DM_GPIO */
225
Simon Glass1a96d7f2014-10-22 21:37:09 -0600226/* set GPIO pin 'gpio' as an input */
227static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
228{
229 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600230
231 /* Configure GPIO direction as input. */
232 _set_gpio_direction(bank, offset, 1);
233
234 return 0;
235}
236
237/* set GPIO pin 'gpio' as an output, with polarity 'value' */
238static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
239 int value)
240{
241 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600242
243 _set_gpio_dataout(bank, offset, value);
244 _set_gpio_direction(bank, offset, 0);
245
246 return 0;
247}
248
249/* read GPIO IN value of pin 'gpio' */
250static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
251{
252 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600253
254 return _get_gpio_value(bank, offset);
255}
256
257/* write GPIO OUT value to pin 'gpio' */
258static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
259 int value)
260{
261 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600262
263 _set_gpio_dataout(bank, offset, value);
264
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600265 return 0;
Tom Rixdd2d1cc2009-05-15 23:48:36 +0200266}
Simon Glass1a96d7f2014-10-22 21:37:09 -0600267
Simon Glass1a96d7f2014-10-22 21:37:09 -0600268static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
269{
270 struct gpio_bank *bank = dev_get_priv(dev);
271
Simon Glass1a96d7f2014-10-22 21:37:09 -0600272 /* GPIOF_FUNC is not implemented yet */
Axel Linbb4ab582015-01-31 22:23:38 +0800273 if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
Simon Glass1a96d7f2014-10-22 21:37:09 -0600274 return GPIOF_OUTPUT;
275 else
276 return GPIOF_INPUT;
277}
278
279static const struct dm_gpio_ops gpio_omap_ops = {
Simon Glass1a96d7f2014-10-22 21:37:09 -0600280 .direction_input = omap_gpio_direction_input,
281 .direction_output = omap_gpio_direction_output,
282 .get_value = omap_gpio_get_value,
283 .set_value = omap_gpio_set_value,
284 .get_function = omap_gpio_get_function,
Simon Glass1a96d7f2014-10-22 21:37:09 -0600285};
286
287static int omap_gpio_probe(struct udevice *dev)
288{
289 struct gpio_bank *bank = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700290 struct omap_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700291 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Adam Ford22f2e932018-06-11 20:05:38 -0500292 char name[18], *str;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600293
Adam Forde387e452018-08-17 14:37:58 -0500294 sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
Adam Ford22f2e932018-06-11 20:05:38 -0500295 str = strdup(name);
296 if (!str)
297 return -ENOMEM;
298 uc_priv->bank_name = str;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600299 uc_priv->gpio_count = GPIO_PER_BANK;
300 bank->base = (void *)plat->base;
Tom Rini7bc2bca2015-07-31 19:55:09 -0400301 return 0;
302}
303
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500304#if !CONFIG_IS_ENABLED(OF_CONTROL)
Tom Rini7bc2bca2015-07-31 19:55:09 -0400305static int omap_gpio_bind(struct udevice *dev)
306{
Simon Glassb75b15b2020-12-03 16:55:23 -0700307 struct omap_gpio_plat *plat = dev_get_plat(dev);
Tom Rini7bc2bca2015-07-31 19:55:09 -0400308 fdt_addr_t base_addr;
309
310 if (plat)
311 return 0;
312
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900313 base_addr = dev_read_addr(dev);
Tom Rini7bc2bca2015-07-31 19:55:09 -0400314 if (base_addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600315 return -EINVAL;
Tom Rini7bc2bca2015-07-31 19:55:09 -0400316
317 /*
318 * TODO:
319 * When every board is converted to driver model and DT is
320 * supported, this can be done by auto-alloc feature, but
Simon Glass71fa5b42020-12-03 16:55:18 -0700321 * not using calloc to alloc memory for plat.
Simon Glass87f4cb72017-09-17 16:54:52 -0600322 *
323 * For example am33xx_gpio uses platform data rather than device tree.
324 *
325 * NOTE: DO NOT COPY this code if you are using device tree.
Tom Rini7bc2bca2015-07-31 19:55:09 -0400326 */
327 plat = calloc(1, sizeof(*plat));
328 if (!plat)
329 return -ENOMEM;
330
331 plat->base = base_addr;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700332 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
Simon Glass95588622020-12-22 19:30:28 -0700333 dev_set_plat(dev, plat);
Simon Glass1a96d7f2014-10-22 21:37:09 -0600334
335 return 0;
336}
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500337#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600338
Simon Glass3580f6d2021-08-07 07:24:03 -0600339#if CONFIG_IS_ENABLED(OF_REAL)
Tom Rini7bc2bca2015-07-31 19:55:09 -0400340static const struct udevice_id omap_gpio_ids[] = {
341 { .compatible = "ti,omap3-gpio" },
342 { .compatible = "ti,omap4-gpio" },
343 { .compatible = "ti,am4372-gpio" },
344 { }
345};
346
Simon Glassaad29ae2020-12-03 16:55:21 -0700347static int omap_gpio_of_to_plat(struct udevice *dev)
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500348{
Simon Glassb75b15b2020-12-03 16:55:23 -0700349 struct omap_gpio_plat *plat = dev_get_plat(dev);
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500350 fdt_addr_t addr;
351
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900352 addr = dev_read_addr(dev);
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500353 if (addr == FDT_ADDR_T_NONE)
354 return -EINVAL;
355
356 plat->base = addr;
357 return 0;
358}
359#endif
360
Simon Glass1a96d7f2014-10-22 21:37:09 -0600361U_BOOT_DRIVER(gpio_omap) = {
362 .name = "gpio_omap",
363 .id = UCLASS_GPIO,
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500364#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass92882652021-08-07 07:24:04 -0600365#if CONFIG_IS_ENABLED(OF_REAL)
Adam Ford49b77762018-08-20 20:27:48 -0500366 .of_match = omap_gpio_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700367 .of_to_plat = of_match_ptr(omap_gpio_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700368 .plat_auto = sizeof(struct omap_gpio_plat),
Adam Ford49b77762018-08-20 20:27:48 -0500369#endif
Adam Ford1ac4c1b2018-06-10 09:29:51 -0500370#else
371 .bind = omap_gpio_bind,
372#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600373 .ops = &gpio_omap_ops,
374 .probe = omap_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700375 .priv_auto = sizeof(struct gpio_bank),
Bin Mengb508ee52018-10-24 06:36:30 -0700376#if !CONFIG_IS_ENABLED(OF_CONTROL)
Faiz Abbasc16d7162018-03-15 21:11:34 +0530377 .flags = DM_FLAG_PRE_RELOC,
Bin Mengb508ee52018-10-24 06:36:30 -0700378#endif
Simon Glass1a96d7f2014-10-22 21:37:09 -0600379};
380
Simon Glassfa4689a2019-12-06 21:41:35 -0700381#endif /* !DM_GPIO */