blob: c697a4c3456ae4b8c1084930c7570a30c01024b4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang309686c2016-07-20 17:16:27 +08002/*
3 * Atmel PIO4 pinctrl driver
4 *
5 * Copyright (C) 2016 Atmel Corporation
6 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yang309686c2016-07-20 17:16:27 +08007 */
8
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Sergiu Mogaf7009542022-09-01 17:22:41 +030011#include <dm/device-internal.h>
12#include <dm/lists.h>
Wenyou Yang309686c2016-07-20 17:16:27 +080013#include <dm/pinctrl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Wenyou Yang309686c2016-07-20 17:16:27 +080015#include <linux/io.h>
16#include <linux/err.h>
Sergiu Moga219a5fd2022-09-01 17:22:42 +030017#include <dm/uclass-internal.h>
Wenyou Yang309686c2016-07-20 17:16:27 +080018#include <mach/atmel_pio4.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22/*
23 * Warning:
24 * In order to not introduce confusion between Atmel PIO groups and pinctrl
25 * framework groups, Atmel PIO groups will be called banks.
26 */
27
Simon Glassb75b15b2020-12-03 16:55:23 -070028struct atmel_pio4_plat {
Wenyou Yang309686c2016-07-20 17:16:27 +080029 struct atmel_pio4_port *reg_base;
Claudiu Beznea153a7932021-01-27 15:00:30 +020030 unsigned int slew_rate_support;
Wenyou Yang309686c2016-07-20 17:16:27 +080031};
32
Sergiu Mogaf7009542022-09-01 17:22:41 +030033/*
34 * Table keeping track of the pinctrl driver's slew rate support and the
35 * corresponding index into the struct udevice_id of the gpio_atmel_pio4 GPIO
36 * driver. This has been done in order to align the DT of U-Boot with the DT of
37 * Linux. In Linux, a phandle from a '-gpio' DT property is linked to the
38 * pinctrl driver, unlike U-Boot which redirects this phandle to a corresponding
39 * UCLASS_GPIO driver. Thus, in order to link the two, a hook to the bind method
40 * of the pinctrl driver in U-Boot has been added. This bind method will attach
41 * the GPIO driver to the pinctrl DT node using this table.
42 * @slew_rate_support pinctrl driver's slew rate support
43 * @gdidx index into the GPIO driver's struct udevide_id
44 * (needed in order to properly bind with driver_data)
45 */
46
47struct atmel_pinctrl_data {
48 unsigned int slew_rate_support;
49 int gdidx;
50};
51
Wenyou Yang309686c2016-07-20 17:16:27 +080052static const struct pinconf_param conf_params[] = {
53 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
54 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
55 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
56 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
57 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
58 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
59 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
Eugen Hristev90add2b2021-01-05 10:54:01 +020060 { "atmel,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
Claudiu Beznea153a7932021-01-27 15:00:30 +020061 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0},
Wenyou Yang309686c2016-07-20 17:16:27 +080062};
63
Claudiu Beznea153a7932021-01-27 15:00:30 +020064static u32 atmel_pinctrl_get_pinconf(struct udevice *config,
65 struct atmel_pio4_plat *plat)
Wenyou Yang309686c2016-07-20 17:16:27 +080066{
67 const struct pinconf_param *params;
68 u32 param, arg, conf = 0;
69 u32 i;
Eugen Hristev90add2b2021-01-05 10:54:01 +020070 u32 val;
Wenyou Yang309686c2016-07-20 17:16:27 +080071
72 for (i = 0; i < ARRAY_SIZE(conf_params); i++) {
73 params = &conf_params[i];
Eugen Hristeve58dc842021-01-05 10:51:53 +020074 if (!dev_read_prop(config, params->property, NULL))
Wenyou Yang309686c2016-07-20 17:16:27 +080075 continue;
76
77 param = params->param;
78 arg = params->default_value;
79
Claudiu Beznea153a7932021-01-27 15:00:30 +020080 /* Keep slew rate enabled by default. */
81 if (plat->slew_rate_support)
82 conf |= ATMEL_PIO_SR;
83
Wenyou Yang309686c2016-07-20 17:16:27 +080084 switch (param) {
85 case PIN_CONFIG_BIAS_DISABLE:
86 conf &= (~ATMEL_PIO_PUEN_MASK);
87 conf &= (~ATMEL_PIO_PDEN_MASK);
88 break;
89 case PIN_CONFIG_BIAS_PULL_UP:
90 conf |= ATMEL_PIO_PUEN_MASK;
91 break;
92 case PIN_CONFIG_BIAS_PULL_DOWN:
93 conf |= ATMEL_PIO_PDEN_MASK;
94 break;
95 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
96 if (arg == 0)
97 conf &= (~ATMEL_PIO_OPD_MASK);
98 else
99 conf |= ATMEL_PIO_OPD_MASK;
100 break;
101 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
102 if (arg == 0)
103 conf |= ATMEL_PIO_SCHMITT_MASK;
104 else
105 conf &= (~ATMEL_PIO_SCHMITT_MASK);
106 break;
107 case PIN_CONFIG_INPUT_DEBOUNCE:
108 if (arg == 0) {
109 conf &= (~ATMEL_PIO_IFEN_MASK);
110 conf &= (~ATMEL_PIO_IFSCEN_MASK);
111 } else {
112 conf |= ATMEL_PIO_IFEN_MASK;
113 conf |= ATMEL_PIO_IFSCEN_MASK;
114 }
115 break;
Eugen Hristev90add2b2021-01-05 10:54:01 +0200116 case PIN_CONFIG_DRIVE_STRENGTH:
117 dev_read_u32(config, params->property, &val);
118 conf &= (~ATMEL_PIO_DRVSTR_MASK);
119 conf |= (val << ATMEL_PIO_DRVSTR_OFFSET)
120 & ATMEL_PIO_DRVSTR_MASK;
121 break;
Claudiu Beznea153a7932021-01-27 15:00:30 +0200122 case PIN_CONFIG_SLEW_RATE:
123 if (!plat->slew_rate_support)
124 break;
125
126 dev_read_u32(config, params->property, &val);
127 /* And disable it if requested. */
128 if (val == 0)
129 conf &= ~ATMEL_PIO_SR;
130 break;
Wenyou Yang309686c2016-07-20 17:16:27 +0800131 default:
132 printf("%s: Unsupported configuration parameter: %u\n",
133 __func__, param);
134 break;
135 }
136 }
137
138 return conf;
139}
140
141static inline struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
142 u32 bank)
143{
Simon Glassb75b15b2020-12-03 16:55:23 -0700144 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Wenyou Yang309686c2016-07-20 17:16:27 +0800145 struct atmel_pio4_port *bank_base =
146 (struct atmel_pio4_port *)((u32)plat->reg_base +
147 ATMEL_PIO_BANK_OFFSET * bank);
148
149 return bank_base;
150}
151
152#define MAX_PINMUX_ENTRIES 40
153
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300154static int atmel_process_config_dev(struct udevice *dev, struct udevice *config)
Wenyou Yang309686c2016-07-20 17:16:27 +0800155{
Claudiu Beznea153a7932021-01-27 15:00:30 +0200156 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700157 int node = dev_of_offset(config);
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300158 struct atmel_pio4_port *bank_base;
Wenyou Yang309686c2016-07-20 17:16:27 +0800159 u32 offset, func, bank, line;
160 u32 cells[MAX_PINMUX_ENTRIES];
161 u32 i, conf;
162 int count;
163
Claudiu Beznea153a7932021-01-27 15:00:30 +0200164 conf = atmel_pinctrl_get_pinconf(config, plat);
Wenyou Yang309686c2016-07-20 17:16:27 +0800165
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300166 /*
167 * The only case where this function returns a negative error value
168 * is when there is no "pinmux" property attached to this node
169 */
170 count = fdtdec_get_int_array_count(gd->fdt_blob, node, "pinmux",
Wenyou Yang309686c2016-07-20 17:16:27 +0800171 cells, ARRAY_SIZE(cells));
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300172 if (count < 0)
173 return count;
Wenyou Yang309686c2016-07-20 17:16:27 +0800174
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300175 if (count > MAX_PINMUX_ENTRIES)
Wenyou Yang309686c2016-07-20 17:16:27 +0800176 return -EINVAL;
Wenyou Yang309686c2016-07-20 17:16:27 +0800177
178 for (i = 0 ; i < count; i++) {
179 offset = ATMEL_GET_PIN_NO(cells[i]);
180 func = ATMEL_GET_PIN_FUNC(cells[i]);
181
182 bank = ATMEL_PIO_BANK(offset);
183 line = ATMEL_PIO_LINE(offset);
184
185 bank_base = atmel_pio4_bank_base(dev, bank);
186
187 writel(BIT(line), &bank_base->mskr);
188 conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
189 conf |= (func & ATMEL_PIO_CFGR_FUNC_MASK);
190 writel(conf, &bank_base->cfgr);
191 }
192
193 return 0;
194}
195
Sergiu Moga219a5fd2022-09-01 17:22:42 +0300196static int atmel_pinctrl_set_state(struct udevice *dev, struct udevice *config)
197{
198 int node = dev_of_offset(config);
199 struct udevice *subconfig;
200 int subnode, subnode_count = 0, ret;
201
202 /*
203 * If this function returns a negative error code then that means
204 * that either the "pinmux" property of the node is missing, which is
205 * the case for pinctrl nodes that do not have all the pins with the
206 * same configuration and are split in multiple subnodes, or something
207 * else went wrong and we have to stop. For the latter case, it would
208 * mean that the node failed even though it has no subnodes.
209 */
210 ret = atmel_process_config_dev(dev, config);
211 if (!ret)
212 return ret;
213
214 /*
215 * If we reach here, it means that the subnode pinctrl's DT has multiple
216 * subnodes. If it does not, then something else went wrong in the
217 * previous call to atmel_process_config_dev.
218 */
219 fdt_for_each_subnode(subnode, gd->fdt_blob, node) {
220 /* Get subnode as an udevice */
221 ret = uclass_find_device_by_of_offset(UCLASS_PINCONFIG, subnode,
222 &subconfig);
223 if (ret)
224 return ret;
225
226 /*
227 * If this time the function returns an error code on a subnode
228 * then something is totally wrong so abort.
229 */
230 ret = atmel_process_config_dev(dev, subconfig);
231 if (ret)
232 return ret;
233
234 subnode_count++;
235 }
236
237 /*
238 * If we somehow got here and we do not have any subnodes, abort.
239 */
240 if (!subnode_count)
241 return -EINVAL;
242
243 return 0;
244}
245
Wenyou Yang309686c2016-07-20 17:16:27 +0800246const struct pinctrl_ops atmel_pinctrl_ops = {
247 .set_state = atmel_pinctrl_set_state,
248};
249
250static int atmel_pinctrl_probe(struct udevice *dev)
251{
Simon Glassb75b15b2020-12-03 16:55:23 -0700252 struct atmel_pio4_plat *plat = dev_get_plat(dev);
Sergiu Mogaf7009542022-09-01 17:22:41 +0300253 struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev);
Wenyou Yang309686c2016-07-20 17:16:27 +0800254 fdt_addr_t addr_base;
255
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900256 addr_base = dev_read_addr(dev);
Wenyou Yang309686c2016-07-20 17:16:27 +0800257 if (addr_base == FDT_ADDR_T_NONE)
258 return -EINVAL;
259
260 plat->reg_base = (struct atmel_pio4_port *)addr_base;
Sergiu Mogaf7009542022-09-01 17:22:41 +0300261 plat->slew_rate_support = priv->slew_rate_support;
Wenyou Yang309686c2016-07-20 17:16:27 +0800262
263 return 0;
264}
265
Sergiu Mogaf7009542022-09-01 17:22:41 +0300266static int atmel_pinctrl_bind(struct udevice *dev)
267{
268 struct udevice *g;
269 struct driver *drv;
270 ofnode node = dev_ofnode(dev);
271 struct atmel_pinctrl_data *priv = (struct atmel_pinctrl_data *)dev_get_driver_data(dev);
272
Simon Glass0282a032023-02-05 15:36:17 -0700273 if (!IS_ENABLED(CONFIG_ATMEL_PIO4))
Sergiu Mogaf7009542022-09-01 17:22:41 +0300274 return 0;
275
276 /* Obtain a handle to the GPIO driver */
277 drv = lists_driver_lookup_name("gpio_atmel_pio4");
278 if (!drv)
279 return -ENOENT;
280
281 /*
282 * Bind the GPIO driver to the pinctrl DT node, together
283 * with its corresponding driver_data.
284 */
285 return device_bind_with_driver_data(dev, drv, drv->name,
286 drv->of_match[priv->gdidx].data,
287 node, &g);
288}
289
290static const struct atmel_pinctrl_data atmel_sama5d2_pinctrl_data = {
291 .gdidx = 0,
292};
293
294static const struct atmel_pinctrl_data microchip_sama7g5_pinctrl_data = {
295 .slew_rate_support = 1,
296 .gdidx = 1,
297};
298
Wenyou Yang309686c2016-07-20 17:16:27 +0800299static const struct udevice_id atmel_pinctrl_match[] = {
Sergiu Mogaf7009542022-09-01 17:22:41 +0300300 { .compatible = "atmel,sama5d2-pinctrl",
301 .data = (ulong)&atmel_sama5d2_pinctrl_data, },
Claudiu Beznea153a7932021-01-27 15:00:30 +0200302 { .compatible = "microchip,sama7g5-pinctrl",
Sergiu Mogaf7009542022-09-01 17:22:41 +0300303 .data = (ulong)&microchip_sama7g5_pinctrl_data, },
Wenyou Yang309686c2016-07-20 17:16:27 +0800304 {}
305};
306
307U_BOOT_DRIVER(atmel_pinctrl) = {
308 .name = "pinctrl_atmel_pio4",
309 .id = UCLASS_PINCTRL,
310 .of_match = atmel_pinctrl_match,
Sergiu Mogaf7009542022-09-01 17:22:41 +0300311 .bind = atmel_pinctrl_bind,
Wenyou Yang309686c2016-07-20 17:16:27 +0800312 .probe = atmel_pinctrl_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700313 .plat_auto = sizeof(struct atmel_pio4_plat),
Wenyou Yang309686c2016-07-20 17:16:27 +0800314 .ops = &atmel_pinctrl_ops,
315};