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