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 | |
| 18 | /** |
| 19 | * exynos_pinctrl_setup_peri: setup pinctrl for a peripheral. |
| 20 | * conf: soc specific pin configuration data array |
| 21 | * num_conf: number of configurations in the conf array. |
| 22 | * base: base address of the pin controller. |
| 23 | */ |
| 24 | void exynos_pinctrl_setup_peri(struct exynos_pinctrl_config_data *conf, |
| 25 | unsigned int num_conf, unsigned long base) |
| 26 | { |
| 27 | unsigned int idx, val; |
| 28 | |
| 29 | for (idx = 0; idx < num_conf; idx++) { |
| 30 | val = readl(base + conf[idx].offset); |
| 31 | val &= ~(conf[idx].mask); |
| 32 | val |= conf[idx].value; |
| 33 | writel(val, base + conf[idx].offset); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /* given a pin-name, return the address of pin config registers */ |
| 38 | static unsigned long pin_to_bank_base(struct udevice *dev, const char *pin_name, |
| 39 | u32 *pin) |
| 40 | { |
| 41 | struct exynos_pinctrl_priv *priv = dev_get_priv(dev); |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 42 | const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl; |
| 43 | const struct samsung_pin_bank_data *bank_data; |
| 44 | u32 nr_banks, pin_ctrl_idx = 0, idx = 0, bank_base; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 45 | char bank[10]; |
| 46 | |
| 47 | /* |
| 48 | * The format of the pin name is <bank name>-<pin_number>. |
| 49 | * Example: gpa0-4 (gpa0 is the bank name and 4 is the pin number. |
| 50 | */ |
| 51 | while (pin_name[idx] != '-') { |
| 52 | bank[idx] = pin_name[idx]; |
| 53 | idx++; |
| 54 | } |
| 55 | bank[idx] = '\0'; |
| 56 | *pin = pin_name[++idx] - '0'; |
| 57 | |
| 58 | /* lookup the pin bank data using the pin bank name */ |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 59 | while (true) { |
| 60 | const struct samsung_pin_ctrl *pin_ctrl = &pin_ctrl_array[pin_ctrl_idx]; |
| 61 | |
| 62 | nr_banks = pin_ctrl->nr_banks; |
| 63 | if (!nr_banks) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 64 | break; |
| 65 | |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 66 | bank_data = pin_ctrl->pin_banks; |
| 67 | for (idx = 0; idx < nr_banks; idx++) { |
| 68 | debug("pinctrl[%d] bank_data[%d] name is: %s\n", |
| 69 | pin_ctrl_idx, idx, bank_data[idx].name); |
| 70 | if (!strcmp(bank, bank_data[idx].name)) { |
| 71 | bank_base = priv->base + bank_data[idx].offset; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | pin_ctrl_idx++; |
| 76 | } |
| 77 | |
| 78 | return bank_base; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * exynos_pinctrl_set_state: configure a pin state. |
| 83 | * dev: the pinctrl device to be configured. |
| 84 | * config: the state to be configured. |
| 85 | */ |
| 86 | int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 87 | { |
| 88 | const void *fdt = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 89 | int node = dev_of_offset(config); |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 90 | unsigned int count, idx, pin_num; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 91 | unsigned int pinfunc, pinpud, pindrv; |
| 92 | unsigned long reg, value; |
| 93 | const char *name; |
| 94 | |
| 95 | /* |
| 96 | * refer to the following document for the pinctrl bindings |
| 97 | * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt |
| 98 | */ |
Masahiro Yamada | db090a2 | 2016-10-17 20:43:01 +0900 | [diff] [blame] | 99 | count = fdt_stringlist_count(fdt, node, "samsung,pins"); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 100 | if (count <= 0) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | pinfunc = fdtdec_get_int(fdt, node, "samsung,pin-function", -1); |
| 104 | pinpud = fdtdec_get_int(fdt, node, "samsung,pin-pud", -1); |
| 105 | pindrv = fdtdec_get_int(fdt, node, "samsung,pin-drv", -1); |
| 106 | |
| 107 | for (idx = 0; idx < count; idx++) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 108 | name = fdt_stringlist_get(fdt, node, "samsung,pins", idx, NULL); |
| 109 | if (!name) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 110 | continue; |
| 111 | reg = pin_to_bank_base(dev, name, &pin_num); |
| 112 | |
| 113 | if (pinfunc != -1) { |
| 114 | value = readl(reg + PIN_CON); |
| 115 | value &= ~(0xf << (pin_num << 2)); |
| 116 | value |= (pinfunc << (pin_num << 2)); |
| 117 | writel(value, reg + PIN_CON); |
| 118 | } |
| 119 | |
| 120 | if (pinpud != -1) { |
| 121 | value = readl(reg + PIN_PUD); |
| 122 | value &= ~(0x3 << (pin_num << 1)); |
| 123 | value |= (pinpud << (pin_num << 1)); |
| 124 | writel(value, reg + PIN_PUD); |
| 125 | } |
| 126 | |
| 127 | if (pindrv != -1) { |
| 128 | value = readl(reg + PIN_DRV); |
| 129 | value &= ~(0x3 << (pin_num << 1)); |
| 130 | value |= (pindrv << (pin_num << 1)); |
| 131 | writel(value, reg + PIN_DRV); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int exynos_pinctrl_probe(struct udevice *dev) |
| 139 | { |
| 140 | struct exynos_pinctrl_priv *priv; |
| 141 | fdt_addr_t base; |
| 142 | |
| 143 | priv = dev_get_priv(dev); |
| 144 | if (!priv) |
| 145 | return -EINVAL; |
| 146 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 147 | base = dev_read_addr(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 148 | if (base == FDT_ADDR_T_NONE) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | priv->base = base; |
| 152 | priv->pin_ctrl = (struct samsung_pin_ctrl *)dev_get_driver_data(dev) + |
Simon Glass | 99382ba | 2020-12-16 21:20:25 -0700 | [diff] [blame] | 153 | dev_seq(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |