blob: d564c252c7d7336d9f5bcc93d9ac306bd42eb539 [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>
Simon Glass16e10402015-01-05 20:05:29 -070011#include <dt-bindings/gpio/gpio.h>
Simon Glassb43d0442012-02-15 15:51:13 -080012
Simon Glassb4d70702014-02-26 15:59:25 -070013DECLARE_GLOBAL_DATA_PTR;
14
Simon Glassb43d0442012-02-15 15:51:13 -080015/* Flags for each GPIO */
16#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
17#define GPIOF_HIGH (1 << 1) /* Currently set high */
Simon Glassb43d0442012-02-15 15:51:13 -080018
19struct gpio_state {
20 const char *label; /* label given by requester */
21 u8 flags; /* flags (GPIOF_...) */
22};
23
Simon Glassb43d0442012-02-15 15:51:13 -080024/* Access routines for GPIO state */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020025static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080026{
Simon Glassb4d70702014-02-26 15:59:25 -070027 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
28 struct gpio_state *state = dev_get_priv(dev);
29
30 if (offset >= uc_priv->gpio_count) {
Simon Glassb43d0442012-02-15 15:51:13 -080031 static u8 invalid_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070032 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -080033 return &invalid_flags;
34 }
35
Simon Glassb4d70702014-02-26 15:59:25 -070036 return &state[offset].flags;
Simon Glassb43d0442012-02-15 15:51:13 -080037}
38
Heiko Schocherb74fcb42014-05-22 12:43:05 +020039static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
Simon Glassb43d0442012-02-15 15:51:13 -080040{
Simon Glassb4d70702014-02-26 15:59:25 -070041 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080042}
43
Heiko Schocherb74fcb42014-05-22 12:43:05 +020044static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
Simon Glassb4d70702014-02-26 15:59:25 -070045 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080046{
Simon Glassb4d70702014-02-26 15:59:25 -070047 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080048
49 if (value)
50 *gpio |= flag;
51 else
52 *gpio &= ~flag;
53
54 return 0;
55}
56
Simon Glassb43d0442012-02-15 15:51:13 -080057/*
58 * Back-channel sandbox-internal-only access to GPIO state
59 */
60
Heiko Schocherb74fcb42014-05-22 12:43:05 +020061int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080062{
Simon Glassb4d70702014-02-26 15:59:25 -070063 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
64 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
65 return get_gpio_flag(dev, offset, GPIOF_HIGH);
Simon Glassb43d0442012-02-15 15:51:13 -080066}
67
Heiko Schocherb74fcb42014-05-22 12:43:05 +020068int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080069{
Simon Glassb4d70702014-02-26 15:59:25 -070070 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glassb43d0442012-02-15 15:51:13 -080071}
72
Heiko Schocherb74fcb42014-05-22 12:43:05 +020073int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080074{
Simon Glassb4d70702014-02-26 15:59:25 -070075 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080076}
77
Heiko Schocherb74fcb42014-05-22 12:43:05 +020078int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080079{
Simon Glassb4d70702014-02-26 15:59:25 -070080 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080081}
82
83/*
84 * These functions implement the public interface within U-Boot
85 */
86
Simon Glassb4d70702014-02-26 15:59:25 -070087/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020088static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080089{
Simon Glassb4d70702014-02-26 15:59:25 -070090 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080091
Simon Glassb4d70702014-02-26 15:59:25 -070092 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -080093}
94
Simon Glassb4d70702014-02-26 15:59:25 -070095/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020096static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -070097 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080098{
Simon Glassb4d70702014-02-26 15:59:25 -070099 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800100
Simon Glassb4d70702014-02-26 15:59:25 -0700101 return sandbox_gpio_set_direction(dev, offset, 1) |
102 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800103}
104
Simon Glassb4d70702014-02-26 15:59:25 -0700105/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200106static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800107{
Simon Glassb4d70702014-02-26 15:59:25 -0700108 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800109
Simon Glassb4d70702014-02-26 15:59:25 -0700110 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800111}
112
Simon Glassb4d70702014-02-26 15:59:25 -0700113/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200114static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800115{
Simon Glassb4d70702014-02-26 15:59:25 -0700116 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800117
Simon Glassb4d70702014-02-26 15:59:25 -0700118 if (!sandbox_gpio_get_direction(dev, offset)) {
119 printf("sandbox_gpio: error: set_value on input gpio %u\n",
120 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800121 return -1;
122 }
123
Simon Glassb4d70702014-02-26 15:59:25 -0700124 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800125}
126
Simon Glass32048632014-10-04 11:29:45 -0600127static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
128{
129 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
130 return GPIOF_OUTPUT;
131 return GPIOF_INPUT;
132}
133
Simon Glass16e10402015-01-05 20:05:29 -0700134static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
135 struct fdtdec_phandle_args *args)
136{
137 desc->offset = args->args[0];
138 if (args->args_count < 2)
139 return 0;
140 if (args->args[1] & GPIO_ACTIVE_LOW)
141 desc->flags |= GPIOD_ACTIVE_LOW;
142 if (args->args[1] & 2)
143 desc->flags |= GPIOD_IS_IN;
144 if (args->args[1] & 4)
145 desc->flags |= GPIOD_IS_OUT;
146 if (args->args[1] & 8)
147 desc->flags |= GPIOD_IS_OUT_ACTIVE;
148
149 return 0;
150}
151
Simon Glassb4d70702014-02-26 15:59:25 -0700152static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700153 .direction_input = sb_gpio_direction_input,
154 .direction_output = sb_gpio_direction_output,
155 .get_value = sb_gpio_get_value,
156 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600157 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700158 .xlate = sb_gpio_xlate,
Simon Glassb4d70702014-02-26 15:59:25 -0700159};
160
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200161static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800162{
Simon Glassb4d70702014-02-26 15:59:25 -0700163 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
Simon Glassb43d0442012-02-15 15:51:13 -0800164
Simon Glassb4d70702014-02-26 15:59:25 -0700165 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
166 "num-gpios", 0);
167 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
168 "gpio-bank-name", NULL);
Simon Glassb43d0442012-02-15 15:51:13 -0800169
Simon Glassb4d70702014-02-26 15:59:25 -0700170 return 0;
171}
Simon Glassb43d0442012-02-15 15:51:13 -0800172
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200173static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700174{
175 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
176
177 if (dev->of_offset == -1) {
178 /* Tell the uclass how many GPIOs we have */
179 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb43d0442012-02-15 15:51:13 -0800180 }
Simon Glassb4d70702014-02-26 15:59:25 -0700181
182 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
183
184 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800185}
Simon Glassb4d70702014-02-26 15:59:25 -0700186
Simon Glassa367f1d2014-10-04 11:29:46 -0600187static int gpio_sandbox_remove(struct udevice *dev)
188{
189 free(dev->priv);
190
191 return 0;
192}
193
Simon Glass767827a2014-06-11 23:29:45 -0600194static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700195 { .compatible = "sandbox,gpio" },
196 { }
197};
198
199U_BOOT_DRIVER(gpio_sandbox) = {
200 .name = "gpio_sandbox",
201 .id = UCLASS_GPIO,
202 .of_match = sandbox_gpio_ids,
203 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
204 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600205 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700206 .ops = &gpio_sandbox_ops,
207};