Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Exynos pinctrl driver common code. |
| 4 | * Copyright (C) 2016 Samsung Electronics |
| 5 | * Thomas Abraham <thomas.ab@samsung.com> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 8 | #include <log.h> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include "pinctrl-exynos.h" |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Sam Protsenko | 2fb6c4f | 2023-11-30 14:13:49 -0600 | [diff] [blame] | 18 | /* CON, DAT, PUD, DRV */ |
| 19 | const struct samsung_pin_bank_type bank_type_alive = { |
| 20 | .fld_width = { 4, 1, 2, 2, }, |
| 21 | .reg_offset = { 0x00, 0x04, 0x08, 0x0c, }, |
| 22 | }; |
| 23 | |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 24 | static const char * const exynos_pinctrl_props[PINCFG_TYPE_NUM] = { |
| 25 | [PINCFG_TYPE_FUNC] = "samsung,pin-function", |
| 26 | [PINCFG_TYPE_DAT] = "samsung,pin-val", |
| 27 | [PINCFG_TYPE_PUD] = "samsung,pin-pud", |
| 28 | [PINCFG_TYPE_DRV] = "samsung,pin-drv", |
| 29 | }; |
| 30 | |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 31 | /** |
| 32 | * exynos_pinctrl_setup_peri: setup pinctrl for a peripheral. |
| 33 | * conf: soc specific pin configuration data array |
| 34 | * num_conf: number of configurations in the conf array. |
| 35 | * base: base address of the pin controller. |
| 36 | */ |
| 37 | void exynos_pinctrl_setup_peri(struct exynos_pinctrl_config_data *conf, |
| 38 | unsigned int num_conf, unsigned long base) |
| 39 | { |
| 40 | unsigned int idx, val; |
| 41 | |
| 42 | for (idx = 0; idx < num_conf; idx++) { |
| 43 | val = readl(base + conf[idx].offset); |
| 44 | val &= ~(conf[idx].mask); |
| 45 | val |= conf[idx].value; |
| 46 | writel(val, base + conf[idx].offset); |
| 47 | } |
| 48 | } |
| 49 | |
Sam Protsenko | 18357b3 | 2023-11-30 14:13:47 -0600 | [diff] [blame] | 50 | static void parse_pin(const char *pin_name, u32 *pin, char *bank_name) |
| 51 | { |
| 52 | u32 idx = 0; |
| 53 | |
| 54 | /* |
| 55 | * The format of the pin name is <bank_name name>-<pin_number>. |
| 56 | * Example: gpa0-4 (gpa0 is the bank_name name and 4 is the pin number. |
| 57 | */ |
| 58 | while (pin_name[idx] != '-') { |
| 59 | bank_name[idx] = pin_name[idx]; |
| 60 | idx++; |
| 61 | } |
| 62 | bank_name[idx] = '\0'; |
| 63 | *pin = pin_name[++idx] - '0'; |
| 64 | } |
| 65 | |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 66 | /* given a bank name, find out the pin bank structure */ |
| 67 | static const struct samsung_pin_bank_data *get_bank(struct udevice *dev, |
| 68 | const char *bank_name) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 69 | { |
| 70 | struct exynos_pinctrl_priv *priv = dev_get_priv(dev); |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 71 | const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl; |
| 72 | const struct samsung_pin_bank_data *bank_data; |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 73 | u32 nr_banks, pin_ctrl_idx = 0, idx = 0; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 74 | |
| 75 | /* lookup the pin bank data using the pin bank name */ |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 76 | while (true) { |
Sam Protsenko | 769abf7 | 2023-11-30 14:13:46 -0600 | [diff] [blame] | 77 | const struct samsung_pin_ctrl *pin_ctrl = |
| 78 | &pin_ctrl_array[pin_ctrl_idx]; |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 79 | |
| 80 | nr_banks = pin_ctrl->nr_banks; |
| 81 | if (!nr_banks) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 82 | break; |
| 83 | |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 84 | bank_data = pin_ctrl->pin_banks; |
| 85 | for (idx = 0; idx < nr_banks; idx++) { |
| 86 | debug("pinctrl[%d] bank_data[%d] name is: %s\n", |
| 87 | pin_ctrl_idx, idx, bank_data[idx].name); |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 88 | if (!strcmp(bank_name, bank_data[idx].name)) |
| 89 | return &bank_data[idx]; |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 90 | } |
| 91 | pin_ctrl_idx++; |
| 92 | } |
| 93 | |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 94 | return NULL; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 95 | } |
| 96 | |
Sam Protsenko | 2fb6c4f | 2023-11-30 14:13:49 -0600 | [diff] [blame] | 97 | static void exynos_pinctrl_set_pincfg(unsigned long reg_base, u32 pin_num, |
| 98 | u32 val, enum pincfg_type pincfg, |
| 99 | const struct samsung_pin_bank_type *type) |
| 100 | { |
| 101 | u32 width = type->fld_width[pincfg]; |
| 102 | u32 reg_offset = type->reg_offset[pincfg]; |
| 103 | u32 mask = (1 << width) - 1; |
| 104 | u32 shift = pin_num * width; |
| 105 | u32 data; |
| 106 | |
| 107 | data = readl(reg_base + reg_offset); |
| 108 | data &= ~(mask << shift); |
| 109 | data |= val << shift; |
| 110 | writel(data, reg_base + reg_offset); |
| 111 | } |
| 112 | |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 113 | /** |
| 114 | * exynos_pinctrl_set_state: configure a pin state. |
| 115 | * dev: the pinctrl device to be configured. |
| 116 | * config: the state to be configured. |
| 117 | */ |
| 118 | int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 119 | { |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 120 | struct exynos_pinctrl_priv *priv = dev_get_priv(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 121 | const void *fdt = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 122 | int node = dev_of_offset(config); |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 123 | unsigned int count, idx, pin_num; |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 124 | unsigned int pinvals[PINCFG_TYPE_NUM]; |
Sam Protsenko | 2fb6c4f | 2023-11-30 14:13:49 -0600 | [diff] [blame] | 125 | unsigned long reg; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 126 | const char *name; |
| 127 | |
| 128 | /* |
| 129 | * refer to the following document for the pinctrl bindings |
| 130 | * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt |
| 131 | */ |
Masahiro Yamada | db090a2 | 2016-10-17 20:43:01 +0900 | [diff] [blame] | 132 | count = fdt_stringlist_count(fdt, node, "samsung,pins"); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 133 | if (count <= 0) |
| 134 | return -EINVAL; |
| 135 | |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 136 | for (idx = 0; idx < PINCFG_TYPE_NUM; ++idx) { |
| 137 | pinvals[idx] = fdtdec_get_int(fdt, node, |
| 138 | exynos_pinctrl_props[idx], -1); |
| 139 | } |
| 140 | pinvals[PINCFG_TYPE_DAT] = -1; /* ignore GPIO data register */ |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 141 | |
| 142 | for (idx = 0; idx < count; idx++) { |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 143 | const struct samsung_pin_bank_data *bank; |
| 144 | char bank_name[10]; |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 145 | int pincfg; |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 146 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 147 | name = fdt_stringlist_get(fdt, node, "samsung,pins", idx, NULL); |
| 148 | if (!name) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 149 | continue; |
Sam Protsenko | 3132b92 | 2023-11-30 14:13:48 -0600 | [diff] [blame] | 150 | parse_pin(name, &pin_num, bank_name); |
| 151 | bank = get_bank(dev, bank_name); |
| 152 | reg = priv->base + bank->offset; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 153 | |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 154 | for (pincfg = 0; pincfg < PINCFG_TYPE_NUM; ++pincfg) { |
| 155 | unsigned int val = pinvals[pincfg]; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 156 | |
Sam Protsenko | 0aea23b | 2023-11-30 14:13:50 -0600 | [diff] [blame^] | 157 | if (val != -1) |
| 158 | exynos_pinctrl_set_pincfg(reg, pin_num, val, |
| 159 | pincfg, bank->type); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int exynos_pinctrl_probe(struct udevice *dev) |
| 167 | { |
| 168 | struct exynos_pinctrl_priv *priv; |
| 169 | fdt_addr_t base; |
| 170 | |
| 171 | priv = dev_get_priv(dev); |
| 172 | if (!priv) |
| 173 | return -EINVAL; |
| 174 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 175 | base = dev_read_addr(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 176 | if (base == FDT_ADDR_T_NONE) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | priv->base = base; |
| 180 | priv->pin_ctrl = (struct samsung_pin_ctrl *)dev_get_driver_data(dev) + |
Simon Glass | 99382ba | 2020-12-16 21:20:25 -0700 | [diff] [blame] | 181 | dev_seq(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |