blob: 8a045cdf7aa8948e9567b61a34f326286a9a416b [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>
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053010#include <dm.h>
11#include <errno.h>
12#include <asm/io.h>
13#include "pinctrl-exynos.h"
14
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060015/* CON, DAT, PUD, DRV */
16const 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 Protsenko0aea23b2023-11-30 14:13:50 -060021static 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 Abrahamcf18bff2016-04-23 22:18:08 +053028/**
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 */
34void 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 Protsenko18357b32023-11-30 14:13:47 -060047static 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 Protsenko3132b922023-11-30 14:13:48 -060063/* given a bank name, find out the pin bank structure */
64static const struct samsung_pin_bank_data *get_bank(struct udevice *dev,
65 const char *bank_name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053066{
67 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030068 const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl;
69 const struct samsung_pin_bank_data *bank_data;
Sam Protsenko3132b922023-11-30 14:13:48 -060070 u32 nr_banks, pin_ctrl_idx = 0, idx = 0;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053071
72 /* lookup the pin bank data using the pin bank name */
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030073 while (true) {
Sam Protsenko769abf72023-11-30 14:13:46 -060074 const struct samsung_pin_ctrl *pin_ctrl =
75 &pin_ctrl_array[pin_ctrl_idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030076
77 nr_banks = pin_ctrl->nr_banks;
78 if (!nr_banks)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053079 break;
80
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030081 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 Protsenko3132b922023-11-30 14:13:48 -060085 if (!strcmp(bank_name, bank_data[idx].name))
86 return &bank_data[idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030087 }
88 pin_ctrl_idx++;
89 }
90
Sam Protsenko3132b922023-11-30 14:13:48 -060091 return NULL;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053092}
93
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060094static 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 Abrahamcf18bff2016-04-23 22:18:08 +0530110/**
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 */
115int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config)
116{
Sam Protsenko3132b922023-11-30 14:13:48 -0600117 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Sam Protsenko7993e842023-11-30 14:13:51 -0600118 unsigned int count, idx;
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600119 unsigned int pinvals[PINCFG_TYPE_NUM];
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530120
121 /*
122 * refer to the following document for the pinctrl bindings
123 * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt
124 */
Sam Protsenko5f621432023-11-30 14:13:52 -0600125 count = dev_read_string_count(config, "samsung,pins");
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530126 if (count <= 0)
127 return -EINVAL;
128
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600129 for (idx = 0; idx < PINCFG_TYPE_NUM; ++idx) {
Sam Protsenko5f621432023-11-30 14:13:52 -0600130 pinvals[idx] = dev_read_u32_default(config,
131 exynos_pinctrl_props[idx], -1);
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600132 }
133 pinvals[PINCFG_TYPE_DAT] = -1; /* ignore GPIO data register */
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530134
135 for (idx = 0; idx < count; idx++) {
Sam Protsenko3132b922023-11-30 14:13:48 -0600136 const struct samsung_pin_bank_data *bank;
Sam Protsenko7993e842023-11-30 14:13:51 -0600137 unsigned int pin_num;
Sam Protsenko3132b922023-11-30 14:13:48 -0600138 char bank_name[10];
Sam Protsenko7993e842023-11-30 14:13:51 -0600139 unsigned long reg;
Sam Protsenko5f621432023-11-30 14:13:52 -0600140 const char *name = NULL;
141 int pincfg, err;
Sam Protsenko3132b922023-11-30 14:13:48 -0600142
Sam Protsenko5f621432023-11-30 14:13:52 -0600143 err = dev_read_string_index(config, "samsung,pins", idx, &name);
144 if (err || !name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530145 continue;
Sam Protsenko5f621432023-11-30 14:13:52 -0600146
Sam Protsenko3132b922023-11-30 14:13:48 -0600147 parse_pin(name, &pin_num, bank_name);
148 bank = get_bank(dev, bank_name);
149 reg = priv->base + bank->offset;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530150
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600151 for (pincfg = 0; pincfg < PINCFG_TYPE_NUM; ++pincfg) {
152 unsigned int val = pinvals[pincfg];
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530153
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600154 if (val != -1)
155 exynos_pinctrl_set_pincfg(reg, pin_num, val,
156 pincfg, bank->type);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530157 }
158 }
159
160 return 0;
161}
162
163int 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 Yamadaa89b4de2020-07-17 14:36:48 +0900172 base = dev_read_addr(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530173 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 Glass99382ba2020-12-16 21:20:25 -0700178 dev_seq(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530179
180 return 0;
181}