blob: 40e87bd1996a4249008bc649f19a1c6f55e2877b [file] [log] [blame]
Simon Glass8fa4d5a2015-08-30 16:55:27 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 *
4 * (C) Copyright 2008-2014 Rockchip Electronics
5 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
Simon Glass9c73e742016-01-21 19:44:09 -070011#include <clk.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060012#include <dm.h>
Simon Glass9c73e742016-01-21 19:44:09 -070013#include <syscon.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060014#include <asm/errno.h>
15#include <asm/gpio.h>
16#include <asm/io.h>
Simon Glass9c73e742016-01-21 19:44:09 -070017#include <asm/arch/clock.h>
18#include <dm/pinctrl.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060019#include <dt-bindings/gpio/gpio.h>
Simon Glass9c73e742016-01-21 19:44:09 -070020#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060021
22enum {
23 ROCKCHIP_GPIOS_PER_BANK = 32,
24};
25
26#define OFFSET_TO_BIT(bit) (1UL << (bit))
27
28struct rockchip_gpio_priv {
29 struct rockchip_gpio_regs *regs;
Simon Glass9c73e742016-01-21 19:44:09 -070030 struct udevice *pinctrl;
31 int bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060032 char name[2];
33};
34
35static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
36{
37 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
38 struct rockchip_gpio_regs *regs = priv->regs;
39
40 clrbits_le32(&regs->swport_ddr, OFFSET_TO_BIT(offset));
41
42 return 0;
43}
44
45static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
46 int value)
47{
48 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
49 struct rockchip_gpio_regs *regs = priv->regs;
50 int mask = OFFSET_TO_BIT(offset);
51
52 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
53 setbits_le32(&regs->swport_ddr, mask);
54
55 return 0;
56}
57
58static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
59{
60 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
61 struct rockchip_gpio_regs *regs = priv->regs;
62
Simon Glassc8d72402016-01-21 19:44:08 -070063 return readl(&regs->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060064}
65
66static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
67 int value)
68{
69 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
70 struct rockchip_gpio_regs *regs = priv->regs;
71 int mask = OFFSET_TO_BIT(offset);
72
73 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
74
75 return 0;
76}
77
78static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
79{
Simon Glass9c73e742016-01-21 19:44:09 -070080#ifdef CONFIG_SPL_BUILD
81 return -ENODATA;
82#else
83 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
84 struct rockchip_gpio_regs *regs = priv->regs;
85 bool is_output;
86 int ret;
87
88 ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
89 if (ret)
90 return ret;
91
92 /* If it's not 0, then it is not a GPIO */
93 if (ret)
94 return GPIOF_FUNC;
95 is_output = readl(&regs->swport_ddr) & OFFSET_TO_BIT(offset);
96
97 return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
98#endif
Simon Glass8fa4d5a2015-08-30 16:55:27 -060099}
100
101static int rockchip_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
102 struct fdtdec_phandle_args *args)
103{
104 desc->offset = args->args[0];
105 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
106
107 return 0;
108}
109
110static int rockchip_gpio_probe(struct udevice *dev)
111{
112 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
113 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
114 char *end;
Simon Glass9c73e742016-01-21 19:44:09 -0700115 int ret;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600116
Simon Glass9c73e742016-01-21 19:44:09 -0700117 /* This only supports RK3288 at present */
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600118 priv->regs = (struct rockchip_gpio_regs *)dev_get_addr(dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700119 ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
Simon Glass9c73e742016-01-21 19:44:09 -0700120 if (ret)
121 return ret;
Simon Glass9c73e742016-01-21 19:44:09 -0700122
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600123 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
124 end = strrchr(dev->name, '@');
Simon Glass9c73e742016-01-21 19:44:09 -0700125 priv->bank = trailing_strtoln(dev->name, end);
126 priv->name[0] = 'A' + priv->bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600127 uc_priv->bank_name = priv->name;
128
129 return 0;
130}
131
132static const struct dm_gpio_ops gpio_rockchip_ops = {
133 .direction_input = rockchip_gpio_direction_input,
134 .direction_output = rockchip_gpio_direction_output,
135 .get_value = rockchip_gpio_get_value,
136 .set_value = rockchip_gpio_set_value,
137 .get_function = rockchip_gpio_get_function,
138 .xlate = rockchip_gpio_xlate,
139};
140
141static const struct udevice_id rockchip_gpio_ids[] = {
142 { .compatible = "rockchip,gpio-bank" },
143 { }
144};
145
146U_BOOT_DRIVER(gpio_rockchip) = {
147 .name = "gpio_rockchip",
148 .id = UCLASS_GPIO,
149 .of_match = rockchip_gpio_ids,
150 .ops = &gpio_rockchip_ops,
151 .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
152 .probe = rockchip_gpio_probe,
153};