blob: 0f22acb0e677acfe209677bfe0d1aeed155bd0e9 [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 Glass12faa022017-05-18 20:09:18 -060011#include <dm/of.h>
Simon Glass16e10402015-01-05 20:05:29 -070012#include <dt-bindings/gpio/gpio.h>
Simon Glassb43d0442012-02-15 15:51:13 -080013
Simon Glassb4d70702014-02-26 15:59:25 -070014DECLARE_GLOBAL_DATA_PTR;
15
Simon Glassb43d0442012-02-15 15:51:13 -080016/* Flags for each GPIO */
17#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
18#define GPIOF_HIGH (1 << 1) /* Currently set high */
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020019#define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
Simon Glassb43d0442012-02-15 15:51:13 -080020
21struct gpio_state {
22 const char *label; /* label given by requester */
23 u8 flags; /* flags (GPIOF_...) */
24};
25
Simon Glassb43d0442012-02-15 15:51:13 -080026/* Access routines for GPIO state */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020027static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080028{
Simon Glassde0977b2015-03-05 12:25:20 -070029 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -070030 struct gpio_state *state = dev_get_priv(dev);
31
32 if (offset >= uc_priv->gpio_count) {
Simon Glassb43d0442012-02-15 15:51:13 -080033 static u8 invalid_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070034 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glassb43d0442012-02-15 15:51:13 -080035 return &invalid_flags;
36 }
37
Simon Glassb4d70702014-02-26 15:59:25 -070038 return &state[offset].flags;
Simon Glassb43d0442012-02-15 15:51:13 -080039}
40
Heiko Schocherb74fcb42014-05-22 12:43:05 +020041static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
Simon Glassb43d0442012-02-15 15:51:13 -080042{
Simon Glassb4d70702014-02-26 15:59:25 -070043 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080044}
45
Heiko Schocherb74fcb42014-05-22 12:43:05 +020046static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
Simon Glassb4d70702014-02-26 15:59:25 -070047 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080048{
Simon Glassb4d70702014-02-26 15:59:25 -070049 u8 *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080050
51 if (value)
52 *gpio |= flag;
53 else
54 *gpio &= ~flag;
55
56 return 0;
57}
58
Simon Glassb43d0442012-02-15 15:51:13 -080059/*
60 * Back-channel sandbox-internal-only access to GPIO state
61 */
62
Heiko Schocherb74fcb42014-05-22 12:43:05 +020063int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080064{
Simon Glassb4d70702014-02-26 15:59:25 -070065 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
66 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
67 return get_gpio_flag(dev, offset, GPIOF_HIGH);
Simon Glassb43d0442012-02-15 15:51:13 -080068}
69
Heiko Schocherb74fcb42014-05-22 12:43:05 +020070int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080071{
Simon Glassb4d70702014-02-26 15:59:25 -070072 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
Simon Glassb43d0442012-02-15 15:51:13 -080073}
74
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020075int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
76{
77 return get_gpio_flag(dev, offset, GPIOF_ODR);
78}
79
80int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
81{
82 return set_gpio_flag(dev, offset, GPIOF_ODR, value);
83}
84
Heiko Schocherb74fcb42014-05-22 12:43:05 +020085int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080086{
Simon Glassb4d70702014-02-26 15:59:25 -070087 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080088}
89
Heiko Schocherb74fcb42014-05-22 12:43:05 +020090int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080091{
Simon Glassb4d70702014-02-26 15:59:25 -070092 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080093}
94
95/*
96 * These functions implement the public interface within U-Boot
97 */
98
Simon Glassb4d70702014-02-26 15:59:25 -070099/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200100static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800101{
Simon Glassb4d70702014-02-26 15:59:25 -0700102 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800103
Simon Glassb4d70702014-02-26 15:59:25 -0700104 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800105}
106
Simon Glassb4d70702014-02-26 15:59:25 -0700107/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200108static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700109 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800110{
Simon Glassb4d70702014-02-26 15:59:25 -0700111 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800112
Simon Glassb4d70702014-02-26 15:59:25 -0700113 return sandbox_gpio_set_direction(dev, offset, 1) |
114 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800115}
116
Simon Glassb4d70702014-02-26 15:59:25 -0700117/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200118static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800119{
Simon Glassb4d70702014-02-26 15:59:25 -0700120 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800121
Simon Glassb4d70702014-02-26 15:59:25 -0700122 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800123}
124
Simon Glassb4d70702014-02-26 15:59:25 -0700125/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200126static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800127{
Simon Glassb4d70702014-02-26 15:59:25 -0700128 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800129
Simon Glassb4d70702014-02-26 15:59:25 -0700130 if (!sandbox_gpio_get_direction(dev, offset)) {
131 printf("sandbox_gpio: error: set_value on input gpio %u\n",
132 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800133 return -1;
134 }
135
Simon Glassb4d70702014-02-26 15:59:25 -0700136 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800137}
138
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200139/* read GPIO ODR value of port 'offset' */
140static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
141{
142 debug("%s: offset:%u\n", __func__, offset);
143
144 return sandbox_gpio_get_open_drain(dev, offset);
145}
146
147/* write GPIO ODR value to port 'offset' */
148static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
149{
150 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
151
152 if (!sandbox_gpio_get_direction(dev, offset)) {
153 printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
154 offset);
155 return -1;
156 }
157
158 return sandbox_gpio_set_open_drain(dev, offset, value);
159}
160
Simon Glass32048632014-10-04 11:29:45 -0600161static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
162{
163 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
164 return GPIOF_OUTPUT;
165 return GPIOF_INPUT;
166}
167
Simon Glass16e10402015-01-05 20:05:29 -0700168static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600169 struct ofnode_phandle_args *args)
Simon Glass16e10402015-01-05 20:05:29 -0700170{
171 desc->offset = args->args[0];
172 if (args->args_count < 2)
173 return 0;
174 if (args->args[1] & GPIO_ACTIVE_LOW)
175 desc->flags |= GPIOD_ACTIVE_LOW;
176 if (args->args[1] & 2)
177 desc->flags |= GPIOD_IS_IN;
178 if (args->args[1] & 4)
179 desc->flags |= GPIOD_IS_OUT;
180 if (args->args[1] & 8)
181 desc->flags |= GPIOD_IS_OUT_ACTIVE;
182
183 return 0;
184}
185
Simon Glassb4d70702014-02-26 15:59:25 -0700186static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700187 .direction_input = sb_gpio_direction_input,
188 .direction_output = sb_gpio_direction_output,
189 .get_value = sb_gpio_get_value,
190 .set_value = sb_gpio_set_value,
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200191 .get_open_drain = sb_gpio_get_open_drain,
192 .set_open_drain = sb_gpio_set_open_drain,
Simon Glass32048632014-10-04 11:29:45 -0600193 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700194 .xlate = sb_gpio_xlate,
Simon Glassb4d70702014-02-26 15:59:25 -0700195};
196
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200197static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800198{
Simon Glassde0977b2015-03-05 12:25:20 -0700199 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800200
Simon Glassdd79d6e2017-01-17 16:52:55 -0700201 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb4d70702014-02-26 15:59:25 -0700202 "num-gpios", 0);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700203 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb4d70702014-02-26 15:59:25 -0700204 "gpio-bank-name", NULL);
Simon Glassb43d0442012-02-15 15:51:13 -0800205
Simon Glassb4d70702014-02-26 15:59:25 -0700206 return 0;
207}
Simon Glassb43d0442012-02-15 15:51:13 -0800208
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200209static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700210{
Simon Glassde0977b2015-03-05 12:25:20 -0700211 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700212
Simon Glassdd79d6e2017-01-17 16:52:55 -0700213 if (dev_of_offset(dev) == -1) {
Simon Glassb4d70702014-02-26 15:59:25 -0700214 /* Tell the uclass how many GPIOs we have */
215 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb43d0442012-02-15 15:51:13 -0800216 }
Simon Glassb4d70702014-02-26 15:59:25 -0700217
218 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
219
220 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800221}
Simon Glassb4d70702014-02-26 15:59:25 -0700222
Simon Glassa367f1d2014-10-04 11:29:46 -0600223static int gpio_sandbox_remove(struct udevice *dev)
224{
225 free(dev->priv);
226
227 return 0;
228}
229
Simon Glass767827a2014-06-11 23:29:45 -0600230static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700231 { .compatible = "sandbox,gpio" },
232 { }
233};
234
235U_BOOT_DRIVER(gpio_sandbox) = {
236 .name = "gpio_sandbox",
237 .id = UCLASS_GPIO,
238 .of_match = sandbox_gpio_ids,
239 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
240 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600241 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700242 .ops = &gpio_sandbox_ops,
243};