1 // SPDX-License-Identifier: GPL-2.0
3 * Pinctrl for Cirrus Logic Madera codecs
5 * Copyright (C) 2016-2018 Cirrus Logic
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2.
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/pinctrl/machine.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/mfd/madera/core.h>
24 #include <linux/mfd/madera/registers.h>
26 #include "../pinctrl-utils.h"
28 #include "pinctrl-madera.h"
31 * Use pin GPIO names for consistency
32 * NOTE: IDs are zero-indexed for coding convenience
34 static const struct pinctrl_pin_desc madera_pins
[] = {
35 PINCTRL_PIN(0, "gpio1"),
36 PINCTRL_PIN(1, "gpio2"),
37 PINCTRL_PIN(2, "gpio3"),
38 PINCTRL_PIN(3, "gpio4"),
39 PINCTRL_PIN(4, "gpio5"),
40 PINCTRL_PIN(5, "gpio6"),
41 PINCTRL_PIN(6, "gpio7"),
42 PINCTRL_PIN(7, "gpio8"),
43 PINCTRL_PIN(8, "gpio9"),
44 PINCTRL_PIN(9, "gpio10"),
45 PINCTRL_PIN(10, "gpio11"),
46 PINCTRL_PIN(11, "gpio12"),
47 PINCTRL_PIN(12, "gpio13"),
48 PINCTRL_PIN(13, "gpio14"),
49 PINCTRL_PIN(14, "gpio15"),
50 PINCTRL_PIN(15, "gpio16"),
51 PINCTRL_PIN(16, "gpio17"),
52 PINCTRL_PIN(17, "gpio18"),
53 PINCTRL_PIN(18, "gpio19"),
54 PINCTRL_PIN(19, "gpio20"),
55 PINCTRL_PIN(20, "gpio21"),
56 PINCTRL_PIN(21, "gpio22"),
57 PINCTRL_PIN(22, "gpio23"),
58 PINCTRL_PIN(23, "gpio24"),
59 PINCTRL_PIN(24, "gpio25"),
60 PINCTRL_PIN(25, "gpio26"),
61 PINCTRL_PIN(26, "gpio27"),
62 PINCTRL_PIN(27, "gpio28"),
63 PINCTRL_PIN(28, "gpio29"),
64 PINCTRL_PIN(29, "gpio30"),
65 PINCTRL_PIN(30, "gpio31"),
66 PINCTRL_PIN(31, "gpio32"),
67 PINCTRL_PIN(32, "gpio33"),
68 PINCTRL_PIN(33, "gpio34"),
69 PINCTRL_PIN(34, "gpio35"),
70 PINCTRL_PIN(35, "gpio36"),
71 PINCTRL_PIN(36, "gpio37"),
72 PINCTRL_PIN(37, "gpio38"),
73 PINCTRL_PIN(38, "gpio39"),
74 PINCTRL_PIN(39, "gpio40"),
78 * All single-pin functions can be mapped to any GPIO, however pinmux applies
79 * functions to pin groups and only those groups declared as supporting that
80 * function. To make this work we must put each pin in its own dummy group so
81 * that the functions can be described as applying to all pins.
82 * Since these do not correspond to anything in the actual hardware - they are
83 * merely an adaptation to pinctrl's view of the world - we use the same name
84 * as the pin to avoid confusion when comparing with datasheet instructions
86 static const char * const madera_pin_single_group_names
[] = {
87 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
88 "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
89 "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
90 "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
91 "gpio29", "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35",
92 "gpio36", "gpio37", "gpio38", "gpio39", "gpio40",
95 /* set of pin numbers for single-pin groups, zero-indexed */
96 static const unsigned int madera_pin_single_group_pins
[] = {
98 7, 8, 9, 10, 11, 12, 13,
99 14, 15, 16, 17, 18, 19, 20,
100 21, 22, 23, 24, 25, 26, 27,
101 28, 29, 30, 31, 32, 33, 34,
105 static const char * const madera_aif1_group_names
[] = { "aif1" };
106 static const char * const madera_aif2_group_names
[] = { "aif2" };
107 static const char * const madera_aif3_group_names
[] = { "aif3" };
108 static const char * const madera_aif4_group_names
[] = { "aif4" };
109 static const char * const madera_mif1_group_names
[] = { "mif1" };
110 static const char * const madera_mif2_group_names
[] = { "mif2" };
111 static const char * const madera_mif3_group_names
[] = { "mif3" };
112 static const char * const madera_dmic3_group_names
[] = { "dmic3" };
113 static const char * const madera_dmic4_group_names
[] = { "dmic4" };
114 static const char * const madera_dmic5_group_names
[] = { "dmic5" };
115 static const char * const madera_dmic6_group_names
[] = { "dmic6" };
116 static const char * const madera_spk1_group_names
[] = { "pdmspk1" };
117 static const char * const madera_spk2_group_names
[] = { "pdmspk2" };
120 * alt-functions always apply to a single pin group, other functions always
123 static const struct {
125 const char * const *group_names
;
127 } madera_mux_funcs
[] = {
130 .group_names
= madera_aif1_group_names
,
135 .group_names
= madera_aif2_group_names
,
140 .group_names
= madera_aif3_group_names
,
145 .group_names
= madera_aif4_group_names
,
150 .group_names
= madera_mif1_group_names
,
155 .group_names
= madera_mif2_group_names
,
160 .group_names
= madera_mif3_group_names
,
165 .group_names
= madera_dmic3_group_names
,
170 .group_names
= madera_dmic4_group_names
,
175 .group_names
= madera_dmic5_group_names
,
180 .group_names
= madera_dmic6_group_names
,
185 .group_names
= madera_spk1_group_names
,
190 .group_names
= madera_spk2_group_names
,
195 .group_names
= madera_pin_single_group_names
,
200 .group_names
= madera_pin_single_group_names
,
205 .group_names
= madera_pin_single_group_names
,
210 .group_names
= madera_pin_single_group_names
,
215 .group_names
= madera_pin_single_group_names
,
220 .group_names
= madera_pin_single_group_names
,
225 .group_names
= madera_pin_single_group_names
,
230 .group_names
= madera_pin_single_group_names
,
235 .group_names
= madera_pin_single_group_names
,
240 .group_names
= madera_pin_single_group_names
,
245 .group_names
= madera_pin_single_group_names
,
249 .name
= "fllao-lock",
250 .group_names
= madera_pin_single_group_names
,
255 .group_names
= madera_pin_single_group_names
,
259 .name
= "opclk-async",
260 .group_names
= madera_pin_single_group_names
,
265 .group_names
= madera_pin_single_group_names
,
270 .group_names
= madera_pin_single_group_names
,
275 .group_names
= madera_pin_single_group_names
,
279 .name
= "asrc1-in1-lock",
280 .group_names
= madera_pin_single_group_names
,
284 .name
= "asrc1-in2-lock",
285 .group_names
= madera_pin_single_group_names
,
289 .name
= "asrc2-in1-lock",
290 .group_names
= madera_pin_single_group_names
,
294 .name
= "asrc2-in2-lock",
295 .group_names
= madera_pin_single_group_names
,
299 .name
= "spkl-short-circuit",
300 .group_names
= madera_pin_single_group_names
,
304 .name
= "spkr-short-circuit",
305 .group_names
= madera_pin_single_group_names
,
309 .name
= "spk-shutdown",
310 .group_names
= madera_pin_single_group_names
,
314 .name
= "spk-overheat-shutdown",
315 .group_names
= madera_pin_single_group_names
,
319 .name
= "spk-overheat-warn",
320 .group_names
= madera_pin_single_group_names
,
324 .name
= "timer1-sts",
325 .group_names
= madera_pin_single_group_names
,
329 .name
= "timer2-sts",
330 .group_names
= madera_pin_single_group_names
,
334 .name
= "timer3-sts",
335 .group_names
= madera_pin_single_group_names
,
339 .name
= "timer4-sts",
340 .group_names
= madera_pin_single_group_names
,
344 .name
= "timer5-sts",
345 .group_names
= madera_pin_single_group_names
,
349 .name
= "timer6-sts",
350 .group_names
= madera_pin_single_group_names
,
354 .name
= "timer7-sts",
355 .group_names
= madera_pin_single_group_names
,
359 .name
= "timer8-sts",
360 .group_names
= madera_pin_single_group_names
,
364 .name
= "log1-fifo-ne",
365 .group_names
= madera_pin_single_group_names
,
369 .name
= "log2-fifo-ne",
370 .group_names
= madera_pin_single_group_names
,
374 .name
= "log3-fifo-ne",
375 .group_names
= madera_pin_single_group_names
,
379 .name
= "log4-fifo-ne",
380 .group_names
= madera_pin_single_group_names
,
384 .name
= "log5-fifo-ne",
385 .group_names
= madera_pin_single_group_names
,
389 .name
= "log6-fifo-ne",
390 .group_names
= madera_pin_single_group_names
,
394 .name
= "log7-fifo-ne",
395 .group_names
= madera_pin_single_group_names
,
399 .name
= "log8-fifo-ne",
400 .group_names
= madera_pin_single_group_names
,
405 static u16
madera_pin_make_drv_str(struct madera_pin_private
*priv
,
406 unsigned int milliamps
)
412 return 2 << MADERA_GP1_DRV_STR_SHIFT
;
417 dev_warn(priv
->dev
, "%u mA not a valid drive strength", milliamps
);
422 static unsigned int madera_pin_unmake_drv_str(struct madera_pin_private
*priv
,
425 regval
= (regval
& MADERA_GP1_DRV_STR_MASK
) >> MADERA_GP1_DRV_STR_SHIFT
;
437 static int madera_get_groups_count(struct pinctrl_dev
*pctldev
)
439 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
441 /* Number of alt function groups plus number of single-pin groups */
442 return priv
->chip
->n_pin_groups
+ priv
->chip
->n_pins
;
445 static const char *madera_get_group_name(struct pinctrl_dev
*pctldev
,
446 unsigned int selector
)
448 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
450 if (selector
< priv
->chip
->n_pin_groups
)
451 return priv
->chip
->pin_groups
[selector
].name
;
453 selector
-= priv
->chip
->n_pin_groups
;
454 return madera_pin_single_group_names
[selector
];
457 static int madera_get_group_pins(struct pinctrl_dev
*pctldev
,
458 unsigned int selector
,
459 const unsigned int **pins
,
460 unsigned int *num_pins
)
462 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
464 if (selector
< priv
->chip
->n_pin_groups
) {
465 *pins
= priv
->chip
->pin_groups
[selector
].pins
;
466 *num_pins
= priv
->chip
->pin_groups
[selector
].n_pins
;
468 /* return the dummy group for a single pin */
469 selector
-= priv
->chip
->n_pin_groups
;
470 *pins
= &madera_pin_single_group_pins
[selector
];
476 static void madera_pin_dbg_show_fn(struct madera_pin_private
*priv
,
478 unsigned int pin
, unsigned int fn
)
480 const struct madera_pin_chip
*chip
= priv
->chip
;
484 for (i
= 0; i
< ARRAY_SIZE(madera_mux_funcs
); ++i
) {
485 if (madera_mux_funcs
[i
].func
== fn
) {
486 seq_printf(s
, " FN=%s",
487 madera_mux_funcs
[i
].name
);
491 return; /* ignore unknown function values */
495 for (i
= 0; i
< chip
->n_pin_groups
; ++i
) {
496 for (g_pin
= 0; g_pin
< chip
->pin_groups
[i
].n_pins
; ++g_pin
) {
497 if (chip
->pin_groups
[i
].pins
[g_pin
] == pin
) {
498 seq_printf(s
, " FN=%s",
499 chip
->pin_groups
[i
].name
);
506 static void __maybe_unused
madera_pin_dbg_show(struct pinctrl_dev
*pctldev
,
510 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
511 unsigned int conf
[2];
512 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
516 ret
= regmap_read(priv
->madera
->regmap
, reg
, &conf
[0]);
520 ret
= regmap_read(priv
->madera
->regmap
, reg
+ 1, &conf
[1]);
524 seq_printf(s
, "%04x:%04x", conf
[0], conf
[1]);
526 fn
= (conf
[0] & MADERA_GP1_FN_MASK
) >> MADERA_GP1_FN_SHIFT
;
527 madera_pin_dbg_show_fn(priv
, s
, pin
, fn
);
529 /* State of direction bit is only relevant if function==1 */
531 if (conf
[1] & MADERA_GP1_DIR_MASK
)
537 if (conf
[1] & MADERA_GP1_PU_MASK
)
540 if (conf
[1] & MADERA_GP1_PD_MASK
)
543 if (conf
[0] & MADERA_GP1_DB_MASK
)
546 if (conf
[0] & MADERA_GP1_OP_CFG_MASK
)
549 seq_puts(s
, " CMOS");
551 seq_printf(s
, " DRV=%umA", madera_pin_unmake_drv_str(priv
, conf
[1]));
553 if (conf
[0] & MADERA_GP1_IP_CFG_MASK
)
554 seq_puts(s
, " SCHMITT");
558 static const struct pinctrl_ops madera_pin_group_ops
= {
559 .get_groups_count
= madera_get_groups_count
,
560 .get_group_name
= madera_get_group_name
,
561 .get_group_pins
= madera_get_group_pins
,
562 #if IS_ENABLED(CONFIG_OF)
563 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
564 .dt_free_map
= pinctrl_utils_free_map
,
566 #if IS_ENABLED(CONFIG_DEBUG_FS)
567 .pin_dbg_show
= madera_pin_dbg_show
,
571 static int madera_mux_get_funcs_count(struct pinctrl_dev
*pctldev
)
573 return ARRAY_SIZE(madera_mux_funcs
);
576 static const char *madera_mux_get_func_name(struct pinctrl_dev
*pctldev
,
577 unsigned int selector
)
579 return madera_mux_funcs
[selector
].name
;
582 static int madera_mux_get_groups(struct pinctrl_dev
*pctldev
,
583 unsigned int selector
,
584 const char * const **groups
,
585 unsigned int * const num_groups
)
587 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
589 *groups
= madera_mux_funcs
[selector
].group_names
;
591 if (madera_mux_funcs
[selector
].func
== 0) {
592 /* alt func always maps to a single group */
595 /* other funcs map to all available gpio pins */
596 *num_groups
= priv
->chip
->n_pins
;
602 static int madera_mux_set_mux(struct pinctrl_dev
*pctldev
,
603 unsigned int selector
,
606 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
607 struct madera
*madera
= priv
->madera
;
608 const struct madera_pin_groups
*pin_group
= priv
->chip
->pin_groups
;
609 unsigned int n_chip_groups
= priv
->chip
->n_pin_groups
;
610 const char *func_name
= madera_mux_funcs
[selector
].name
;
614 dev_dbg(priv
->dev
, "%s selecting %u (%s) for group %u (%s)\n",
615 __func__
, selector
, func_name
, group
,
616 madera_get_group_name(pctldev
, group
));
618 if (madera_mux_funcs
[selector
].func
== 0) {
619 /* alt func pin assignments are codec-specific */
620 for (i
= 0; i
< n_chip_groups
; ++i
) {
621 if (strcmp(func_name
, pin_group
->name
) == 0)
627 if (i
== n_chip_groups
)
630 for (i
= 0; i
< pin_group
->n_pins
; ++i
) {
631 reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin_group
->pins
[i
]);
633 dev_dbg(priv
->dev
, "%s setting 0x%x func bits to 0\n",
636 ret
= regmap_update_bits(madera
->regmap
, reg
,
637 MADERA_GP1_FN_MASK
, 0);
644 * for other funcs the group will be the gpio number and will
645 * be offset by the number of chip-specific functions at the
646 * start of the group list
648 group
-= n_chip_groups
;
649 reg
= MADERA_GPIO1_CTRL_1
+ (2 * group
);
651 dev_dbg(priv
->dev
, "%s setting 0x%x func bits to 0x%x\n",
652 __func__
, reg
, madera_mux_funcs
[selector
].func
);
654 ret
= regmap_update_bits(madera
->regmap
,
657 madera_mux_funcs
[selector
].func
);
661 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
666 static int madera_gpio_set_direction(struct pinctrl_dev
*pctldev
,
667 struct pinctrl_gpio_range
*range
,
671 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
672 struct madera
*madera
= priv
->madera
;
673 unsigned int reg
= MADERA_GPIO1_CTRL_2
+ (2 * offset
);
678 val
= MADERA_GP1_DIR
;
682 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_DIR_MASK
, val
);
684 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
689 static int madera_gpio_request_enable(struct pinctrl_dev
*pctldev
,
690 struct pinctrl_gpio_range
*range
,
693 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
694 struct madera
*madera
= priv
->madera
;
695 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * offset
);
698 /* put the pin into GPIO mode */
699 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_FN_MASK
, 1);
701 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
706 static void madera_gpio_disable_free(struct pinctrl_dev
*pctldev
,
707 struct pinctrl_gpio_range
*range
,
710 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
711 struct madera
*madera
= priv
->madera
;
712 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * offset
);
715 /* disable GPIO by setting to GPIO IN */
716 madera_gpio_set_direction(pctldev
, range
, offset
, true);
718 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_FN_MASK
, 1);
720 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
723 static const struct pinmux_ops madera_pin_mux_ops
= {
724 .get_functions_count
= madera_mux_get_funcs_count
,
725 .get_function_name
= madera_mux_get_func_name
,
726 .get_function_groups
= madera_mux_get_groups
,
727 .set_mux
= madera_mux_set_mux
,
728 .gpio_request_enable
= madera_gpio_request_enable
,
729 .gpio_disable_free
= madera_gpio_disable_free
,
730 .gpio_set_direction
= madera_gpio_set_direction
,
731 .strict
= true, /* GPIO and other functions are exclusive */
734 static int madera_pin_conf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
735 unsigned long *config
)
737 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
738 unsigned int param
= pinconf_to_config_param(*config
);
739 unsigned int result
= 0;
740 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
741 unsigned int conf
[2];
744 ret
= regmap_read(priv
->madera
->regmap
, reg
, &conf
[0]);
746 ret
= regmap_read(priv
->madera
->regmap
, reg
+ 1, &conf
[1]);
749 dev_err(priv
->dev
, "Failed to read GP%d conf (%d)\n",
755 case PIN_CONFIG_BIAS_BUS_HOLD
:
756 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
757 if (conf
[1] == (MADERA_GP1_PU
| MADERA_GP1_PD
))
760 case PIN_CONFIG_BIAS_DISABLE
:
761 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
765 case PIN_CONFIG_BIAS_PULL_DOWN
:
766 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
767 if (conf
[1] == MADERA_GP1_PD_MASK
)
770 case PIN_CONFIG_BIAS_PULL_UP
:
771 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
772 if (conf
[1] == MADERA_GP1_PU_MASK
)
775 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
776 if (conf
[0] & MADERA_GP1_OP_CFG_MASK
)
779 case PIN_CONFIG_DRIVE_PUSH_PULL
:
780 if (!(conf
[0] & MADERA_GP1_OP_CFG_MASK
))
783 case PIN_CONFIG_DRIVE_STRENGTH
:
784 result
= madera_pin_unmake_drv_str(priv
, conf
[1]);
786 case PIN_CONFIG_INPUT_DEBOUNCE
:
787 if (conf
[0] & MADERA_GP1_DB_MASK
)
790 case PIN_CONFIG_INPUT_ENABLE
:
791 if (conf
[0] & MADERA_GP1_DIR_MASK
)
794 case PIN_CONFIG_INPUT_SCHMITT
:
795 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
796 if (conf
[0] & MADERA_GP1_IP_CFG_MASK
)
799 case PIN_CONFIG_OUTPUT
:
800 if ((conf
[1] & MADERA_GP1_DIR_MASK
) &&
801 (conf
[0] & MADERA_GP1_LVL_MASK
))
808 *config
= pinconf_to_config_packed(param
, result
);
813 static int madera_pin_conf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
814 unsigned long *configs
, unsigned int num_configs
)
816 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
817 u16 conf
[2] = {0, 0};
818 u16 mask
[2] = {0, 0};
819 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
823 while (num_configs
) {
824 dev_dbg(priv
->dev
, "%s config 0x%lx\n", __func__
, *configs
);
826 switch (pinconf_to_config_param(*configs
)) {
827 case PIN_CONFIG_BIAS_BUS_HOLD
:
828 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
829 conf
[1] |= MADERA_GP1_PU
| MADERA_GP1_PD
;
831 case PIN_CONFIG_BIAS_DISABLE
:
832 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
833 conf
[1] &= ~(MADERA_GP1_PU
| MADERA_GP1_PD
);
835 case PIN_CONFIG_BIAS_PULL_DOWN
:
836 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
837 conf
[1] |= MADERA_GP1_PD
;
838 conf
[1] &= ~MADERA_GP1_PU
;
840 case PIN_CONFIG_BIAS_PULL_UP
:
841 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
842 conf
[1] |= MADERA_GP1_PU
;
843 conf
[1] &= ~MADERA_GP1_PD
;
845 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
846 mask
[0] |= MADERA_GP1_OP_CFG_MASK
;
847 conf
[0] |= MADERA_GP1_OP_CFG
;
849 case PIN_CONFIG_DRIVE_PUSH_PULL
:
850 mask
[0] |= MADERA_GP1_OP_CFG_MASK
;
851 conf
[0] &= ~MADERA_GP1_OP_CFG
;
853 case PIN_CONFIG_DRIVE_STRENGTH
:
854 val
= pinconf_to_config_argument(*configs
);
855 mask
[1] |= MADERA_GP1_DRV_STR_MASK
;
856 conf
[1] &= ~MADERA_GP1_DRV_STR_MASK
;
857 conf
[1] |= madera_pin_make_drv_str(priv
, val
);
859 case PIN_CONFIG_INPUT_DEBOUNCE
:
860 mask
[0] |= MADERA_GP1_DB_MASK
;
863 * we can't configure debounce time per-pin so value
866 val
= pinconf_to_config_argument(*configs
);
868 conf
[0] |= MADERA_GP1_DB
;
870 conf
[0] &= ~MADERA_GP1_DB
;
872 case PIN_CONFIG_INPUT_ENABLE
:
873 val
= pinconf_to_config_argument(*configs
);
874 mask
[1] |= MADERA_GP1_DIR_MASK
;
876 conf
[1] |= MADERA_GP1_DIR
;
878 conf
[1] &= ~MADERA_GP1_DIR
;
880 case PIN_CONFIG_INPUT_SCHMITT
:
881 val
= pinconf_to_config_argument(*configs
);
882 mask
[0] |= MADERA_GP1_IP_CFG
;
884 conf
[0] |= MADERA_GP1_IP_CFG
;
886 conf
[0] &= ~MADERA_GP1_IP_CFG
;
888 mask
[1] |= MADERA_GP1_DIR_MASK
;
889 conf
[1] |= MADERA_GP1_DIR
;
891 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
892 mask
[0] |= MADERA_GP1_IP_CFG
;
893 conf
[0] |= MADERA_GP1_IP_CFG
;
894 mask
[1] |= MADERA_GP1_DIR_MASK
;
895 conf
[1] |= MADERA_GP1_DIR
;
897 case PIN_CONFIG_OUTPUT
:
898 val
= pinconf_to_config_argument(*configs
);
899 mask
[0] |= MADERA_GP1_LVL_MASK
;
901 conf
[0] |= MADERA_GP1_LVL
;
903 conf
[0] &= ~MADERA_GP1_LVL
;
905 mask
[1] |= MADERA_GP1_DIR_MASK
;
906 conf
[1] &= ~MADERA_GP1_DIR
;
917 "%s gpio%d 0x%x:0x%x 0x%x:0x%x\n",
918 __func__
, pin
+ 1, reg
, conf
[0], reg
+ 1, conf
[1]);
920 ret
= regmap_update_bits(priv
->madera
->regmap
, reg
, mask
[0], conf
[0]);
925 ret
= regmap_update_bits(priv
->madera
->regmap
, reg
, mask
[1], conf
[1]);
933 "Failed to write GPIO%d conf (%d) reg 0x%x\n",
939 static int madera_pin_conf_group_set(struct pinctrl_dev
*pctldev
,
940 unsigned int selector
,
941 unsigned long *configs
,
942 unsigned int num_configs
)
944 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
945 const struct madera_pin_groups
*pin_group
;
946 unsigned int n_groups
= priv
->chip
->n_pin_groups
;
949 dev_dbg(priv
->dev
, "%s setting group %s\n", __func__
,
950 madera_get_group_name(pctldev
, selector
));
952 if (selector
>= n_groups
) {
953 /* group is a single pin, convert to pin number and set */
954 return madera_pin_conf_set(pctldev
,
959 pin_group
= &priv
->chip
->pin_groups
[selector
];
961 for (i
= 0; i
< pin_group
->n_pins
; ++i
) {
962 ret
= madera_pin_conf_set(pctldev
,
974 static const struct pinconf_ops madera_pin_conf_ops
= {
976 .pin_config_get
= madera_pin_conf_get
,
977 .pin_config_set
= madera_pin_conf_set
,
978 .pin_config_group_set
= madera_pin_conf_group_set
,
981 static struct pinctrl_desc madera_pin_desc
= {
982 .name
= "madera-pinctrl",
984 .pctlops
= &madera_pin_group_ops
,
985 .pmxops
= &madera_pin_mux_ops
,
986 .confops
= &madera_pin_conf_ops
,
987 .owner
= THIS_MODULE
,
990 static int madera_pin_probe(struct platform_device
*pdev
)
992 struct madera
*madera
= dev_get_drvdata(pdev
->dev
.parent
);
993 const struct madera_pdata
*pdata
= dev_get_platdata(madera
->dev
);
994 struct madera_pin_private
*priv
;
997 BUILD_BUG_ON(ARRAY_SIZE(madera_pin_single_group_names
) !=
998 ARRAY_SIZE(madera_pin_single_group_pins
));
1000 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1002 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
1006 priv
->dev
= &pdev
->dev
;
1007 priv
->madera
= madera
;
1008 pdev
->dev
.of_node
= madera
->dev
->of_node
;
1010 switch (madera
->type
) {
1012 if (IS_ENABLED(CONFIG_PINCTRL_CS47L35
))
1013 priv
->chip
= &cs47l35_pin_chip
;
1017 if (IS_ENABLED(CONFIG_PINCTRL_CS47L85
))
1018 priv
->chip
= &cs47l85_pin_chip
;
1022 if (IS_ENABLED(CONFIG_PINCTRL_CS47L90
))
1023 priv
->chip
= &cs47l90_pin_chip
;
1032 madera_pin_desc
.npins
= priv
->chip
->n_pins
;
1034 ret
= devm_pinctrl_register_and_init(&pdev
->dev
,
1039 dev_err(priv
->dev
, "Failed pinctrl register (%d)\n", ret
);
1043 /* if the configuration is provided through pdata, apply it */
1044 if (pdata
&& pdata
->gpio_configs
) {
1045 ret
= pinctrl_register_mappings(pdata
->gpio_configs
,
1046 pdata
->n_gpio_configs
);
1049 "Failed to register pdata mappings (%d)\n",
1055 ret
= pinctrl_enable(priv
->pctl
);
1057 dev_err(priv
->dev
, "Failed to enable pinctrl (%d)\n", ret
);
1061 dev_dbg(priv
->dev
, "pinctrl probed ok\n");
1066 static struct platform_driver madera_pin_driver
= {
1067 .probe
= madera_pin_probe
,
1069 .name
= "madera-pinctrl",
1073 module_platform_driver(madera_pin_driver
);
1075 MODULE_DESCRIPTION("Madera pinctrl driver");
1076 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1077 MODULE_LICENSE("GPL v2");