blob: fefe3ca20359ab0b849fe4d6735cae92a4b08c8b [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 Glass48647822016-01-21 19:44:09 -070019#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass1f8f7732015-08-30 16:55:27 -060020
21enum {
22 ROCKCHIP_GPIOS_PER_BANK = 32,
23};
24
25#define OFFSET_TO_BIT(bit) (1UL << (bit))
26
27struct rockchip_gpio_priv {
28 struct rockchip_gpio_regs *regs;
Simon Glass48647822016-01-21 19:44:09 -070029 struct udevice *pinctrl;
30 int bank;
Simon Glass1f8f7732015-08-30 16:55:27 -060031 char name[2];
32};
33
34static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
35{
36 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
37 struct rockchip_gpio_regs *regs = priv->regs;
38
39 clrbits_le32(&regs->swport_ddr, OFFSET_TO_BIT(offset));
40
41 return 0;
42}
43
44static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
45 int value)
46{
47 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
48 struct rockchip_gpio_regs *regs = priv->regs;
49 int mask = OFFSET_TO_BIT(offset);
50
51 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
52 setbits_le32(&regs->swport_ddr, mask);
53
54 return 0;
55}
56
57static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
58{
59 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
60 struct rockchip_gpio_regs *regs = priv->regs;
61
Simon Glass7d0c2c32016-01-21 19:44:08 -070062 return readl(&regs->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
Simon Glass1f8f7732015-08-30 16:55:27 -060063}
64
65static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
66 int value)
67{
68 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
69 struct rockchip_gpio_regs *regs = priv->regs;
70 int mask = OFFSET_TO_BIT(offset);
71
72 clrsetbits_le32(&regs->swport_dr, mask, value ? mask : 0);
73
74 return 0;
75}
76
77static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
78{
Simon Glass48647822016-01-21 19:44:09 -070079#ifdef CONFIG_SPL_BUILD
80 return -ENODATA;
81#else
82 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
83 struct rockchip_gpio_regs *regs = priv->regs;
84 bool is_output;
85 int ret;
86
87 ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
88 if (ret)
89 return ret;
90
91 /* If it's not 0, then it is not a GPIO */
92 if (ret)
93 return GPIOF_FUNC;
94 is_output = readl(&regs->swport_ddr) & OFFSET_TO_BIT(offset);
95
96 return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
97#endif
Simon Glass1f8f7732015-08-30 16:55:27 -060098}
99
Simon Glass1f8f7732015-08-30 16:55:27 -0600100static int rockchip_gpio_probe(struct udevice *dev)
101{
102 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
103 struct rockchip_gpio_priv *priv = dev_get_priv(dev);
104 char *end;
Simon Glass48647822016-01-21 19:44:09 -0700105 int ret;
Simon Glass1f8f7732015-08-30 16:55:27 -0600106
Simon Glass48647822016-01-21 19:44:09 -0700107 /* This only supports RK3288 at present */
Simon Glass1f8f7732015-08-30 16:55:27 -0600108 priv->regs = (struct rockchip_gpio_regs *)dev_get_addr(dev);
Simon Glass3f603cb2016-02-11 13:23:26 -0700109 ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
Simon Glass48647822016-01-21 19:44:09 -0700110 if (ret)
111 return ret;
Simon Glass48647822016-01-21 19:44:09 -0700112
Simon Glass1f8f7732015-08-30 16:55:27 -0600113 uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
114 end = strrchr(dev->name, '@');
Simon Glass48647822016-01-21 19:44:09 -0700115 priv->bank = trailing_strtoln(dev->name, end);
116 priv->name[0] = 'A' + priv->bank;
Simon Glass1f8f7732015-08-30 16:55:27 -0600117 uc_priv->bank_name = priv->name;
118
119 return 0;
120}
121
122static const struct dm_gpio_ops gpio_rockchip_ops = {
123 .direction_input = rockchip_gpio_direction_input,
124 .direction_output = rockchip_gpio_direction_output,
125 .get_value = rockchip_gpio_get_value,
126 .set_value = rockchip_gpio_set_value,
127 .get_function = rockchip_gpio_get_function,
Simon Glass1f8f7732015-08-30 16:55:27 -0600128};
129
130static const struct udevice_id rockchip_gpio_ids[] = {
131 { .compatible = "rockchip,gpio-bank" },
132 { }
133};
134
135U_BOOT_DRIVER(gpio_rockchip) = {
136 .name = "gpio_rockchip",
137 .id = UCLASS_GPIO,
138 .of_match = rockchip_gpio_ids,
139 .ops = &gpio_rockchip_ops,
140 .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
141 .probe = rockchip_gpio_probe,
142};