Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2008-2014 Rockchip Electronics |
| 6 | * Peter, Software Engineering, <superpeter.cai@gmail.com>. |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 11 | #include <syscon.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 13 | #include <asm/gpio.h> |
| 14 | #include <asm/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 15 | #include <asm/arch-rockchip/clock.h> |
| 16 | #include <asm/arch-rockchip/gpio.h> |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 17 | #include <dm/pinctrl.h> |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 18 | #include <dt-bindings/clock/rk3288-cru.h> |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 19 | |
| 20 | enum { |
| 21 | ROCKCHIP_GPIOS_PER_BANK = 32, |
| 22 | }; |
| 23 | |
| 24 | #define OFFSET_TO_BIT(bit) (1UL << (bit)) |
| 25 | |
| 26 | struct rockchip_gpio_priv { |
| 27 | struct rockchip_gpio_regs *regs; |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 28 | struct udevice *pinctrl; |
| 29 | int bank; |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 30 | char name[2]; |
| 31 | }; |
| 32 | |
| 33 | static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 34 | { |
| 35 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 36 | struct rockchip_gpio_regs *regs = priv->regs; |
| 37 | |
| 38 | clrbits_le32(®s->swport_ddr, OFFSET_TO_BIT(offset)); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 44 | int value) |
| 45 | { |
| 46 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 47 | struct rockchip_gpio_regs *regs = priv->regs; |
| 48 | int mask = OFFSET_TO_BIT(offset); |
| 49 | |
| 50 | clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0); |
| 51 | setbits_le32(®s->swport_ddr, mask); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset) |
| 57 | { |
| 58 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 59 | struct rockchip_gpio_regs *regs = priv->regs; |
| 60 | |
Simon Glass | 7d0c2c3 | 2016-01-21 19:44:08 -0700 | [diff] [blame] | 61 | return readl(®s->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0; |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset, |
| 65 | int value) |
| 66 | { |
| 67 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 68 | struct rockchip_gpio_regs *regs = priv->regs; |
| 69 | int mask = OFFSET_TO_BIT(offset); |
| 70 | |
| 71 | clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset) |
| 77 | { |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_SPL_BUILD |
| 79 | return -ENODATA; |
| 80 | #else |
| 81 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 82 | struct rockchip_gpio_regs *regs = priv->regs; |
| 83 | bool is_output; |
| 84 | int ret; |
| 85 | |
| 86 | ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset); |
| 87 | if (ret) |
| 88 | return ret; |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 89 | is_output = readl(®s->swport_ddr) & OFFSET_TO_BIT(offset); |
| 90 | |
| 91 | return is_output ? GPIOF_OUTPUT : GPIOF_INPUT; |
| 92 | #endif |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 93 | } |
| 94 | |
Simon Glass | aa48c94 | 2019-01-21 14:53:34 -0700 | [diff] [blame] | 95 | /* Simple SPL interface to GPIOs */ |
| 96 | #ifdef CONFIG_SPL_BUILD |
| 97 | |
| 98 | enum { |
| 99 | PULL_NONE_1V8 = 0, |
| 100 | PULL_DOWN_1V8 = 1, |
| 101 | PULL_UP_1V8 = 3, |
| 102 | }; |
| 103 | |
| 104 | int spl_gpio_set_pull(void *vregs, uint gpio, int pull) |
| 105 | { |
| 106 | u32 *regs = vregs; |
| 107 | uint val; |
| 108 | |
| 109 | regs += gpio >> GPIO_BANK_SHIFT; |
| 110 | gpio &= GPIO_OFFSET_MASK; |
| 111 | switch (pull) { |
| 112 | case GPIO_PULL_UP: |
| 113 | val = PULL_UP_1V8; |
| 114 | break; |
| 115 | case GPIO_PULL_DOWN: |
| 116 | val = PULL_DOWN_1V8; |
| 117 | break; |
| 118 | case GPIO_PULL_NORMAL: |
| 119 | default: |
| 120 | val = PULL_NONE_1V8; |
| 121 | break; |
| 122 | } |
| 123 | clrsetbits_le32(regs, 3 << (gpio * 2), val << (gpio * 2)); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int spl_gpio_output(void *vregs, uint gpio, int value) |
| 129 | { |
| 130 | struct rockchip_gpio_regs * const regs = vregs; |
| 131 | |
| 132 | clrsetbits_le32(®s->swport_dr, 1 << gpio, value << gpio); |
| 133 | |
| 134 | /* Set direction */ |
| 135 | clrsetbits_le32(®s->swport_ddr, 1 << gpio, 1 << gpio); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | #endif /* CONFIG_SPL_BUILD */ |
| 140 | |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 141 | static int rockchip_gpio_probe(struct udevice *dev) |
| 142 | { |
| 143 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 144 | struct rockchip_gpio_priv *priv = dev_get_priv(dev); |
| 145 | char *end; |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 146 | int ret; |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 147 | |
Philipp Tomsich | a1d3480 | 2017-09-11 22:04:24 +0200 | [diff] [blame] | 148 | priv->regs = dev_read_addr_ptr(dev); |
Simon Glass | 3f603cb | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 149 | ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl); |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 150 | if (ret) |
| 151 | return ret; |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 152 | |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 153 | uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK; |
| 154 | end = strrchr(dev->name, '@'); |
Simon Glass | 4864782 | 2016-01-21 19:44:09 -0700 | [diff] [blame] | 155 | priv->bank = trailing_strtoln(dev->name, end); |
| 156 | priv->name[0] = 'A' + priv->bank; |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 157 | uc_priv->bank_name = priv->name; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static const struct dm_gpio_ops gpio_rockchip_ops = { |
| 163 | .direction_input = rockchip_gpio_direction_input, |
| 164 | .direction_output = rockchip_gpio_direction_output, |
| 165 | .get_value = rockchip_gpio_get_value, |
| 166 | .set_value = rockchip_gpio_set_value, |
| 167 | .get_function = rockchip_gpio_get_function, |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | static const struct udevice_id rockchip_gpio_ids[] = { |
| 171 | { .compatible = "rockchip,gpio-bank" }, |
| 172 | { } |
| 173 | }; |
| 174 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 175 | U_BOOT_DRIVER(rockchip_gpio_bank) = { |
| 176 | .name = "rockchip_gpio_bank", |
Simon Glass | 1f8f773 | 2015-08-30 16:55:27 -0600 | [diff] [blame] | 177 | .id = UCLASS_GPIO, |
| 178 | .of_match = rockchip_gpio_ids, |
| 179 | .ops = &gpio_rockchip_ops, |
| 180 | .priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv), |
| 181 | .probe = rockchip_gpio_probe, |
| 182 | }; |