blob: c62f0251c4aa2e66060a41d344cb2684911646ee [file] [log] [blame]
Simon Glass1f8f7732015-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 Glass48647822016-01-21 19:44:09 -070011#include <clk.h>
Simon Glass1f8f7732015-08-30 16:55:27 -060012#include <dm.h>
Simon Glass48647822016-01-21 19:44:09 -070013#include <syscon.h>
Simon Glass1f8f7732015-08-30 16:55:27 -060014#include <asm/errno.h>
15#include <asm/gpio.h>
16#include <asm/io.h>
Simon Glass48647822016-01-21 19:44:09 -070017#include <asm/arch/clock.h>
18#include <dm/pinctrl.h>
Simon Glass1f8f7732015-08-30 16:55:27 -060019#include <dt-bindings/gpio/gpio.h>
Simon Glass48647822016-01-21 19:44:09 -070020#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass1f8f7732015-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 Glass48647822016-01-21 19:44:09 -070030 struct udevice *pinctrl;
31 int bank;
Simon Glass1f8f7732015-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 Glass7d0c2c32016-01-21 19:44:08 -070063 return readl(&regs->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
Simon Glass1f8f7732015-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 Glass48647822016-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 Glass1f8f7732015-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 Glass48647822016-01-21 19:44:09 -0700115 int ret;
Simon Glass1f8f7732015-08-30 16:55:27 -0600116
Simon Glass48647822016-01-21 19:44:09 -0700117 /* This only supports RK3288 at present */
Simon Glass1f8f7732015-08-30 16:55:27 -0600118 priv->regs = (struct rockchip_gpio_regs *)dev_get_addr(dev);
Simon Glass48647822016-01-21 19:44:09 -0700119 ret = uclass_first_device(UCLASS_PINCTRL, &priv->pinctrl);
120 if (ret)
121 return ret;
122 if (!priv->pinctrl)
123 return -ENODEV;
124
Simon Glass1f8f7732015-08-30 16:55:27 -0600125 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
126 end = strrchr(dev->name, '@');
Simon Glass48647822016-01-21 19:44:09 -0700127 priv->bank = trailing_strtoln(dev->name, end);
128 priv->name[0] = 'A' + priv->bank;
Simon Glass1f8f7732015-08-30 16:55:27 -0600129 uc_priv->bank_name = priv->name;
130
131 return 0;
132}
133
134static const struct dm_gpio_ops gpio_rockchip_ops = {
135 .direction_input = rockchip_gpio_direction_input,
136 .direction_output = rockchip_gpio_direction_output,
137 .get_value = rockchip_gpio_get_value,
138 .set_value = rockchip_gpio_set_value,
139 .get_function = rockchip_gpio_get_function,
140 .xlate = rockchip_gpio_xlate,
141};
142
143static const struct udevice_id rockchip_gpio_ids[] = {
144 { .compatible = "rockchip,gpio-bank" },
145 { }
146};
147
148U_BOOT_DRIVER(gpio_rockchip) = {
149 .name = "gpio_rockchip",
150 .id = UCLASS_GPIO,
151 .of_match = rockchip_gpio_ids,
152 .ops = &gpio_rockchip_ops,
153 .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
154 .probe = rockchip_gpio_probe,
155};