David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <dm/pinctrl.h> |
| 8 | #include <regmap.h> |
| 9 | #include <syscon.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 11 | |
| 12 | #include "pinctrl-rockchip.h" |
| 13 | |
| 14 | static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = { |
| 15 | { |
| 16 | .num = 2, |
| 17 | .pin = 20, |
| 18 | .reg = 0xe8, |
| 19 | .bit = 0, |
| 20 | .mask = 0x7 |
| 21 | }, { |
| 22 | .num = 2, |
| 23 | .pin = 21, |
| 24 | .reg = 0xe8, |
| 25 | .bit = 4, |
| 26 | .mask = 0x7 |
| 27 | }, { |
| 28 | .num = 2, |
| 29 | .pin = 22, |
| 30 | .reg = 0xe8, |
| 31 | .bit = 8, |
| 32 | .mask = 0x7 |
| 33 | }, { |
| 34 | .num = 2, |
| 35 | .pin = 23, |
| 36 | .reg = 0xe8, |
| 37 | .bit = 12, |
| 38 | .mask = 0x7 |
| 39 | }, { |
| 40 | .num = 2, |
| 41 | .pin = 24, |
| 42 | .reg = 0xd4, |
| 43 | .bit = 12, |
| 44 | .mask = 0x7 |
| 45 | }, |
| 46 | }; |
| 47 | |
| 48 | static struct rockchip_mux_route_data rk3128_mux_route_data[] = { |
| 49 | { |
| 50 | /* spi-0 */ |
| 51 | .bank_num = 1, |
| 52 | .pin = 10, |
| 53 | .func = 1, |
| 54 | .route_offset = 0x144, |
| 55 | .route_val = BIT(16 + 3) | BIT(16 + 4), |
| 56 | }, { |
| 57 | /* spi-1 */ |
| 58 | .bank_num = 1, |
| 59 | .pin = 27, |
| 60 | .func = 3, |
| 61 | .route_offset = 0x144, |
| 62 | .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3), |
| 63 | }, { |
| 64 | /* spi-2 */ |
| 65 | .bank_num = 0, |
| 66 | .pin = 13, |
| 67 | .func = 2, |
| 68 | .route_offset = 0x144, |
| 69 | .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4), |
| 70 | }, { |
| 71 | /* i2s-0 */ |
| 72 | .bank_num = 1, |
| 73 | .pin = 5, |
| 74 | .func = 1, |
| 75 | .route_offset = 0x144, |
| 76 | .route_val = BIT(16 + 5), |
| 77 | }, { |
| 78 | /* i2s-1 */ |
| 79 | .bank_num = 0, |
| 80 | .pin = 14, |
| 81 | .func = 1, |
| 82 | .route_offset = 0x144, |
| 83 | .route_val = BIT(16 + 5) | BIT(5), |
| 84 | }, { |
| 85 | /* emmc-0 */ |
| 86 | .bank_num = 1, |
| 87 | .pin = 22, |
| 88 | .func = 2, |
| 89 | .route_offset = 0x144, |
| 90 | .route_val = BIT(16 + 6), |
| 91 | }, { |
| 92 | /* emmc-1 */ |
| 93 | .bank_num = 2, |
| 94 | .pin = 4, |
| 95 | .func = 2, |
| 96 | .route_offset = 0x144, |
| 97 | .route_val = BIT(16 + 6) | BIT(6), |
| 98 | }, |
| 99 | }; |
| 100 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 101 | static int rk3128_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 102 | { |
| 103 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 104 | int iomux_num = (pin / 8); |
| 105 | struct regmap *regmap; |
| 106 | int reg, ret, mask, mux_type; |
| 107 | u8 bit; |
Jagan Teki | 9e0e681 | 2022-12-14 23:20:56 +0530 | [diff] [blame] | 108 | u32 data; |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 109 | |
| 110 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 111 | ? priv->regmap_pmu : priv->regmap_base; |
| 112 | |
| 113 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 114 | mux_type = bank->iomux[iomux_num].type; |
| 115 | reg = bank->iomux[iomux_num].offset; |
| 116 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 117 | |
| 118 | if (bank->recalced_mask & BIT(pin)) |
| 119 | rockchip_get_recalced_mux(bank, pin, ®, &bit, &mask); |
| 120 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 121 | data = (mask << (bit + 16)); |
| 122 | data |= (mux & mask) << bit; |
| 123 | ret = regmap_write(regmap, reg, data); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 128 | #define RK3128_PULL_OFFSET 0x118 |
| 129 | #define RK3128_PULL_PINS_PER_REG 16 |
| 130 | #define RK3128_PULL_BANK_STRIDE 8 |
| 131 | |
| 132 | static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 133 | int pin_num, struct regmap **regmap, |
| 134 | int *reg, u8 *bit) |
| 135 | { |
| 136 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 137 | |
| 138 | *regmap = priv->regmap_base; |
| 139 | *reg = RK3128_PULL_OFFSET; |
| 140 | *reg += bank->bank_num * RK3128_PULL_BANK_STRIDE; |
| 141 | *reg += ((pin_num / RK3128_PULL_PINS_PER_REG) * 4); |
| 142 | |
| 143 | *bit = pin_num % RK3128_PULL_PINS_PER_REG; |
| 144 | } |
| 145 | |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 146 | static int rk3128_set_pull(struct rockchip_pin_bank *bank, |
| 147 | int pin_num, int pull) |
| 148 | { |
| 149 | struct regmap *regmap; |
| 150 | int reg, ret; |
| 151 | u8 bit; |
| 152 | u32 data; |
| 153 | |
| 154 | if (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT && |
| 155 | pull != PIN_CONFIG_BIAS_DISABLE) |
| 156 | return -ENOTSUPP; |
| 157 | |
| 158 | rk3128_calc_pull_reg_and_bit(bank, pin_num, ®map, ®, &bit); |
| 159 | data = BIT(bit + 16); |
| 160 | if (pull == PIN_CONFIG_BIAS_DISABLE) |
| 161 | data |= BIT(bit); |
| 162 | ret = regmap_write(regmap, reg, data); |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 167 | static struct rockchip_pin_bank rk3128_pin_banks[] = { |
| 168 | PIN_BANK(0, 32, "gpio0"), |
| 169 | PIN_BANK(1, 32, "gpio1"), |
| 170 | PIN_BANK(2, 32, "gpio2"), |
| 171 | PIN_BANK(3, 32, "gpio3"), |
| 172 | }; |
| 173 | |
| 174 | static struct rockchip_pin_ctrl rk3128_pin_ctrl = { |
| 175 | .pin_banks = rk3128_pin_banks, |
| 176 | .nr_banks = ARRAY_SIZE(rk3128_pin_banks), |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 177 | .grf_mux_offset = 0xa8, |
| 178 | .iomux_recalced = rk3128_mux_recalced_data, |
| 179 | .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data), |
| 180 | .iomux_routes = rk3128_mux_route_data, |
| 181 | .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data), |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame] | 182 | .set_mux = rk3128_set_mux, |
David Wu | 2972c45 | 2019-04-16 21:57:05 +0800 | [diff] [blame] | 183 | .set_pull = rk3128_set_pull, |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | static const struct udevice_id rk3128_pinctrl_ids[] = { |
| 187 | { .compatible = "rockchip,rk3128-pinctrl", |
| 188 | .data = (ulong)&rk3128_pin_ctrl }, |
| 189 | { } |
| 190 | }; |
| 191 | |
| 192 | U_BOOT_DRIVER(pinctrl_rk3128) = { |
| 193 | .name = "pinctrl_rk3128", |
| 194 | .id = UCLASS_PINCTRL, |
| 195 | .of_match = rk3128_pinctrl_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 196 | .priv_auto = sizeof(struct rockchip_pinctrl_priv), |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 197 | .ops = &rockchip_pinctrl_ops, |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 198 | #if CONFIG_IS_ENABLED(OF_REAL) |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 199 | .bind = dm_scan_fdt_dev, |
| 200 | #endif |
| 201 | .probe = rockchip_pinctrl_probe, |
| 202 | }; |