blob: 2ef5c67ad593c153b27b6a1d393b208bbb2331f0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb43d0442012-02-15 15:51:13 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
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
14/* Flags for each GPIO */
15#define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
16#define GPIOF_HIGH (1 << 1) /* Currently set high */
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020017#define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
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 Glassde0977b2015-03-05 12:25:20 -070027 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -070028 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
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020073int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
74{
75 return get_gpio_flag(dev, offset, GPIOF_ODR);
76}
77
78int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
79{
80 return set_gpio_flag(dev, offset, GPIOF_ODR, value);
81}
82
Heiko Schocherb74fcb42014-05-22 12:43:05 +020083int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080084{
Simon Glassb4d70702014-02-26 15:59:25 -070085 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
Simon Glassb43d0442012-02-15 15:51:13 -080086}
87
Heiko Schocherb74fcb42014-05-22 12:43:05 +020088int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080089{
Simon Glassb4d70702014-02-26 15:59:25 -070090 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
Simon Glassb43d0442012-02-15 15:51:13 -080091}
92
93/*
94 * These functions implement the public interface within U-Boot
95 */
96
Simon Glassb4d70702014-02-26 15:59:25 -070097/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020098static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080099{
Simon Glassb4d70702014-02-26 15:59:25 -0700100 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800101
Simon Glassb4d70702014-02-26 15:59:25 -0700102 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800103}
104
Simon Glassb4d70702014-02-26 15:59:25 -0700105/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200106static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700107 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800108{
Simon Glassb4d70702014-02-26 15:59:25 -0700109 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800110
Simon Glassb4d70702014-02-26 15:59:25 -0700111 return sandbox_gpio_set_direction(dev, offset, 1) |
112 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800113}
114
Simon Glassb4d70702014-02-26 15:59:25 -0700115/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200116static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800117{
Simon Glassb4d70702014-02-26 15:59:25 -0700118 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800119
Simon Glassb4d70702014-02-26 15:59:25 -0700120 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800121}
122
Simon Glassb4d70702014-02-26 15:59:25 -0700123/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200124static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800125{
Simon Glassb4d70702014-02-26 15:59:25 -0700126 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800127
Simon Glassb4d70702014-02-26 15:59:25 -0700128 if (!sandbox_gpio_get_direction(dev, offset)) {
129 printf("sandbox_gpio: error: set_value on input gpio %u\n",
130 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800131 return -1;
132 }
133
Simon Glassb4d70702014-02-26 15:59:25 -0700134 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800135}
136
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200137/* read GPIO ODR value of port 'offset' */
138static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
139{
140 debug("%s: offset:%u\n", __func__, offset);
141
142 return sandbox_gpio_get_open_drain(dev, offset);
143}
144
145/* write GPIO ODR value to port 'offset' */
146static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
147{
148 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
149
150 if (!sandbox_gpio_get_direction(dev, offset)) {
151 printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
152 offset);
153 return -1;
154 }
155
156 return sandbox_gpio_set_open_drain(dev, offset, value);
157}
158
Simon Glass32048632014-10-04 11:29:45 -0600159static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
160{
161 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
162 return GPIOF_OUTPUT;
163 return GPIOF_INPUT;
164}
165
Simon Glass16e10402015-01-05 20:05:29 -0700166static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600167 struct ofnode_phandle_args *args)
Simon Glass16e10402015-01-05 20:05:29 -0700168{
169 desc->offset = args->args[0];
170 if (args->args_count < 2)
171 return 0;
172 if (args->args[1] & GPIO_ACTIVE_LOW)
173 desc->flags |= GPIOD_ACTIVE_LOW;
174 if (args->args[1] & 2)
175 desc->flags |= GPIOD_IS_IN;
176 if (args->args[1] & 4)
177 desc->flags |= GPIOD_IS_OUT;
178 if (args->args[1] & 8)
179 desc->flags |= GPIOD_IS_OUT_ACTIVE;
180
181 return 0;
182}
183
Simon Glassb4d70702014-02-26 15:59:25 -0700184static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700185 .direction_input = sb_gpio_direction_input,
186 .direction_output = sb_gpio_direction_output,
187 .get_value = sb_gpio_get_value,
188 .set_value = sb_gpio_set_value,
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +0200189 .get_open_drain = sb_gpio_get_open_drain,
190 .set_open_drain = sb_gpio_set_open_drain,
Simon Glass32048632014-10-04 11:29:45 -0600191 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700192 .xlate = sb_gpio_xlate,
Simon Glassb4d70702014-02-26 15:59:25 -0700193};
194
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200195static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800196{
Simon Glassde0977b2015-03-05 12:25:20 -0700197 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800198
Simon Glass9e7ab232018-02-03 10:36:59 -0700199 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
200 0);
Simon Glass1185fcb2017-05-18 20:09:20 -0600201 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
Simon Glassb43d0442012-02-15 15:51:13 -0800202
Simon Glassb4d70702014-02-26 15:59:25 -0700203 return 0;
204}
Simon Glassb43d0442012-02-15 15:51:13 -0800205
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200206static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700207{
Simon Glassde0977b2015-03-05 12:25:20 -0700208 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700209
Simon Glass1185fcb2017-05-18 20:09:20 -0600210 if (!dev_of_valid(dev))
Simon Glassb4d70702014-02-26 15:59:25 -0700211 /* Tell the uclass how many GPIOs we have */
212 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb4d70702014-02-26 15:59:25 -0700213
214 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
215
216 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800217}
Simon Glassb4d70702014-02-26 15:59:25 -0700218
Simon Glassa367f1d2014-10-04 11:29:46 -0600219static int gpio_sandbox_remove(struct udevice *dev)
220{
221 free(dev->priv);
222
223 return 0;
224}
225
Simon Glass767827a2014-06-11 23:29:45 -0600226static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700227 { .compatible = "sandbox,gpio" },
228 { }
229};
230
231U_BOOT_DRIVER(gpio_sandbox) = {
232 .name = "gpio_sandbox",
233 .id = UCLASS_GPIO,
234 .of_match = sandbox_gpio_ids,
235 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
236 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600237 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700238 .ops = &gpio_sandbox_ops,
239};