blob: b393127c642c063116f753808b51219f4e091596 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05302/*
3 * Exynos pinctrl driver common code.
4 * Copyright (C) 2016 Samsung Electronics
5 * Thomas Abraham <thomas.ab@samsung.com>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05306 */
7
Dzmitry Sankouski0024e442021-10-17 13:45:40 +03008#include <log.h>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05309#include <dm.h>
10#include <errno.h>
11#include <asm/io.h>
12#include "pinctrl-exynos.h"
13
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060014/* CON, DAT, PUD, DRV */
15const struct samsung_pin_bank_type bank_type_alive = {
16 .fld_width = { 4, 1, 2, 2, },
17 .reg_offset = { 0x00, 0x04, 0x08, 0x0c, },
18};
19
Sam Protsenko0aea23b2023-11-30 14:13:50 -060020static const char * const exynos_pinctrl_props[PINCFG_TYPE_NUM] = {
21 [PINCFG_TYPE_FUNC] = "samsung,pin-function",
22 [PINCFG_TYPE_DAT] = "samsung,pin-val",
23 [PINCFG_TYPE_PUD] = "samsung,pin-pud",
24 [PINCFG_TYPE_DRV] = "samsung,pin-drv",
25};
26
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053027/**
28 * exynos_pinctrl_setup_peri: setup pinctrl for a peripheral.
29 * conf: soc specific pin configuration data array
30 * num_conf: number of configurations in the conf array.
31 * base: base address of the pin controller.
32 */
33void exynos_pinctrl_setup_peri(struct exynos_pinctrl_config_data *conf,
34 unsigned int num_conf, unsigned long base)
35{
36 unsigned int idx, val;
37
38 for (idx = 0; idx < num_conf; idx++) {
39 val = readl(base + conf[idx].offset);
40 val &= ~(conf[idx].mask);
41 val |= conf[idx].value;
42 writel(val, base + conf[idx].offset);
43 }
44}
45
Sam Protsenko18357b32023-11-30 14:13:47 -060046static void parse_pin(const char *pin_name, u32 *pin, char *bank_name)
47{
48 u32 idx = 0;
49
50 /*
51 * The format of the pin name is <bank_name name>-<pin_number>.
52 * Example: gpa0-4 (gpa0 is the bank_name name and 4 is the pin number.
53 */
54 while (pin_name[idx] != '-') {
55 bank_name[idx] = pin_name[idx];
56 idx++;
57 }
58 bank_name[idx] = '\0';
59 *pin = pin_name[++idx] - '0';
60}
61
Sam Protsenko3132b922023-11-30 14:13:48 -060062/* given a bank name, find out the pin bank structure */
63static const struct samsung_pin_bank_data *get_bank(struct udevice *dev,
64 const char *bank_name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053065{
66 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030067 const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl;
68 const struct samsung_pin_bank_data *bank_data;
Sam Protsenko3132b922023-11-30 14:13:48 -060069 u32 nr_banks, pin_ctrl_idx = 0, idx = 0;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053070
71 /* lookup the pin bank data using the pin bank name */
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030072 while (true) {
Sam Protsenko769abf72023-11-30 14:13:46 -060073 const struct samsung_pin_ctrl *pin_ctrl =
74 &pin_ctrl_array[pin_ctrl_idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030075
76 nr_banks = pin_ctrl->nr_banks;
77 if (!nr_banks)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053078 break;
79
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030080 bank_data = pin_ctrl->pin_banks;
81 for (idx = 0; idx < nr_banks; idx++) {
82 debug("pinctrl[%d] bank_data[%d] name is: %s\n",
83 pin_ctrl_idx, idx, bank_data[idx].name);
Sam Protsenko3132b922023-11-30 14:13:48 -060084 if (!strcmp(bank_name, bank_data[idx].name))
85 return &bank_data[idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030086 }
87 pin_ctrl_idx++;
88 }
89
Sam Protsenko3132b922023-11-30 14:13:48 -060090 return NULL;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053091}
92
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060093static void exynos_pinctrl_set_pincfg(unsigned long reg_base, u32 pin_num,
94 u32 val, enum pincfg_type pincfg,
95 const struct samsung_pin_bank_type *type)
96{
97 u32 width = type->fld_width[pincfg];
98 u32 reg_offset = type->reg_offset[pincfg];
99 u32 mask = (1 << width) - 1;
100 u32 shift = pin_num * width;
101 u32 data;
102
103 data = readl(reg_base + reg_offset);
104 data &= ~(mask << shift);
105 data |= val << shift;
106 writel(data, reg_base + reg_offset);
107}
108
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530109/**
110 * exynos_pinctrl_set_state: configure a pin state.
111 * dev: the pinctrl device to be configured.
112 * config: the state to be configured.
113 */
114int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config)
115{
Sam Protsenko3132b922023-11-30 14:13:48 -0600116 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Sam Protsenko7993e842023-11-30 14:13:51 -0600117 unsigned int count, idx;
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600118 unsigned int pinvals[PINCFG_TYPE_NUM];
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530119
120 /*
121 * refer to the following document for the pinctrl bindings
122 * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt
123 */
Sam Protsenko5f621432023-11-30 14:13:52 -0600124 count = dev_read_string_count(config, "samsung,pins");
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530125 if (count <= 0)
126 return -EINVAL;
127
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600128 for (idx = 0; idx < PINCFG_TYPE_NUM; ++idx) {
Sam Protsenko5f621432023-11-30 14:13:52 -0600129 pinvals[idx] = dev_read_u32_default(config,
130 exynos_pinctrl_props[idx], -1);
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600131 }
132 pinvals[PINCFG_TYPE_DAT] = -1; /* ignore GPIO data register */
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530133
134 for (idx = 0; idx < count; idx++) {
Sam Protsenko3132b922023-11-30 14:13:48 -0600135 const struct samsung_pin_bank_data *bank;
Sam Protsenko7993e842023-11-30 14:13:51 -0600136 unsigned int pin_num;
Sam Protsenko3132b922023-11-30 14:13:48 -0600137 char bank_name[10];
Sam Protsenko7993e842023-11-30 14:13:51 -0600138 unsigned long reg;
Sam Protsenko5f621432023-11-30 14:13:52 -0600139 const char *name = NULL;
140 int pincfg, err;
Sam Protsenko3132b922023-11-30 14:13:48 -0600141
Sam Protsenko5f621432023-11-30 14:13:52 -0600142 err = dev_read_string_index(config, "samsung,pins", idx, &name);
143 if (err || !name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530144 continue;
Sam Protsenko5f621432023-11-30 14:13:52 -0600145
Sam Protsenko3132b922023-11-30 14:13:48 -0600146 parse_pin(name, &pin_num, bank_name);
147 bank = get_bank(dev, bank_name);
148 reg = priv->base + bank->offset;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530149
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600150 for (pincfg = 0; pincfg < PINCFG_TYPE_NUM; ++pincfg) {
151 unsigned int val = pinvals[pincfg];
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530152
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600153 if (val != -1)
154 exynos_pinctrl_set_pincfg(reg, pin_num, val,
155 pincfg, bank->type);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530156 }
157 }
158
159 return 0;
160}
161
162int exynos_pinctrl_probe(struct udevice *dev)
163{
164 struct exynos_pinctrl_priv *priv;
165 fdt_addr_t base;
166
167 priv = dev_get_priv(dev);
168 if (!priv)
169 return -EINVAL;
170
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900171 base = dev_read_addr(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530172 if (base == FDT_ADDR_T_NONE)
173 return -EINVAL;
174
175 priv->base = base;
176 priv->pin_ctrl = (struct samsung_pin_ctrl *)dev_get_driver_data(dev) +
Simon Glass99382ba2020-12-16 21:20:25 -0700177 dev_seq(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530178
179 return 0;
180}