blob: 53c80d5be65dd85c0dbe20d2e1823e0400a4937f [file] [log] [blame]
Simon Glassb43d0442012-02-15 15:51:13 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glassb43d0442012-02-15 15:51:13 -08004 */
5
6#include <common.h>
Simon Glassb4d70702014-02-26 15:59:25 -07007#include <dm.h>
8#include <fdtdec.h>
9#include <malloc.h>
Simon Glassb43d0442012-02-15 15:51:13 -080010#include <asm/gpio.h>
11
Simon Glassb4d70702014-02-26 15:59:25 -070012DECLARE_GLOBAL_DATA_PTR;
13
Simon Glassb43d0442012-02-15 15:51:13 -080014/* Flags for each GPIO */
15#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
16#define GPIOF_HIGH (1 << 1) /* Currently set high */
Simon Glassb43d0442012-02-15 15:51:13 -080017
18struct gpio_state {
19 const char *label; /* label given by requester */
20 u8 flags; /* flags (GPIOF_...) */
21};
22
Simon Glassb43d0442012-02-15 15:51:13 -080023/* Access routines for GPIO state */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020024static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080025{
Simon Glassb4d70702014-02-26 15:59:25 -070026 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
27 struct gpio_state *state = dev_get_priv(dev);
28
29 if (offset >= uc_priv->gpio_count) {
Simon Glassb43d0442012-02-15 15:51:13 -080030 static u8 invalid_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070031 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -080032 return &invalid_flags;
33 }
34
Simon Glassb4d70702014-02-26 15:59:25 -070035 return &state[offset].flags;
Simon Glassb43d0442012-02-15 15:51:13 -080036}
37
Heiko Schocherb74fcb42014-05-22 12:43:05 +020038static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
Simon Glassb43d0442012-02-15 15:51:13 -080039{
Simon Glassb4d70702014-02-26 15:59:25 -070040 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080041}
42
Heiko Schocherb74fcb42014-05-22 12:43:05 +020043static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
Simon Glassb4d70702014-02-26 15:59:25 -070044 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080045{
Simon Glassb4d70702014-02-26 15:59:25 -070046 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080047
48 if (value)
49 *gpio |= flag;
50 else
51 *gpio &= ~flag;
52
53 return 0;
54}
55
Simon Glassb43d0442012-02-15 15:51:13 -080056/*
57 * Back-channel sandbox-internal-only access to GPIO state
58 */
59
Heiko Schocherb74fcb42014-05-22 12:43:05 +020060int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080061{
Simon Glassb4d70702014-02-26 15:59:25 -070062 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
63 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
64 return get_gpio_flag(dev, offset, GPIOF_HIGH);
Simon Glassb43d0442012-02-15 15:51:13 -080065}
66
Heiko Schocherb74fcb42014-05-22 12:43:05 +020067int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080068{
Simon Glassb4d70702014-02-26 15:59:25 -070069 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glassb43d0442012-02-15 15:51:13 -080070}
71
Heiko Schocherb74fcb42014-05-22 12:43:05 +020072int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080073{
Simon Glassb4d70702014-02-26 15:59:25 -070074 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080075}
76
Heiko Schocherb74fcb42014-05-22 12:43:05 +020077int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080078{
Simon Glassb4d70702014-02-26 15:59:25 -070079 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080080}
81
82/*
83 * These functions implement the public interface within U-Boot
84 */
85
Simon Glassb4d70702014-02-26 15:59:25 -070086/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020087static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080088{
Simon Glassb4d70702014-02-26 15:59:25 -070089 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080090
Simon Glassb4d70702014-02-26 15:59:25 -070091 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -080092}
93
Simon Glassb4d70702014-02-26 15:59:25 -070094/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020095static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -070096 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080097{
Simon Glassb4d70702014-02-26 15:59:25 -070098 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -080099
Simon Glassb4d70702014-02-26 15:59:25 -0700100 return sandbox_gpio_set_direction(dev, offset, 1) |
101 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800102}
103
Simon Glassb4d70702014-02-26 15:59:25 -0700104/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200105static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800106{
Simon Glassb4d70702014-02-26 15:59:25 -0700107 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800108
Simon Glassb4d70702014-02-26 15:59:25 -0700109 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800110}
111
Simon Glassb4d70702014-02-26 15:59:25 -0700112/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200113static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800114{
Simon Glassb4d70702014-02-26 15:59:25 -0700115 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800116
Simon Glassb4d70702014-02-26 15:59:25 -0700117 if (!sandbox_gpio_get_direction(dev, offset)) {
118 printf("sandbox_gpio: error: set_value on input gpio %u\n",
119 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800120 return -1;
121 }
122
Simon Glassb4d70702014-02-26 15:59:25 -0700123 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800124}
125
Simon Glass32048632014-10-04 11:29:45 -0600126static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
127{
128 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
129 return GPIOF_OUTPUT;
130 return GPIOF_INPUT;
131}
132
Simon Glassb4d70702014-02-26 15:59:25 -0700133static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700134 .direction_input = sb_gpio_direction_input,
135 .direction_output = sb_gpio_direction_output,
136 .get_value = sb_gpio_get_value,
137 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600138 .get_function = sb_gpio_get_function,
Simon Glassb4d70702014-02-26 15:59:25 -0700139};
140
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200141static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800142{
Simon Glassb4d70702014-02-26 15:59:25 -0700143 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
Simon Glassb43d0442012-02-15 15:51:13 -0800144
Simon Glassb4d70702014-02-26 15:59:25 -0700145 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
146 "num-gpios", 0);
147 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
148 "gpio-bank-name", NULL);
Simon Glassb43d0442012-02-15 15:51:13 -0800149
Simon Glassb4d70702014-02-26 15:59:25 -0700150 return 0;
151}
Simon Glassb43d0442012-02-15 15:51:13 -0800152
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200153static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700154{
155 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
156
157 if (dev->of_offset == -1) {
158 /* Tell the uclass how many GPIOs we have */
159 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb43d0442012-02-15 15:51:13 -0800160 }
Simon Glassb4d70702014-02-26 15:59:25 -0700161
162 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
163
164 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800165}
Simon Glassb4d70702014-02-26 15:59:25 -0700166
Simon Glassa367f1d2014-10-04 11:29:46 -0600167static int gpio_sandbox_remove(struct udevice *dev)
168{
169 free(dev->priv);
170
171 return 0;
172}
173
Simon Glass767827a2014-06-11 23:29:45 -0600174static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700175 { .compatible = "sandbox,gpio" },
176 { }
177};
178
179U_BOOT_DRIVER(gpio_sandbox) = {
180 .name = "gpio_sandbox",
181 .id = UCLASS_GPIO,
182 .of_match = sandbox_gpio_ids,
183 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
184 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600185 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700186 .ops = &gpio_sandbox_ops,
187};