blob: e834dddfd137d4e352db79509ccda940240cca09 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gregory CLEMENTa2490a22017-05-09 13:36:21 +02002/*
3 * U-Boot Marvell 37xx SoC pinctrl driver
4 *
5 * Copyright (C) 2017 Stefan Roese <sr@denx.de>
6 *
7 * This driver is based on the Linux driver version, which is:
8 * Copyright (C) 2017 Marvell
9 * Gregory CLEMENT <gregory.clement@free-electrons.com>
10 *
11 * Additionally parts are derived from the Meson U-Boot pinctrl driver,
12 * which is:
13 * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com>
14 * Based on code from Linux kernel:
15 * Copyright (C) 2016 Endless Mobile, Inc.
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020016 * https://spdx.org/licenses
17 */
18
19#include <common.h>
20#include <config.h>
21#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +020024#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <dm/devres.h>
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +020027#include <dm/lists.h>
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020028#include <dm/pinctrl.h>
29#include <dm/root.h>
30#include <errno.h>
31#include <fdtdec.h>
32#include <regmap.h>
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +020033#include <asm/gpio.h>
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020034#include <asm/system.h>
35#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060037#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060038#include <linux/printk.h>
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020039
40DECLARE_GLOBAL_DATA_PTR;
41
42#define OUTPUT_EN 0x0
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +020043#define INPUT_VAL 0x10
44#define OUTPUT_VAL 0x18
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020045#define OUTPUT_CTL 0x20
46#define SELECTION 0x30
47
48#define IRQ_EN 0x0
49#define IRQ_POL 0x08
50#define IRQ_STATUS 0x10
51#define IRQ_WKUP 0x18
52
Ken Mab28aedb2018-03-26 15:56:01 +080053#define NB_FUNCS 3
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020054#define GPIO_PER_REG 32
55
56/**
57 * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
58 * The pins of a pinmux groups are composed of one or two groups of contiguous
59 * pins.
60 * @name: Name of the pin group, used to lookup the group.
61 * @start_pins: Index of the first pin of the main range of pins belonging to
62 * the group
63 * @npins: Number of pins included in the first range
64 * @reg_mask: Bit mask matching the group in the selection register
65 * @extra_pins: Index of the first pin of the optional second range of pins
66 * belonging to the group
67 * @npins: Number of pins included in the second optional range
68 * @funcs: A list of pinmux functions that can be selected for this group.
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020069 */
70struct armada_37xx_pin_group {
71 const char *name;
72 unsigned int start_pin;
73 unsigned int npins;
74 u32 reg_mask;
75 u32 val[NB_FUNCS];
76 unsigned int extra_pin;
77 unsigned int extra_npins;
78 const char *funcs[NB_FUNCS];
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020079};
80
81struct armada_37xx_pin_data {
82 u8 nr_pins;
83 char *name;
84 struct armada_37xx_pin_group *groups;
85 int ngroups;
86};
87
88struct armada_37xx_pmx_func {
89 const char *name;
90 const char **groups;
91 unsigned int ngroups;
92};
93
94struct armada_37xx_pinctrl {
95 void __iomem *base;
96 const struct armada_37xx_pin_data *data;
97 struct udevice *dev;
98 struct pinctrl_dev *pctl_dev;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +020099 struct armada_37xx_pmx_func *funcs;
100 unsigned int nfuncs;
101};
102
Pali Rohár0738d9d2022-07-25 14:09:01 +0200103#define PIN_GRP_GPIO_0(_name, _start, _nr) \
104 { \
105 .name = _name, \
106 .start_pin = _start, \
107 .npins = _nr, \
108 .reg_mask = 0, \
109 .val = {0}, \
110 .funcs = {"gpio"} \
111 }
112
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200113#define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1) \
114 { \
115 .name = _name, \
116 .start_pin = _start, \
117 .npins = _nr, \
118 .reg_mask = _mask, \
119 .val = {0, _mask}, \
120 .funcs = {_func1, "gpio"} \
121 }
122
123#define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1) \
124 { \
125 .name = _name, \
126 .start_pin = _start, \
127 .npins = _nr, \
128 .reg_mask = _mask, \
129 .val = {_val1, _val2}, \
130 .funcs = {_func1, "gpio"} \
131 }
132
Ken Mab28aedb2018-03-26 15:56:01 +0800133#define PIN_GRP_GPIO_3(_name, _start, _nr, _mask, _v1, _v2, _v3, _f1, _f2) \
134 { \
135 .name = _name, \
136 .start_pin = _start, \
137 .npins = _nr, \
138 .reg_mask = _mask, \
139 .val = {_v1, _v2, _v3}, \
140 .funcs = {_f1, _f2, "gpio"} \
141 }
142
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200143#define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
144 _f1, _f2) \
145 { \
146 .name = _name, \
147 .start_pin = _start, \
148 .npins = _nr, \
149 .reg_mask = _mask, \
150 .val = {_v1, _v2}, \
151 .extra_pin = _start2, \
152 .extra_npins = _nr2, \
153 .funcs = {_f1, _f2} \
154 }
155
156static struct armada_37xx_pin_group armada_37xx_nb_groups[] = {
157 PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
158 PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
159 PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
Marek Behún9eaf20b2021-07-23 19:57:11 +0200160 PIN_GRP_GPIO_3("pwm0", 11, 1, BIT(3) | BIT(20), 0, BIT(20), BIT(3),
161 "pwm", "led"),
Marek Behún54021602022-02-28 15:59:37 +0100162 PIN_GRP_GPIO_3("pwm1", 12, 1, BIT(4) | BIT(21), 0, BIT(21), BIT(4),
Marek Behún9eaf20b2021-07-23 19:57:11 +0200163 "pwm", "led"),
Marek Behún54021602022-02-28 15:59:37 +0100164 PIN_GRP_GPIO_3("pwm2", 13, 1, BIT(5) | BIT(22), 0, BIT(22), BIT(5),
Marek Behún9eaf20b2021-07-23 19:57:11 +0200165 "pwm", "led"),
Marek Behún54021602022-02-28 15:59:37 +0100166 PIN_GRP_GPIO_3("pwm3", 14, 1, BIT(6) | BIT(23), 0, BIT(23), BIT(6),
Marek Behún9eaf20b2021-07-23 19:57:11 +0200167 "pwm", "led"),
Ken Ma3cdd35b2018-03-26 15:56:03 +0800168 PIN_GRP_GPIO("pmic1", 7, 1, BIT(7), "pmic"),
169 PIN_GRP_GPIO("pmic0", 6, 1, BIT(8), "pmic"),
Pali Rohár0738d9d2022-07-25 14:09:01 +0200170 PIN_GRP_GPIO_0("gpio1_5", 5, 1),
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200171 PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
172 PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
173 PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
174 PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
175 PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
176 PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
177 PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
178 PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
Ken Maa12d6372017-06-22 17:13:35 +0800179 PIN_GRP_EXTRA("uart2", 9, 2, BIT(1) | BIT(13) | BIT(14) | BIT(19),
180 BIT(1) | BIT(13) | BIT(14), BIT(1) | BIT(19),
181 18, 2, "gpio", "uart"),
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200182};
183
184static struct armada_37xx_pin_group armada_37xx_sb_groups[] = {
185 PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
186 PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
Pali Rohár0738d9d2022-07-25 14:09:01 +0200187 PIN_GRP_GPIO_0("gpio2_2", 2, 1),
Ken Ma3cdd35b2018-03-26 15:56:03 +0800188 PIN_GRP_GPIO("sdio_sb", 24, 6, BIT(2), "sdio"),
189 PIN_GRP_GPIO("rgmii", 6, 12, BIT(3), "mii"),
190 PIN_GRP_GPIO("smi", 18, 2, BIT(4), "smi"),
Pali Rohár0738d9d2022-07-25 14:09:01 +0200191 PIN_GRP_GPIO("pcie1", 3, 1, BIT(5), "pcie"), /* this actually controls "pcie1_reset" */
192 PIN_GRP_GPIO("pcie1_clkreq", 4, 1, BIT(9), "pcie"),
193 PIN_GRP_GPIO("pcie1_wakeup", 5, 1, BIT(10), "pcie"),
Pali Rohár3a8dccb2022-08-04 12:41:54 +0200194 PIN_GRP_GPIO("ptp", 20, 1, BIT(11), "ptp"),
195 PIN_GRP_GPIO_3("ptp_clk", 21, 1, BIT(6) | BIT(12), 0, BIT(6), BIT(12),
196 "ptp", "mii"),
197 PIN_GRP_GPIO_3("ptp_trig", 22, 1, BIT(7) | BIT(13), 0, BIT(7), BIT(13),
198 "ptp", "mii"),
Ken Mab28aedb2018-03-26 15:56:01 +0800199 PIN_GRP_GPIO_3("mii_col", 23, 1, BIT(8) | BIT(14), 0, BIT(8), BIT(14),
200 "mii", "mii_err"),
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200201};
202
Pali Rohár1497f2e2022-07-25 14:09:00 +0200203static const struct armada_37xx_pin_data armada_37xx_pin_nb = {
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200204 .nr_pins = 36,
205 .name = "GPIO1",
206 .groups = armada_37xx_nb_groups,
207 .ngroups = ARRAY_SIZE(armada_37xx_nb_groups),
208};
209
Pali Rohár1497f2e2022-07-25 14:09:00 +0200210static const struct armada_37xx_pin_data armada_37xx_pin_sb = {
Ken Maa1d81602018-03-26 15:55:59 +0800211 .nr_pins = 30,
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200212 .name = "GPIO2",
213 .groups = armada_37xx_sb_groups,
214 .ngroups = ARRAY_SIZE(armada_37xx_sb_groups),
215};
216
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200217static inline void armada_37xx_update_reg(unsigned int *reg,
Ken Maf72c5292018-03-26 15:56:02 +0800218 unsigned int *offset)
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200219{
220 /* We never have more than 2 registers */
Ken Maf72c5292018-03-26 15:56:02 +0800221 if (*offset >= GPIO_PER_REG) {
222 *offset -= GPIO_PER_REG;
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200223 *reg += sizeof(u32);
224 }
225}
226
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200227static int armada_37xx_get_func_reg(struct armada_37xx_pin_group *grp,
228 const char *func)
229{
230 int f;
231
Ken Mab28aedb2018-03-26 15:56:01 +0800232 for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++)
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200233 if (!strcmp(grp->funcs[f], func))
234 return f;
235
236 return -ENOTSUPP;
237}
238
239static int armada_37xx_pmx_get_groups_count(struct udevice *dev)
240{
241 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
242
Pali Rohár579b5d62022-07-25 14:08:59 +0200243 return info->data->ngroups;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200244}
245
246static const char *armada_37xx_pmx_dummy_name = "_dummy";
247
248static const char *armada_37xx_pmx_get_group_name(struct udevice *dev,
249 unsigned selector)
250{
251 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
252
Pali Rohár579b5d62022-07-25 14:08:59 +0200253 if (!info->data->groups[selector].name)
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200254 return armada_37xx_pmx_dummy_name;
255
Pali Rohár579b5d62022-07-25 14:08:59 +0200256 return info->data->groups[selector].name;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200257}
258
259static int armada_37xx_pmx_get_funcs_count(struct udevice *dev)
260{
261 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
262
263 return info->nfuncs;
264}
265
266static const char *armada_37xx_pmx_get_func_name(struct udevice *dev,
267 unsigned selector)
268{
269 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
270
271 return info->funcs[selector].name;
272}
273
274static int armada_37xx_pmx_set_by_name(struct udevice *dev,
275 const char *name,
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200276 struct armada_37xx_pin_group *grp,
277 bool warn_on_change)
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200278{
279 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
280 unsigned int reg = SELECTION;
281 unsigned int mask = grp->reg_mask;
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200282 int func, val, old_func;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200283
284 dev_dbg(info->dev, "enable function %s group %s\n",
285 name, grp->name);
286
287 func = armada_37xx_get_func_reg(grp, name);
288
289 if (func < 0)
290 return func;
291
292 val = grp->val[func];
293
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200294 if (warn_on_change && val != (readl(info->base + reg) & mask)) {
295 for (old_func = 0; (old_func < NB_FUNCS) && grp->funcs[old_func]; old_func++) {
296 if (grp->val[old_func] == val)
297 break;
298 }
299 dev_warn(info->dev, "Warning: Changing MPPs %u-%u function from %s to %s...\n",
300 grp->start_pin, grp->start_pin + grp->npins - 1,
301 ((old_func < NB_FUNCS && grp->funcs[old_func]) ?
302 grp->funcs[old_func] : "unknown"),
303 name);
304 }
305
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200306 clrsetbits_le32(info->base + reg, mask, val);
307
308 return 0;
309}
310
311static int armada_37xx_pmx_group_set(struct udevice *dev,
312 unsigned group_selector,
313 unsigned func_selector)
314{
315 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
Pali Rohár579b5d62022-07-25 14:08:59 +0200316 struct armada_37xx_pin_group *grp = &info->data->groups[group_selector];
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200317 const char *name = info->funcs[func_selector].name;
318
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200319 return armada_37xx_pmx_set_by_name(dev, name, grp, false);
320}
321
322static int armada_37xx_pmx_gpio_request_enable(struct udevice *dev, unsigned int selector)
323{
324 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
325 int ret = -ENOTSUPP;
326 int n;
327
328 /* Find all groups where is requested selector pin and set each group to gpio function */
329 for (n = 0; n < info->data->ngroups; n++) {
330 struct armada_37xx_pin_group *grp = &info->data->groups[n];
331
332 if ((selector >= grp->start_pin && selector < grp->start_pin + grp->npins) ||
333 (selector >= grp->extra_pin && selector < grp->extra_pin + grp->extra_npins)) {
334 ret = armada_37xx_pmx_set_by_name(dev, "gpio", grp, true);
335 if (ret)
336 return ret;
337 }
338 }
339
340 return ret;
341}
342
343static int armada_37xx_pmx_gpio_disable_free(struct udevice *dev, unsigned int selector)
344{
345 /* nothing to do */
346 return 0;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200347}
348
Pali Rohárf6f0c092022-07-25 14:09:03 +0200349static int armada_37xx_pmx_get_pins_count(struct udevice *dev)
350{
351 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
352
353 return info->data->nr_pins;
354}
355
356static const char *armada_37xx_pmx_get_pin_name(struct udevice *dev, unsigned int selector)
357{
358 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
359 static char buf[sizeof("MPPx_XX")];
360
361 sprintf(buf, "MPP%c_%u", info->data->name[4], selector);
362 return buf;
363}
364
365static int armada_37xx_pmx_get_pin_muxing(struct udevice *dev, unsigned int selector,
366 char *buf, int size)
367{
368 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
369 int n;
370
371 /*
372 * First check if selected pin is in some extra pin group.
373 * Function in extra pin group is active only when it is not gpio.
374 */
375 for (n = 0; n < info->data->ngroups; n++) {
376 struct armada_37xx_pin_group *grp = &info->data->groups[n];
377
378 if (selector >= grp->extra_pin && selector < grp->extra_pin + grp->extra_npins) {
379 unsigned int reg = SELECTION;
380 unsigned int mask = grp->reg_mask;
381 int f, val;
382
383 val = (readl(info->base + reg) & mask);
384
385 for (f = 0; f < NB_FUNCS && grp->funcs[f]; f++) {
386 if (grp->val[f] == val) {
387 if (strcmp(grp->funcs[f], "gpio") != 0) {
388 strlcpy(buf, grp->funcs[f], size);
389 return 0;
390 }
391 break;
392 }
393 }
394 }
395 }
396
397 /* If pin is not active in some extra pin group then check regular groups. */
398 for (n = 0; n < info->data->ngroups; n++) {
399 struct armada_37xx_pin_group *grp = &info->data->groups[n];
400
401 if (selector >= grp->start_pin && selector < grp->start_pin + grp->npins) {
402 unsigned int reg = SELECTION;
403 unsigned int mask = grp->reg_mask;
404 int f, val;
405
406 val = (readl(info->base + reg) & mask);
407
408 for (f = 0; f < NB_FUNCS && grp->funcs[f]; f++) {
409 if (grp->val[f] == val) {
Pali Rohárd2dac1a2022-08-04 12:41:56 +0200410 /*
411 * In more cases group name consist of
412 * function name followed by function
413 * number. So if function name is just
414 * prefix of group name, show group name.
415 */
416 if (strncmp(grp->name, grp->funcs[f],
417 strlen(grp->funcs[f])) == 0)
418 strlcpy(buf, grp->name, size);
419 else
420 strlcpy(buf, grp->funcs[f], size);
Pali Rohárf6f0c092022-07-25 14:09:03 +0200421 return 0;
422 }
423 }
424
425 strlcpy(buf, "unknown", size);
426 return 0;
427 }
428 }
429
430 strlcpy(buf, "unknown", size);
431 return 0;
432}
433
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200434/**
435 * armada_37xx_add_function() - Add a new function to the list
436 * @funcs: array of function to add the new one
437 * @funcsize: size of the remaining space for the function
438 * @name: name of the function to add
439 *
440 * If it is a new function then create it by adding its name else
441 * increment the number of group associated to this function.
442 */
443static int armada_37xx_add_function(struct armada_37xx_pmx_func *funcs,
444 int *funcsize, const char *name)
445{
446 int i = 0;
447
448 if (*funcsize <= 0)
449 return -EOVERFLOW;
450
451 while (funcs->ngroups) {
452 /* function already there */
453 if (strcmp(funcs->name, name) == 0) {
454 funcs->ngroups++;
455
456 return -EEXIST;
457 }
458 funcs++;
459 i++;
460 }
461
462 /* append new unique function */
463 funcs->name = name;
464 funcs->ngroups = 1;
465 (*funcsize)--;
466
467 return 0;
468}
469
470/**
471 * armada_37xx_fill_group() - complete the group array
472 * @info: info driver instance
473 *
474 * Based on the data available from the armada_37xx_pin_group array
475 * completes the last member of the struct for each function: the list
476 * of the groups associated to this function.
477 *
478 */
479static int armada_37xx_fill_group(struct armada_37xx_pinctrl *info)
480{
481 int n, num = 0, funcsize = info->data->nr_pins;
482
Pali Rohár579b5d62022-07-25 14:08:59 +0200483 for (n = 0; n < info->data->ngroups; n++) {
484 struct armada_37xx_pin_group *grp = &info->data->groups[n];
Pali Rohár34eff3f2022-07-25 14:08:58 +0200485 int f;
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200486
Ken Mab28aedb2018-03-26 15:56:01 +0800487 for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++) {
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200488 int ret;
489 /* check for unique functions and count groups */
490 ret = armada_37xx_add_function(info->funcs, &funcsize,
491 grp->funcs[f]);
492 if (ret == -EOVERFLOW)
493 dev_err(info->dev,
494 "More functions than pins(%d)\n",
495 info->data->nr_pins);
496 if (ret < 0)
497 continue;
498 num++;
499 }
500 }
501
502 info->nfuncs = num;
503
504 return 0;
505}
506
507/**
508 * armada_37xx_fill_funcs() - complete the funcs array
509 * @info: info driver instance
510 *
511 * Based on the data available from the armada_37xx_pin_group array
512 * completes the last two member of the struct for each group:
513 * - the list of the pins included in the group
514 * - the list of pinmux functions that can be selected for this group
515 *
516 */
517static int armada_37xx_fill_func(struct armada_37xx_pinctrl *info)
518{
519 struct armada_37xx_pmx_func *funcs = info->funcs;
520 int n;
521
522 for (n = 0; n < info->nfuncs; n++) {
523 const char *name = funcs[n].name;
524 const char **groups;
525 int g;
526
527 funcs[n].groups = devm_kzalloc(info->dev, funcs[n].ngroups *
528 sizeof(*(funcs[n].groups)),
529 GFP_KERNEL);
530 if (!funcs[n].groups)
531 return -ENOMEM;
532
533 groups = funcs[n].groups;
534
Pali Rohár579b5d62022-07-25 14:08:59 +0200535 for (g = 0; g < info->data->ngroups; g++) {
536 struct armada_37xx_pin_group *gp = &info->data->groups[g];
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200537 int f;
538
Ken Mab28aedb2018-03-26 15:56:01 +0800539 for (f = 0; (f < NB_FUNCS) && gp->funcs[f]; f++) {
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200540 if (strcmp(gp->funcs[f], name) == 0) {
541 *groups = gp->name;
542 groups++;
543 }
544 }
545 }
546 }
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200547 return 0;
548}
549
550static int armada_37xx_gpio_get(struct udevice *dev, unsigned int offset)
551{
552 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
553 unsigned int reg = INPUT_VAL;
554 unsigned int val, mask;
555
Ken Maf72c5292018-03-26 15:56:02 +0800556 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200557 mask = BIT(offset);
558
559 val = readl(info->base + reg);
560
561 return (val & mask) != 0;
562}
563
564static int armada_37xx_gpio_set(struct udevice *dev, unsigned int offset,
565 int value)
566{
567 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
568 unsigned int reg = OUTPUT_VAL;
569 unsigned int mask, val;
570
Ken Maf72c5292018-03-26 15:56:02 +0800571 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200572 mask = BIT(offset);
573 val = value ? mask : 0;
574
575 clrsetbits_le32(info->base + reg, mask, val);
576
577 return 0;
578}
579
580static int armada_37xx_gpio_get_direction(struct udevice *dev,
581 unsigned int offset)
582{
583 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
584 unsigned int reg = OUTPUT_EN;
585 unsigned int val, mask;
586
Ken Maf72c5292018-03-26 15:56:02 +0800587 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200588 mask = BIT(offset);
589 val = readl(info->base + reg);
590
591 if (val & mask)
592 return GPIOF_OUTPUT;
593 else
594 return GPIOF_INPUT;
595}
596
597static int armada_37xx_gpio_direction_input(struct udevice *dev,
598 unsigned int offset)
599{
600 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
601 unsigned int reg = OUTPUT_EN;
602 unsigned int mask;
603
Ken Maf72c5292018-03-26 15:56:02 +0800604 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200605 mask = BIT(offset);
606
607 clrbits_le32(info->base + reg, mask);
608
609 return 0;
610}
611
612static int armada_37xx_gpio_direction_output(struct udevice *dev,
613 unsigned int offset, int value)
614{
615 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
616 unsigned int reg = OUTPUT_EN;
617 unsigned int mask;
618
Ken Maf72c5292018-03-26 15:56:02 +0800619 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200620 mask = BIT(offset);
621
622 setbits_le32(info->base + reg, mask);
623
624 /* And set the requested value */
625 return armada_37xx_gpio_set(dev, offset, value);
626}
627
628static int armada_37xx_gpio_probe(struct udevice *dev)
629{
630 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
631 struct gpio_dev_priv *uc_priv;
632
633 uc_priv = dev_get_uclass_priv(dev);
634 uc_priv->bank_name = info->data->name;
635 uc_priv->gpio_count = info->data->nr_pins;
636
637 return 0;
638}
639
640static const struct dm_gpio_ops armada_37xx_gpio_ops = {
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200641 .request = pinctrl_gpio_request,
642 .rfree = pinctrl_gpio_free,
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200643 .set_value = armada_37xx_gpio_set,
644 .get_value = armada_37xx_gpio_get,
645 .get_function = armada_37xx_gpio_get_direction,
646 .direction_input = armada_37xx_gpio_direction_input,
647 .direction_output = armada_37xx_gpio_direction_output,
648};
649
650static struct driver armada_37xx_gpio_driver = {
651 .name = "armada-37xx-gpio",
652 .id = UCLASS_GPIO,
653 .probe = armada_37xx_gpio_probe,
654 .ops = &armada_37xx_gpio_ops,
655};
656
657static int armada_37xx_gpiochip_register(struct udevice *parent,
658 struct armada_37xx_pinctrl *info)
659{
660 const void *blob = gd->fdt_blob;
661 int node = dev_of_offset(parent);
662 struct uclass_driver *drv;
663 struct udevice *dev;
664 int ret = -ENODEV;
665 int subnode;
666 char *name;
667
Simon Glass6996c662020-11-28 17:50:03 -0700668 /* FIXME: Should not need to lookup GPIO uclass */
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200669 drv = lists_uclass_lookup(UCLASS_GPIO);
670 if (!drv) {
671 puts("Cannot find GPIO driver\n");
672 return -ENOENT;
673 }
674
Simon Glass6996c662020-11-28 17:50:03 -0700675 /* FIXME: Use livtree and check the result of device_bind() below */
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200676 fdt_for_each_subnode(subnode, blob, node) {
Ken Mafeb93c62017-06-22 17:13:36 +0800677 if (fdtdec_get_bool(blob, subnode, "gpio-controller")) {
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200678 ret = 0;
679 break;
680 }
681 };
682 if (ret)
683 return ret;
684
685 name = calloc(1, 32);
686 sprintf(name, "armada-37xx-gpio");
687
688 /* Create child device UCLASS_GPIO and bind it */
Simon Glass6996c662020-11-28 17:50:03 -0700689 device_bind(parent, &armada_37xx_gpio_driver, name, NULL,
690 offset_to_ofnode(subnode), &dev);
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200691
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200692 return 0;
693}
694
Pali Rohár1497f2e2022-07-25 14:09:00 +0200695static const struct pinctrl_ops armada_37xx_pinctrl_ops = {
Pali Rohárf6f0c092022-07-25 14:09:03 +0200696 .get_pins_count = armada_37xx_pmx_get_pins_count,
697 .get_pin_name = armada_37xx_pmx_get_pin_name,
698 .get_pin_muxing = armada_37xx_pmx_get_pin_muxing,
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200699 .get_groups_count = armada_37xx_pmx_get_groups_count,
700 .get_group_name = armada_37xx_pmx_get_group_name,
701 .get_functions_count = armada_37xx_pmx_get_funcs_count,
702 .get_function_name = armada_37xx_pmx_get_func_name,
703 .pinmux_group_set = armada_37xx_pmx_group_set,
Pali Rohár9eaab3b2022-07-25 14:09:02 +0200704 .gpio_request_enable = armada_37xx_pmx_gpio_request_enable,
705 .gpio_disable_free = armada_37xx_pmx_gpio_disable_free,
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200706 .set_state = pinctrl_generic_set_state,
707};
708
Pali Rohár1497f2e2022-07-25 14:09:00 +0200709static int armada_37xx_pinctrl_probe(struct udevice *dev)
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200710{
711 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
712 const struct armada_37xx_pin_data *pin_data;
713 int ret;
714
715 info->data = (struct armada_37xx_pin_data *)dev_get_driver_data(dev);
716 pin_data = info->data;
717
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900718 info->base = dev_read_addr_ptr(dev);
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200719 if (!info->base) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900720 pr_err("unable to find regmap\n");
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200721 return -ENODEV;
722 }
723
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200724 /*
725 * we allocate functions for number of pins and hope there are
726 * fewer unique functions than pins available
727 */
728 info->funcs = devm_kzalloc(info->dev, pin_data->nr_pins *
729 sizeof(struct armada_37xx_pmx_func), GFP_KERNEL);
730 if (!info->funcs)
731 return -ENOMEM;
732
733
734 ret = armada_37xx_fill_group(info);
735 if (ret)
736 return ret;
737
738 ret = armada_37xx_fill_func(info);
739 if (ret)
740 return ret;
741
Gregory CLEMENT7ca5e2b2017-05-17 17:05:25 +0200742 ret = armada_37xx_gpiochip_register(dev, info);
743 if (ret)
744 return ret;
745
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200746 return 0;
747}
748
Robert Markoe3f26082023-01-17 15:08:15 +0100749static int armada_37xx_pinctrl_bind(struct udevice *dev)
750{
751 /*
752 * Make sure that the pinctrl driver gets probed after binding
753 * as on A37XX the pinctrl driver is the one that is also
754 * registering the GPIO one during probe, so if its not probed
755 * GPIO-s are not registered as well.
756 */
757 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
758
759 return 0;
760}
761
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200762static const struct udevice_id armada_37xx_pinctrl_of_match[] = {
763 {
764 .compatible = "marvell,armada3710-sb-pinctrl",
765 .data = (ulong)&armada_37xx_pin_sb,
766 },
767 {
768 .compatible = "marvell,armada3710-nb-pinctrl",
769 .data = (ulong)&armada_37xx_pin_nb,
770 },
771 { /* sentinel */ }
772};
773
774U_BOOT_DRIVER(armada_37xx_pinctrl) = {
775 .name = "armada-37xx-pinctrl",
776 .id = UCLASS_PINCTRL,
777 .of_match = of_match_ptr(armada_37xx_pinctrl_of_match),
778 .probe = armada_37xx_pinctrl_probe,
Robert Markoe3f26082023-01-17 15:08:15 +0100779 .bind = armada_37xx_pinctrl_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700780 .priv_auto = sizeof(struct armada_37xx_pinctrl),
Gregory CLEMENTa2490a22017-05-09 13:36:21 +0200781 .ops = &armada_37xx_pinctrl_ops,
782};