blob: a8f311bbd675af265477e60f47cdab6ae9b640e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8fa4d5a2015-08-30 16:55:27 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 *
5 * (C) Copyright 2008-2014 Rockchip Electronics
6 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
Simon Glass8fa4d5a2015-08-30 16:55:27 -06007 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass9c73e742016-01-21 19:44:09 -070011#include <syscon.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060013#include <asm/gpio.h>
14#include <asm/io.h>
Simon Glass9c73e742016-01-21 19:44:09 -070015#include <asm/arch/clock.h>
16#include <dm/pinctrl.h>
Simon Glass9c73e742016-01-21 19:44:09 -070017#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass8fa4d5a2015-08-30 16:55:27 -060018
19enum {
20 ROCKCHIP_GPIOS_PER_BANK = 32,
21};
22
23#define OFFSET_TO_BIT(bit) (1UL << (bit))
24
25struct rockchip_gpio_priv {
26 struct rockchip_gpio_regs *regs;
Simon Glass9c73e742016-01-21 19:44:09 -070027 struct udevice *pinctrl;
28 int bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060029 char name[2];
30};
31
32static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
33{
34 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
35 struct rockchip_gpio_regs *regs = priv->regs;
36
37 clrbits_le32(&regs->swport_ddr, OFFSET_TO_BIT(offset));
38
39 return 0;
40}
41
42static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
43 int value)
44{
45 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
46 struct rockchip_gpio_regs *regs = priv->regs;
47 int mask = OFFSET_TO_BIT(offset);
48
49 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
50 setbits_le32(&regs->swport_ddr, mask);
51
52 return 0;
53}
54
55static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
56{
57 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
58 struct rockchip_gpio_regs *regs = priv->regs;
59
Simon Glassc8d72402016-01-21 19:44:08 -070060 return readl(&regs->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
Simon Glass8fa4d5a2015-08-30 16:55:27 -060061}
62
63static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
64 int value)
65{
66 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
67 struct rockchip_gpio_regs *regs = priv->regs;
68 int mask = OFFSET_TO_BIT(offset);
69
70 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
71
72 return 0;
73}
74
75static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
76{
Simon Glass9c73e742016-01-21 19:44:09 -070077#ifdef CONFIG_SPL_BUILD
78 return -ENODATA;
79#else
80 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
81 struct rockchip_gpio_regs *regs = priv->regs;
82 bool is_output;
83 int ret;
84
85 ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
86 if (ret)
87 return ret;
Simon Glass9c73e742016-01-21 19:44:09 -070088 is_output = readl(&regs->swport_ddr) & OFFSET_TO_BIT(offset);
89
90 return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
91#endif
Simon Glass8fa4d5a2015-08-30 16:55:27 -060092}
93
Simon Glass8fa4d5a2015-08-30 16:55:27 -060094static int rockchip_gpio_probe(struct udevice *dev)
95{
96 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
97 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
98 char *end;
Simon Glass9c73e742016-01-21 19:44:09 -070099 int ret;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600100
Philipp Tomsich66482052017-09-11 22:04:24 +0200101 priv->regs = dev_read_addr_ptr(dev);
Simon Glassc7298e72016-02-11 13:23:26 -0700102 ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
Simon Glass9c73e742016-01-21 19:44:09 -0700103 if (ret)
104 return ret;
Simon Glass9c73e742016-01-21 19:44:09 -0700105
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600106 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
107 end = strrchr(dev->name, '@');
Simon Glass9c73e742016-01-21 19:44:09 -0700108 priv->bank = trailing_strtoln(dev->name, end);
109 priv->name[0] = 'A' + priv->bank;
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600110 uc_priv->bank_name = priv->name;
111
112 return 0;
113}
114
115static const struct dm_gpio_ops gpio_rockchip_ops = {
116 .direction_input = rockchip_gpio_direction_input,
117 .direction_output = rockchip_gpio_direction_output,
118 .get_value = rockchip_gpio_get_value,
119 .set_value = rockchip_gpio_set_value,
120 .get_function = rockchip_gpio_get_function,
Simon Glass8fa4d5a2015-08-30 16:55:27 -0600121};
122
123static const struct udevice_id rockchip_gpio_ids[] = {
124 { .compatible = "rockchip,gpio-bank" },
125 { }
126};
127
128U_BOOT_DRIVER(gpio_rockchip) = {
129 .name = "gpio_rockchip",
130 .id = UCLASS_GPIO,
131 .of_match = rockchip_gpio_ids,
132 .ops = &gpio_rockchip_ops,
133 .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
134 .probe = rockchip_gpio_probe,
135};