blob: 74d915950a6e0bcd7e8ca7d0c4aff41f79a6a72c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gregory CLEMENT08718062017-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 CLEMENT08718062017-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 Glass336d4612020-02-03 07:36:16 -070022#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +020024#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070026#include <dm/devres.h>
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +020027#include <dm/lists.h>
Gregory CLEMENT08718062017-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 CLEMENTd2d92bd2017-05-17 17:05:25 +020033#include <asm/gpio.h>
Gregory CLEMENT08718062017-05-09 13:36:21 +020034#include <asm/system.h>
35#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060037#include <linux/libfdt.h>
Gregory CLEMENT08718062017-05-09 13:36:21 +020038
39DECLARE_GLOBAL_DATA_PTR;
40
41#define OUTPUT_EN 0x0
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +020042#define INPUT_VAL 0x10
43#define OUTPUT_VAL 0x18
Gregory CLEMENT08718062017-05-09 13:36:21 +020044#define OUTPUT_CTL 0x20
45#define SELECTION 0x30
46
47#define IRQ_EN 0x0
48#define IRQ_POL 0x08
49#define IRQ_STATUS 0x10
50#define IRQ_WKUP 0x18
51
Ken Ma23626ca2018-03-26 15:56:01 +080052#define NB_FUNCS 3
Gregory CLEMENT08718062017-05-09 13:36:21 +020053#define GPIO_PER_REG 32
54
55/**
56 * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
57 * The pins of a pinmux groups are composed of one or two groups of contiguous
58 * pins.
59 * @name: Name of the pin group, used to lookup the group.
60 * @start_pins: Index of the first pin of the main range of pins belonging to
61 * the group
62 * @npins: Number of pins included in the first range
63 * @reg_mask: Bit mask matching the group in the selection register
64 * @extra_pins: Index of the first pin of the optional second range of pins
65 * belonging to the group
66 * @npins: Number of pins included in the second optional range
67 * @funcs: A list of pinmux functions that can be selected for this group.
Gregory CLEMENT08718062017-05-09 13:36:21 +020068 */
69struct armada_37xx_pin_group {
70 const char *name;
71 unsigned int start_pin;
72 unsigned int npins;
73 u32 reg_mask;
74 u32 val[NB_FUNCS];
75 unsigned int extra_pin;
76 unsigned int extra_npins;
77 const char *funcs[NB_FUNCS];
Gregory CLEMENT08718062017-05-09 13:36:21 +020078};
79
80struct armada_37xx_pin_data {
81 u8 nr_pins;
82 char *name;
83 struct armada_37xx_pin_group *groups;
84 int ngroups;
85};
86
87struct armada_37xx_pmx_func {
88 const char *name;
89 const char **groups;
90 unsigned int ngroups;
91};
92
93struct armada_37xx_pinctrl {
94 void __iomem *base;
95 const struct armada_37xx_pin_data *data;
96 struct udevice *dev;
97 struct pinctrl_dev *pctl_dev;
Gregory CLEMENT08718062017-05-09 13:36:21 +020098 struct armada_37xx_pmx_func *funcs;
99 unsigned int nfuncs;
100};
101
102#define PIN_GRP(_name, _start, _nr, _mask, _func1, _func2) \
103 { \
104 .name = _name, \
105 .start_pin = _start, \
106 .npins = _nr, \
107 .reg_mask = _mask, \
108 .val = {0, _mask}, \
109 .funcs = {_func1, _func2} \
110 }
111
Pali Rohár3d980712022-07-25 14:09:01 +0200112#define PIN_GRP_GPIO_0(_name, _start, _nr) \
113 { \
114 .name = _name, \
115 .start_pin = _start, \
116 .npins = _nr, \
117 .reg_mask = 0, \
118 .val = {0}, \
119 .funcs = {"gpio"} \
120 }
121
Gregory CLEMENT08718062017-05-09 13:36:21 +0200122#define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1) \
123 { \
124 .name = _name, \
125 .start_pin = _start, \
126 .npins = _nr, \
127 .reg_mask = _mask, \
128 .val = {0, _mask}, \
129 .funcs = {_func1, "gpio"} \
130 }
131
132#define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1) \
133 { \
134 .name = _name, \
135 .start_pin = _start, \
136 .npins = _nr, \
137 .reg_mask = _mask, \
138 .val = {_val1, _val2}, \
139 .funcs = {_func1, "gpio"} \
140 }
141
Ken Ma23626ca2018-03-26 15:56:01 +0800142#define PIN_GRP_GPIO_3(_name, _start, _nr, _mask, _v1, _v2, _v3, _f1, _f2) \
143 { \
144 .name = _name, \
145 .start_pin = _start, \
146 .npins = _nr, \
147 .reg_mask = _mask, \
148 .val = {_v1, _v2, _v3}, \
149 .funcs = {_f1, _f2, "gpio"} \
150 }
151
Gregory CLEMENT08718062017-05-09 13:36:21 +0200152#define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
153 _f1, _f2) \
154 { \
155 .name = _name, \
156 .start_pin = _start, \
157 .npins = _nr, \
158 .reg_mask = _mask, \
159 .val = {_v1, _v2}, \
160 .extra_pin = _start2, \
161 .extra_npins = _nr2, \
162 .funcs = {_f1, _f2} \
163 }
164
165static struct armada_37xx_pin_group armada_37xx_nb_groups[] = {
166 PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
167 PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
168 PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
Marek Behún5534fb42021-07-23 19:57:11 +0200169 PIN_GRP_GPIO_3("pwm0", 11, 1, BIT(3) | BIT(20), 0, BIT(20), BIT(3),
170 "pwm", "led"),
Marek Behún87724d52022-02-28 15:59:37 +0100171 PIN_GRP_GPIO_3("pwm1", 12, 1, BIT(4) | BIT(21), 0, BIT(21), BIT(4),
Marek Behún5534fb42021-07-23 19:57:11 +0200172 "pwm", "led"),
Marek Behún87724d52022-02-28 15:59:37 +0100173 PIN_GRP_GPIO_3("pwm2", 13, 1, BIT(5) | BIT(22), 0, BIT(22), BIT(5),
Marek Behún5534fb42021-07-23 19:57:11 +0200174 "pwm", "led"),
Marek Behún87724d52022-02-28 15:59:37 +0100175 PIN_GRP_GPIO_3("pwm3", 14, 1, BIT(6) | BIT(23), 0, BIT(23), BIT(6),
Marek Behún5534fb42021-07-23 19:57:11 +0200176 "pwm", "led"),
Ken Madc362352018-03-26 15:56:03 +0800177 PIN_GRP_GPIO("pmic1", 7, 1, BIT(7), "pmic"),
178 PIN_GRP_GPIO("pmic0", 6, 1, BIT(8), "pmic"),
Pali Rohár3d980712022-07-25 14:09:01 +0200179 PIN_GRP_GPIO_0("gpio1_5", 5, 1),
Gregory CLEMENT08718062017-05-09 13:36:21 +0200180 PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
181 PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
182 PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
183 PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
184 PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
185 PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
186 PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
187 PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
Ken Mab5a6c942017-06-22 17:13:35 +0800188 PIN_GRP_EXTRA("uart2", 9, 2, BIT(1) | BIT(13) | BIT(14) | BIT(19),
189 BIT(1) | BIT(13) | BIT(14), BIT(1) | BIT(19),
190 18, 2, "gpio", "uart"),
Gregory CLEMENT08718062017-05-09 13:36:21 +0200191};
192
193static struct armada_37xx_pin_group armada_37xx_sb_groups[] = {
194 PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
195 PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
Pali Rohár3d980712022-07-25 14:09:01 +0200196 PIN_GRP_GPIO_0("gpio2_2", 2, 1),
Ken Madc362352018-03-26 15:56:03 +0800197 PIN_GRP_GPIO("sdio_sb", 24, 6, BIT(2), "sdio"),
198 PIN_GRP_GPIO("rgmii", 6, 12, BIT(3), "mii"),
199 PIN_GRP_GPIO("smi", 18, 2, BIT(4), "smi"),
Pali Rohár3d980712022-07-25 14:09:01 +0200200 PIN_GRP_GPIO("pcie1", 3, 1, BIT(5), "pcie"), /* this actually controls "pcie1_reset" */
201 PIN_GRP_GPIO("pcie1_clkreq", 4, 1, BIT(9), "pcie"),
202 PIN_GRP_GPIO("pcie1_wakeup", 5, 1, BIT(10), "pcie"),
Ken Madc362352018-03-26 15:56:03 +0800203 PIN_GRP_GPIO("ptp", 20, 3, BIT(11) | BIT(12) | BIT(13), "ptp"),
Gregory CLEMENT08718062017-05-09 13:36:21 +0200204 PIN_GRP("ptp_clk", 21, 1, BIT(6), "ptp", "mii"),
205 PIN_GRP("ptp_trig", 22, 1, BIT(7), "ptp", "mii"),
Ken Ma23626ca2018-03-26 15:56:01 +0800206 PIN_GRP_GPIO_3("mii_col", 23, 1, BIT(8) | BIT(14), 0, BIT(8), BIT(14),
207 "mii", "mii_err"),
Gregory CLEMENT08718062017-05-09 13:36:21 +0200208};
209
Pali Rohárffab0492022-07-25 14:09:00 +0200210static const struct armada_37xx_pin_data armada_37xx_pin_nb = {
Gregory CLEMENT08718062017-05-09 13:36:21 +0200211 .nr_pins = 36,
212 .name = "GPIO1",
213 .groups = armada_37xx_nb_groups,
214 .ngroups = ARRAY_SIZE(armada_37xx_nb_groups),
215};
216
Pali Rohárffab0492022-07-25 14:09:00 +0200217static const struct armada_37xx_pin_data armada_37xx_pin_sb = {
Ken Ma44ac7472018-03-26 15:55:59 +0800218 .nr_pins = 30,
Gregory CLEMENT08718062017-05-09 13:36:21 +0200219 .name = "GPIO2",
220 .groups = armada_37xx_sb_groups,
221 .ngroups = ARRAY_SIZE(armada_37xx_sb_groups),
222};
223
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200224static inline void armada_37xx_update_reg(unsigned int *reg,
Ken Ma02374482018-03-26 15:56:02 +0800225 unsigned int *offset)
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200226{
227 /* We never have more than 2 registers */
Ken Ma02374482018-03-26 15:56:02 +0800228 if (*offset >= GPIO_PER_REG) {
229 *offset -= GPIO_PER_REG;
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200230 *reg += sizeof(u32);
231 }
232}
233
Gregory CLEMENT08718062017-05-09 13:36:21 +0200234static int armada_37xx_get_func_reg(struct armada_37xx_pin_group *grp,
235 const char *func)
236{
237 int f;
238
Ken Ma23626ca2018-03-26 15:56:01 +0800239 for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++)
Gregory CLEMENT08718062017-05-09 13:36:21 +0200240 if (!strcmp(grp->funcs[f], func))
241 return f;
242
243 return -ENOTSUPP;
244}
245
246static int armada_37xx_pmx_get_groups_count(struct udevice *dev)
247{
248 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
249
Pali Rohár33893e52022-07-25 14:08:59 +0200250 return info->data->ngroups;
Gregory CLEMENT08718062017-05-09 13:36:21 +0200251}
252
253static const char *armada_37xx_pmx_dummy_name = "_dummy";
254
255static const char *armada_37xx_pmx_get_group_name(struct udevice *dev,
256 unsigned selector)
257{
258 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
259
Pali Rohár33893e52022-07-25 14:08:59 +0200260 if (!info->data->groups[selector].name)
Gregory CLEMENT08718062017-05-09 13:36:21 +0200261 return armada_37xx_pmx_dummy_name;
262
Pali Rohár33893e52022-07-25 14:08:59 +0200263 return info->data->groups[selector].name;
Gregory CLEMENT08718062017-05-09 13:36:21 +0200264}
265
266static int armada_37xx_pmx_get_funcs_count(struct udevice *dev)
267{
268 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
269
270 return info->nfuncs;
271}
272
273static const char *armada_37xx_pmx_get_func_name(struct udevice *dev,
274 unsigned selector)
275{
276 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
277
278 return info->funcs[selector].name;
279}
280
281static int armada_37xx_pmx_set_by_name(struct udevice *dev,
282 const char *name,
Pali Rohár140ebcd2022-07-25 14:09:02 +0200283 struct armada_37xx_pin_group *grp,
284 bool warn_on_change)
Gregory CLEMENT08718062017-05-09 13:36:21 +0200285{
286 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
287 unsigned int reg = SELECTION;
288 unsigned int mask = grp->reg_mask;
Pali Rohár140ebcd2022-07-25 14:09:02 +0200289 int func, val, old_func;
Gregory CLEMENT08718062017-05-09 13:36:21 +0200290
291 dev_dbg(info->dev, "enable function %s group %s\n",
292 name, grp->name);
293
294 func = armada_37xx_get_func_reg(grp, name);
295
296 if (func < 0)
297 return func;
298
299 val = grp->val[func];
300
Pali Rohár140ebcd2022-07-25 14:09:02 +0200301 if (warn_on_change && val != (readl(info->base + reg) & mask)) {
302 for (old_func = 0; (old_func < NB_FUNCS) && grp->funcs[old_func]; old_func++) {
303 if (grp->val[old_func] == val)
304 break;
305 }
306 dev_warn(info->dev, "Warning: Changing MPPs %u-%u function from %s to %s...\n",
307 grp->start_pin, grp->start_pin + grp->npins - 1,
308 ((old_func < NB_FUNCS && grp->funcs[old_func]) ?
309 grp->funcs[old_func] : "unknown"),
310 name);
311 }
312
Gregory CLEMENT08718062017-05-09 13:36:21 +0200313 clrsetbits_le32(info->base + reg, mask, val);
314
315 return 0;
316}
317
318static int armada_37xx_pmx_group_set(struct udevice *dev,
319 unsigned group_selector,
320 unsigned func_selector)
321{
322 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
Pali Rohár33893e52022-07-25 14:08:59 +0200323 struct armada_37xx_pin_group *grp = &info->data->groups[group_selector];
Gregory CLEMENT08718062017-05-09 13:36:21 +0200324 const char *name = info->funcs[func_selector].name;
325
Pali Rohár140ebcd2022-07-25 14:09:02 +0200326 return armada_37xx_pmx_set_by_name(dev, name, grp, false);
327}
328
329static int armada_37xx_pmx_gpio_request_enable(struct udevice *dev, unsigned int selector)
330{
331 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
332 int ret = -ENOTSUPP;
333 int n;
334
335 /* Find all groups where is requested selector pin and set each group to gpio function */
336 for (n = 0; n < info->data->ngroups; n++) {
337 struct armada_37xx_pin_group *grp = &info->data->groups[n];
338
339 if ((selector >= grp->start_pin && selector < grp->start_pin + grp->npins) ||
340 (selector >= grp->extra_pin && selector < grp->extra_pin + grp->extra_npins)) {
341 ret = armada_37xx_pmx_set_by_name(dev, "gpio", grp, true);
342 if (ret)
343 return ret;
344 }
345 }
346
347 return ret;
348}
349
350static int armada_37xx_pmx_gpio_disable_free(struct udevice *dev, unsigned int selector)
351{
352 /* nothing to do */
353 return 0;
Gregory CLEMENT08718062017-05-09 13:36:21 +0200354}
355
356/**
357 * armada_37xx_add_function() - Add a new function to the list
358 * @funcs: array of function to add the new one
359 * @funcsize: size of the remaining space for the function
360 * @name: name of the function to add
361 *
362 * If it is a new function then create it by adding its name else
363 * increment the number of group associated to this function.
364 */
365static int armada_37xx_add_function(struct armada_37xx_pmx_func *funcs,
366 int *funcsize, const char *name)
367{
368 int i = 0;
369
370 if (*funcsize <= 0)
371 return -EOVERFLOW;
372
373 while (funcs->ngroups) {
374 /* function already there */
375 if (strcmp(funcs->name, name) == 0) {
376 funcs->ngroups++;
377
378 return -EEXIST;
379 }
380 funcs++;
381 i++;
382 }
383
384 /* append new unique function */
385 funcs->name = name;
386 funcs->ngroups = 1;
387 (*funcsize)--;
388
389 return 0;
390}
391
392/**
393 * armada_37xx_fill_group() - complete the group array
394 * @info: info driver instance
395 *
396 * Based on the data available from the armada_37xx_pin_group array
397 * completes the last member of the struct for each function: the list
398 * of the groups associated to this function.
399 *
400 */
401static int armada_37xx_fill_group(struct armada_37xx_pinctrl *info)
402{
403 int n, num = 0, funcsize = info->data->nr_pins;
404
Pali Rohár33893e52022-07-25 14:08:59 +0200405 for (n = 0; n < info->data->ngroups; n++) {
406 struct armada_37xx_pin_group *grp = &info->data->groups[n];
Pali Rohárbd913a72022-07-25 14:08:58 +0200407 int f;
Gregory CLEMENT08718062017-05-09 13:36:21 +0200408
Ken Ma23626ca2018-03-26 15:56:01 +0800409 for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++) {
Gregory CLEMENT08718062017-05-09 13:36:21 +0200410 int ret;
411 /* check for unique functions and count groups */
412 ret = armada_37xx_add_function(info->funcs, &funcsize,
413 grp->funcs[f]);
414 if (ret == -EOVERFLOW)
415 dev_err(info->dev,
416 "More functions than pins(%d)\n",
417 info->data->nr_pins);
418 if (ret < 0)
419 continue;
420 num++;
421 }
422 }
423
424 info->nfuncs = num;
425
426 return 0;
427}
428
429/**
430 * armada_37xx_fill_funcs() - complete the funcs array
431 * @info: info driver instance
432 *
433 * Based on the data available from the armada_37xx_pin_group array
434 * completes the last two member of the struct for each group:
435 * - the list of the pins included in the group
436 * - the list of pinmux functions that can be selected for this group
437 *
438 */
439static int armada_37xx_fill_func(struct armada_37xx_pinctrl *info)
440{
441 struct armada_37xx_pmx_func *funcs = info->funcs;
442 int n;
443
444 for (n = 0; n < info->nfuncs; n++) {
445 const char *name = funcs[n].name;
446 const char **groups;
447 int g;
448
449 funcs[n].groups = devm_kzalloc(info->dev, funcs[n].ngroups *
450 sizeof(*(funcs[n].groups)),
451 GFP_KERNEL);
452 if (!funcs[n].groups)
453 return -ENOMEM;
454
455 groups = funcs[n].groups;
456
Pali Rohár33893e52022-07-25 14:08:59 +0200457 for (g = 0; g < info->data->ngroups; g++) {
458 struct armada_37xx_pin_group *gp = &info->data->groups[g];
Gregory CLEMENT08718062017-05-09 13:36:21 +0200459 int f;
460
Ken Ma23626ca2018-03-26 15:56:01 +0800461 for (f = 0; (f < NB_FUNCS) && gp->funcs[f]; f++) {
Gregory CLEMENT08718062017-05-09 13:36:21 +0200462 if (strcmp(gp->funcs[f], name) == 0) {
463 *groups = gp->name;
464 groups++;
465 }
466 }
467 }
468 }
469 return 0;
470}
471
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200472static int armada_37xx_gpio_get(struct udevice *dev, unsigned int offset)
473{
474 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
475 unsigned int reg = INPUT_VAL;
476 unsigned int val, mask;
477
Ken Ma02374482018-03-26 15:56:02 +0800478 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200479 mask = BIT(offset);
480
481 val = readl(info->base + reg);
482
483 return (val & mask) != 0;
484}
485
486static int armada_37xx_gpio_set(struct udevice *dev, unsigned int offset,
487 int value)
488{
489 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
490 unsigned int reg = OUTPUT_VAL;
491 unsigned int mask, val;
492
Ken Ma02374482018-03-26 15:56:02 +0800493 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200494 mask = BIT(offset);
495 val = value ? mask : 0;
496
497 clrsetbits_le32(info->base + reg, mask, val);
498
499 return 0;
500}
501
502static int armada_37xx_gpio_get_direction(struct udevice *dev,
503 unsigned int offset)
504{
505 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
506 unsigned int reg = OUTPUT_EN;
507 unsigned int val, mask;
508
Ken Ma02374482018-03-26 15:56:02 +0800509 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200510 mask = BIT(offset);
511 val = readl(info->base + reg);
512
513 if (val & mask)
514 return GPIOF_OUTPUT;
515 else
516 return GPIOF_INPUT;
517}
518
519static int armada_37xx_gpio_direction_input(struct udevice *dev,
520 unsigned int offset)
521{
522 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
523 unsigned int reg = OUTPUT_EN;
524 unsigned int mask;
525
Ken Ma02374482018-03-26 15:56:02 +0800526 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200527 mask = BIT(offset);
528
529 clrbits_le32(info->base + reg, mask);
530
531 return 0;
532}
533
534static int armada_37xx_gpio_direction_output(struct udevice *dev,
535 unsigned int offset, int value)
536{
537 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
538 unsigned int reg = OUTPUT_EN;
539 unsigned int mask;
540
Ken Ma02374482018-03-26 15:56:02 +0800541 armada_37xx_update_reg(&reg, &offset);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200542 mask = BIT(offset);
543
544 setbits_le32(info->base + reg, mask);
545
546 /* And set the requested value */
547 return armada_37xx_gpio_set(dev, offset, value);
548}
549
550static int armada_37xx_gpio_probe(struct udevice *dev)
551{
552 struct armada_37xx_pinctrl *info = dev_get_priv(dev->parent);
553 struct gpio_dev_priv *uc_priv;
554
555 uc_priv = dev_get_uclass_priv(dev);
556 uc_priv->bank_name = info->data->name;
557 uc_priv->gpio_count = info->data->nr_pins;
558
559 return 0;
560}
561
562static const struct dm_gpio_ops armada_37xx_gpio_ops = {
Pali Rohár140ebcd2022-07-25 14:09:02 +0200563 .request = pinctrl_gpio_request,
564 .rfree = pinctrl_gpio_free,
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200565 .set_value = armada_37xx_gpio_set,
566 .get_value = armada_37xx_gpio_get,
567 .get_function = armada_37xx_gpio_get_direction,
568 .direction_input = armada_37xx_gpio_direction_input,
569 .direction_output = armada_37xx_gpio_direction_output,
570};
571
572static struct driver armada_37xx_gpio_driver = {
573 .name = "armada-37xx-gpio",
574 .id = UCLASS_GPIO,
575 .probe = armada_37xx_gpio_probe,
576 .ops = &armada_37xx_gpio_ops,
577};
578
579static int armada_37xx_gpiochip_register(struct udevice *parent,
580 struct armada_37xx_pinctrl *info)
581{
582 const void *blob = gd->fdt_blob;
583 int node = dev_of_offset(parent);
584 struct uclass_driver *drv;
585 struct udevice *dev;
586 int ret = -ENODEV;
587 int subnode;
588 char *name;
589
Simon Glassa2703ce2020-11-28 17:50:03 -0700590 /* FIXME: Should not need to lookup GPIO uclass */
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200591 drv = lists_uclass_lookup(UCLASS_GPIO);
592 if (!drv) {
593 puts("Cannot find GPIO driver\n");
594 return -ENOENT;
595 }
596
Simon Glassa2703ce2020-11-28 17:50:03 -0700597 /* FIXME: Use livtree and check the result of device_bind() below */
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200598 fdt_for_each_subnode(subnode, blob, node) {
Ken Maae118b62017-06-22 17:13:36 +0800599 if (fdtdec_get_bool(blob, subnode, "gpio-controller")) {
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200600 ret = 0;
601 break;
602 }
603 };
604 if (ret)
605 return ret;
606
607 name = calloc(1, 32);
608 sprintf(name, "armada-37xx-gpio");
609
610 /* Create child device UCLASS_GPIO and bind it */
Simon Glassa2703ce2020-11-28 17:50:03 -0700611 device_bind(parent, &armada_37xx_gpio_driver, name, NULL,
612 offset_to_ofnode(subnode), &dev);
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200613
614 return 0;
615}
616
Pali Rohárffab0492022-07-25 14:09:00 +0200617static const struct pinctrl_ops armada_37xx_pinctrl_ops = {
Gregory CLEMENT08718062017-05-09 13:36:21 +0200618 .get_groups_count = armada_37xx_pmx_get_groups_count,
619 .get_group_name = armada_37xx_pmx_get_group_name,
620 .get_functions_count = armada_37xx_pmx_get_funcs_count,
621 .get_function_name = armada_37xx_pmx_get_func_name,
622 .pinmux_group_set = armada_37xx_pmx_group_set,
Pali Rohár140ebcd2022-07-25 14:09:02 +0200623 .gpio_request_enable = armada_37xx_pmx_gpio_request_enable,
624 .gpio_disable_free = armada_37xx_pmx_gpio_disable_free,
Gregory CLEMENT08718062017-05-09 13:36:21 +0200625 .set_state = pinctrl_generic_set_state,
626};
627
Pali Rohárffab0492022-07-25 14:09:00 +0200628static int armada_37xx_pinctrl_probe(struct udevice *dev)
Gregory CLEMENT08718062017-05-09 13:36:21 +0200629{
630 struct armada_37xx_pinctrl *info = dev_get_priv(dev);
631 const struct armada_37xx_pin_data *pin_data;
632 int ret;
633
634 info->data = (struct armada_37xx_pin_data *)dev_get_driver_data(dev);
635 pin_data = info->data;
636
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900637 info->base = dev_read_addr_ptr(dev);
Gregory CLEMENT08718062017-05-09 13:36:21 +0200638 if (!info->base) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900639 pr_err("unable to find regmap\n");
Gregory CLEMENT08718062017-05-09 13:36:21 +0200640 return -ENODEV;
641 }
642
Gregory CLEMENT08718062017-05-09 13:36:21 +0200643 /*
644 * we allocate functions for number of pins and hope there are
645 * fewer unique functions than pins available
646 */
647 info->funcs = devm_kzalloc(info->dev, pin_data->nr_pins *
648 sizeof(struct armada_37xx_pmx_func), GFP_KERNEL);
649 if (!info->funcs)
650 return -ENOMEM;
651
652
653 ret = armada_37xx_fill_group(info);
654 if (ret)
655 return ret;
656
657 ret = armada_37xx_fill_func(info);
658 if (ret)
659 return ret;
660
Gregory CLEMENTd2d92bd2017-05-17 17:05:25 +0200661 ret = armada_37xx_gpiochip_register(dev, info);
662 if (ret)
663 return ret;
664
Gregory CLEMENT08718062017-05-09 13:36:21 +0200665 return 0;
666}
667
668static const struct udevice_id armada_37xx_pinctrl_of_match[] = {
669 {
670 .compatible = "marvell,armada3710-sb-pinctrl",
671 .data = (ulong)&armada_37xx_pin_sb,
672 },
673 {
674 .compatible = "marvell,armada3710-nb-pinctrl",
675 .data = (ulong)&armada_37xx_pin_nb,
676 },
677 { /* sentinel */ }
678};
679
680U_BOOT_DRIVER(armada_37xx_pinctrl) = {
681 .name = "armada-37xx-pinctrl",
682 .id = UCLASS_PINCTRL,
683 .of_match = of_match_ptr(armada_37xx_pinctrl_of_match),
684 .probe = armada_37xx_pinctrl_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700685 .priv_auto = sizeof(struct armada_37xx_pinctrl),
Gregory CLEMENT08718062017-05-09 13:36:21 +0200686 .ops = &armada_37xx_pinctrl_ops,
687};