1 // SPDX-License-Identifier: GPL-2.0-only
3 * Pinctrl for Cirrus Logic Madera codecs
5 * Copyright (C) 2016-2018 Cirrus Logic
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/property.h>
12 #include <linux/regmap.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/pinctrl/pinconf.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
22 #include <linux/mfd/madera/core.h>
23 #include <linux/mfd/madera/registers.h>
25 #include "../pinctrl-utils.h"
27 #include "pinctrl-madera.h"
30 * Use pin GPIO names for consistency
31 * NOTE: IDs are zero-indexed for coding convenience
33 static const struct pinctrl_pin_desc madera_pins
[] = {
34 PINCTRL_PIN(0, "gpio1"),
35 PINCTRL_PIN(1, "gpio2"),
36 PINCTRL_PIN(2, "gpio3"),
37 PINCTRL_PIN(3, "gpio4"),
38 PINCTRL_PIN(4, "gpio5"),
39 PINCTRL_PIN(5, "gpio6"),
40 PINCTRL_PIN(6, "gpio7"),
41 PINCTRL_PIN(7, "gpio8"),
42 PINCTRL_PIN(8, "gpio9"),
43 PINCTRL_PIN(9, "gpio10"),
44 PINCTRL_PIN(10, "gpio11"),
45 PINCTRL_PIN(11, "gpio12"),
46 PINCTRL_PIN(12, "gpio13"),
47 PINCTRL_PIN(13, "gpio14"),
48 PINCTRL_PIN(14, "gpio15"),
49 PINCTRL_PIN(15, "gpio16"),
50 PINCTRL_PIN(16, "gpio17"),
51 PINCTRL_PIN(17, "gpio18"),
52 PINCTRL_PIN(18, "gpio19"),
53 PINCTRL_PIN(19, "gpio20"),
54 PINCTRL_PIN(20, "gpio21"),
55 PINCTRL_PIN(21, "gpio22"),
56 PINCTRL_PIN(22, "gpio23"),
57 PINCTRL_PIN(23, "gpio24"),
58 PINCTRL_PIN(24, "gpio25"),
59 PINCTRL_PIN(25, "gpio26"),
60 PINCTRL_PIN(26, "gpio27"),
61 PINCTRL_PIN(27, "gpio28"),
62 PINCTRL_PIN(28, "gpio29"),
63 PINCTRL_PIN(29, "gpio30"),
64 PINCTRL_PIN(30, "gpio31"),
65 PINCTRL_PIN(31, "gpio32"),
66 PINCTRL_PIN(32, "gpio33"),
67 PINCTRL_PIN(33, "gpio34"),
68 PINCTRL_PIN(34, "gpio35"),
69 PINCTRL_PIN(35, "gpio36"),
70 PINCTRL_PIN(36, "gpio37"),
71 PINCTRL_PIN(37, "gpio38"),
72 PINCTRL_PIN(38, "gpio39"),
73 PINCTRL_PIN(39, "gpio40"),
77 * All single-pin functions can be mapped to any GPIO, however pinmux applies
78 * functions to pin groups and only those groups declared as supporting that
79 * function. To make this work we must put each pin in its own dummy group so
80 * that the functions can be described as applying to all pins.
81 * Since these do not correspond to anything in the actual hardware - they are
82 * merely an adaptation to pinctrl's view of the world - we use the same name
83 * as the pin to avoid confusion when comparing with datasheet instructions
85 static const char * const madera_pin_single_group_names
[] = {
86 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
87 "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
88 "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
89 "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
90 "gpio29", "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35",
91 "gpio36", "gpio37", "gpio38", "gpio39", "gpio40",
94 /* set of pin numbers for single-pin groups, zero-indexed */
95 static const unsigned int madera_pin_single_group_pins
[] = {
97 7, 8, 9, 10, 11, 12, 13,
98 14, 15, 16, 17, 18, 19, 20,
99 21, 22, 23, 24, 25, 26, 27,
100 28, 29, 30, 31, 32, 33, 34,
104 static const char * const madera_aif1_group_names
[] = { "aif1" };
105 static const char * const madera_aif2_group_names
[] = { "aif2" };
106 static const char * const madera_aif3_group_names
[] = { "aif3" };
107 static const char * const madera_aif4_group_names
[] = { "aif4" };
108 static const char * const madera_mif1_group_names
[] = { "mif1" };
109 static const char * const madera_mif2_group_names
[] = { "mif2" };
110 static const char * const madera_mif3_group_names
[] = { "mif3" };
111 static const char * const madera_dmic3_group_names
[] = { "dmic3" };
112 static const char * const madera_dmic4_group_names
[] = { "dmic4" };
113 static const char * const madera_dmic5_group_names
[] = { "dmic5" };
114 static const char * const madera_dmic6_group_names
[] = { "dmic6" };
115 static const char * const madera_spk1_group_names
[] = { "pdmspk1" };
116 static const char * const madera_spk2_group_names
[] = { "pdmspk2" };
119 * alt-functions always apply to a single pin group, other functions always
122 static const struct {
124 const char * const *group_names
;
126 } madera_mux_funcs
[] = {
129 .group_names
= madera_aif1_group_names
,
134 .group_names
= madera_aif2_group_names
,
139 .group_names
= madera_aif3_group_names
,
144 .group_names
= madera_aif4_group_names
,
149 .group_names
= madera_mif1_group_names
,
154 .group_names
= madera_mif2_group_names
,
159 .group_names
= madera_mif3_group_names
,
164 .group_names
= madera_dmic3_group_names
,
169 .group_names
= madera_dmic4_group_names
,
174 .group_names
= madera_dmic5_group_names
,
179 .group_names
= madera_dmic6_group_names
,
184 .group_names
= madera_spk1_group_names
,
189 .group_names
= madera_spk2_group_names
,
194 .group_names
= madera_pin_single_group_names
,
199 .group_names
= madera_pin_single_group_names
,
204 .group_names
= madera_pin_single_group_names
,
209 .group_names
= madera_pin_single_group_names
,
214 .group_names
= madera_pin_single_group_names
,
219 .group_names
= madera_pin_single_group_names
,
224 .group_names
= madera_pin_single_group_names
,
229 .group_names
= madera_pin_single_group_names
,
234 .group_names
= madera_pin_single_group_names
,
239 .group_names
= madera_pin_single_group_names
,
244 .group_names
= madera_pin_single_group_names
,
248 .name
= "fllao-lock",
249 .group_names
= madera_pin_single_group_names
,
254 .group_names
= madera_pin_single_group_names
,
258 .name
= "opclk-async",
259 .group_names
= madera_pin_single_group_names
,
264 .group_names
= madera_pin_single_group_names
,
269 .group_names
= madera_pin_single_group_names
,
274 .group_names
= madera_pin_single_group_names
,
278 .name
= "asrc1-in1-lock",
279 .group_names
= madera_pin_single_group_names
,
283 .name
= "asrc1-in2-lock",
284 .group_names
= madera_pin_single_group_names
,
288 .name
= "asrc2-in1-lock",
289 .group_names
= madera_pin_single_group_names
,
293 .name
= "asrc2-in2-lock",
294 .group_names
= madera_pin_single_group_names
,
298 .name
= "spkl-short-circuit",
299 .group_names
= madera_pin_single_group_names
,
303 .name
= "spkr-short-circuit",
304 .group_names
= madera_pin_single_group_names
,
308 .name
= "spk-shutdown",
309 .group_names
= madera_pin_single_group_names
,
313 .name
= "spk-overheat-shutdown",
314 .group_names
= madera_pin_single_group_names
,
318 .name
= "spk-overheat-warn",
319 .group_names
= madera_pin_single_group_names
,
323 .name
= "timer1-sts",
324 .group_names
= madera_pin_single_group_names
,
328 .name
= "timer2-sts",
329 .group_names
= madera_pin_single_group_names
,
333 .name
= "timer3-sts",
334 .group_names
= madera_pin_single_group_names
,
338 .name
= "timer4-sts",
339 .group_names
= madera_pin_single_group_names
,
343 .name
= "timer5-sts",
344 .group_names
= madera_pin_single_group_names
,
348 .name
= "timer6-sts",
349 .group_names
= madera_pin_single_group_names
,
353 .name
= "timer7-sts",
354 .group_names
= madera_pin_single_group_names
,
358 .name
= "timer8-sts",
359 .group_names
= madera_pin_single_group_names
,
363 .name
= "log1-fifo-ne",
364 .group_names
= madera_pin_single_group_names
,
368 .name
= "log2-fifo-ne",
369 .group_names
= madera_pin_single_group_names
,
373 .name
= "log3-fifo-ne",
374 .group_names
= madera_pin_single_group_names
,
378 .name
= "log4-fifo-ne",
379 .group_names
= madera_pin_single_group_names
,
383 .name
= "log5-fifo-ne",
384 .group_names
= madera_pin_single_group_names
,
388 .name
= "log6-fifo-ne",
389 .group_names
= madera_pin_single_group_names
,
393 .name
= "log7-fifo-ne",
394 .group_names
= madera_pin_single_group_names
,
398 .name
= "log8-fifo-ne",
399 .group_names
= madera_pin_single_group_names
,
403 .name
= "aux-pdm-clk",
404 .group_names
= madera_pin_single_group_names
,
408 .name
= "aux-pdm-dat",
409 .group_names
= madera_pin_single_group_names
,
414 static u16
madera_pin_make_drv_str(struct madera_pin_private
*priv
,
415 unsigned int milliamps
)
421 return 2 << MADERA_GP1_DRV_STR_SHIFT
;
426 dev_warn(priv
->dev
, "%u mA not a valid drive strength", milliamps
);
431 static unsigned int madera_pin_unmake_drv_str(struct madera_pin_private
*priv
,
434 regval
= (regval
& MADERA_GP1_DRV_STR_MASK
) >> MADERA_GP1_DRV_STR_SHIFT
;
446 static int madera_get_groups_count(struct pinctrl_dev
*pctldev
)
448 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
450 /* Number of alt function groups plus number of single-pin groups */
451 return priv
->chip
->n_pin_groups
+ priv
->chip
->n_pins
;
454 static const char *madera_get_group_name(struct pinctrl_dev
*pctldev
,
455 unsigned int selector
)
457 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
459 if (selector
< priv
->chip
->n_pin_groups
)
460 return priv
->chip
->pin_groups
[selector
].name
;
462 selector
-= priv
->chip
->n_pin_groups
;
463 return madera_pin_single_group_names
[selector
];
466 static int madera_get_group_pins(struct pinctrl_dev
*pctldev
,
467 unsigned int selector
,
468 const unsigned int **pins
,
469 unsigned int *num_pins
)
471 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
473 if (selector
< priv
->chip
->n_pin_groups
) {
474 *pins
= priv
->chip
->pin_groups
[selector
].pins
;
475 *num_pins
= priv
->chip
->pin_groups
[selector
].n_pins
;
477 /* return the dummy group for a single pin */
478 selector
-= priv
->chip
->n_pin_groups
;
479 *pins
= &madera_pin_single_group_pins
[selector
];
485 static void madera_pin_dbg_show_fn(struct madera_pin_private
*priv
,
487 unsigned int pin
, unsigned int fn
)
489 const struct madera_pin_chip
*chip
= priv
->chip
;
493 for (i
= 0; i
< ARRAY_SIZE(madera_mux_funcs
); ++i
) {
494 if (madera_mux_funcs
[i
].func
== fn
) {
495 seq_printf(s
, " FN=%s",
496 madera_mux_funcs
[i
].name
);
500 return; /* ignore unknown function values */
504 for (i
= 0; i
< chip
->n_pin_groups
; ++i
) {
505 for (g_pin
= 0; g_pin
< chip
->pin_groups
[i
].n_pins
; ++g_pin
) {
506 if (chip
->pin_groups
[i
].pins
[g_pin
] == pin
) {
507 seq_printf(s
, " FN=%s",
508 chip
->pin_groups
[i
].name
);
515 static void __maybe_unused
madera_pin_dbg_show(struct pinctrl_dev
*pctldev
,
519 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
520 unsigned int conf
[2];
521 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
525 ret
= regmap_read(priv
->madera
->regmap
, reg
, &conf
[0]);
529 ret
= regmap_read(priv
->madera
->regmap
, reg
+ 1, &conf
[1]);
533 seq_printf(s
, "%04x:%04x", conf
[0], conf
[1]);
535 fn
= (conf
[0] & MADERA_GP1_FN_MASK
) >> MADERA_GP1_FN_SHIFT
;
536 madera_pin_dbg_show_fn(priv
, s
, pin
, fn
);
538 /* State of direction bit is only relevant if function==1 */
540 if (conf
[1] & MADERA_GP1_DIR_MASK
)
546 if (conf
[1] & MADERA_GP1_PU_MASK
)
549 if (conf
[1] & MADERA_GP1_PD_MASK
)
552 if (conf
[0] & MADERA_GP1_DB_MASK
)
555 if (conf
[0] & MADERA_GP1_OP_CFG_MASK
)
558 seq_puts(s
, " CMOS");
560 seq_printf(s
, " DRV=%umA", madera_pin_unmake_drv_str(priv
, conf
[1]));
562 if (conf
[0] & MADERA_GP1_IP_CFG_MASK
)
563 seq_puts(s
, " SCHMITT");
566 static const struct pinctrl_ops madera_pin_group_ops
= {
567 .get_groups_count
= madera_get_groups_count
,
568 .get_group_name
= madera_get_group_name
,
569 .get_group_pins
= madera_get_group_pins
,
570 #if IS_ENABLED(CONFIG_OF)
571 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
572 .dt_free_map
= pinctrl_utils_free_map
,
574 #if IS_ENABLED(CONFIG_DEBUG_FS)
575 .pin_dbg_show
= madera_pin_dbg_show
,
579 static int madera_mux_get_funcs_count(struct pinctrl_dev
*pctldev
)
581 return ARRAY_SIZE(madera_mux_funcs
);
584 static const char *madera_mux_get_func_name(struct pinctrl_dev
*pctldev
,
585 unsigned int selector
)
587 return madera_mux_funcs
[selector
].name
;
590 static int madera_mux_get_groups(struct pinctrl_dev
*pctldev
,
591 unsigned int selector
,
592 const char * const **groups
,
593 unsigned int * const num_groups
)
595 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
597 *groups
= madera_mux_funcs
[selector
].group_names
;
599 if (madera_mux_funcs
[selector
].func
== 0) {
600 /* alt func always maps to a single group */
603 /* other funcs map to all available gpio pins */
604 *num_groups
= priv
->chip
->n_pins
;
610 static int madera_mux_set_mux(struct pinctrl_dev
*pctldev
,
611 unsigned int selector
,
614 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
615 struct madera
*madera
= priv
->madera
;
616 const struct madera_pin_groups
*pin_group
= priv
->chip
->pin_groups
;
617 unsigned int n_chip_groups
= priv
->chip
->n_pin_groups
;
618 const char *func_name
= madera_mux_funcs
[selector
].name
;
622 dev_dbg(priv
->dev
, "%s selecting %u (%s) for group %u (%s)\n",
623 __func__
, selector
, func_name
, group
,
624 madera_get_group_name(pctldev
, group
));
626 if (madera_mux_funcs
[selector
].func
== 0) {
627 /* alt func pin assignments are codec-specific */
628 for (i
= 0; i
< n_chip_groups
; ++i
) {
629 if (strcmp(func_name
, pin_group
->name
) == 0)
635 if (i
== n_chip_groups
)
638 for (i
= 0; i
< pin_group
->n_pins
; ++i
) {
639 reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin_group
->pins
[i
]);
641 dev_dbg(priv
->dev
, "%s setting 0x%x func bits to 0\n",
644 ret
= regmap_update_bits(madera
->regmap
, reg
,
645 MADERA_GP1_FN_MASK
, 0);
652 * for other funcs the group will be the gpio number and will
653 * be offset by the number of chip-specific functions at the
654 * start of the group list
656 group
-= n_chip_groups
;
657 reg
= MADERA_GPIO1_CTRL_1
+ (2 * group
);
659 dev_dbg(priv
->dev
, "%s setting 0x%x func bits to 0x%x\n",
660 __func__
, reg
, madera_mux_funcs
[selector
].func
);
662 ret
= regmap_update_bits(madera
->regmap
,
665 madera_mux_funcs
[selector
].func
);
669 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
674 static int madera_gpio_set_direction(struct pinctrl_dev
*pctldev
,
675 struct pinctrl_gpio_range
*range
,
679 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
680 struct madera
*madera
= priv
->madera
;
681 unsigned int reg
= MADERA_GPIO1_CTRL_2
+ (2 * offset
);
686 val
= MADERA_GP1_DIR
;
690 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_DIR_MASK
, val
);
692 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
697 static int madera_gpio_request_enable(struct pinctrl_dev
*pctldev
,
698 struct pinctrl_gpio_range
*range
,
701 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
702 struct madera
*madera
= priv
->madera
;
703 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * offset
);
706 /* put the pin into GPIO mode */
707 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_FN_MASK
, 1);
709 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
714 static void madera_gpio_disable_free(struct pinctrl_dev
*pctldev
,
715 struct pinctrl_gpio_range
*range
,
718 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
719 struct madera
*madera
= priv
->madera
;
720 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * offset
);
723 /* disable GPIO by setting to GPIO IN */
724 madera_gpio_set_direction(pctldev
, range
, offset
, true);
726 ret
= regmap_update_bits(madera
->regmap
, reg
, MADERA_GP1_FN_MASK
, 1);
728 dev_err(priv
->dev
, "Failed to write to 0x%x (%d)\n", reg
, ret
);
731 static const struct pinmux_ops madera_pin_mux_ops
= {
732 .get_functions_count
= madera_mux_get_funcs_count
,
733 .get_function_name
= madera_mux_get_func_name
,
734 .get_function_groups
= madera_mux_get_groups
,
735 .set_mux
= madera_mux_set_mux
,
736 .gpio_request_enable
= madera_gpio_request_enable
,
737 .gpio_disable_free
= madera_gpio_disable_free
,
738 .gpio_set_direction
= madera_gpio_set_direction
,
739 .strict
= true, /* GPIO and other functions are exclusive */
742 static int madera_pin_conf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
743 unsigned long *config
)
745 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
746 unsigned int param
= pinconf_to_config_param(*config
);
747 unsigned int result
= 0;
748 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
749 unsigned int conf
[2];
752 ret
= regmap_read(priv
->madera
->regmap
, reg
, &conf
[0]);
754 ret
= regmap_read(priv
->madera
->regmap
, reg
+ 1, &conf
[1]);
757 dev_err(priv
->dev
, "Failed to read GP%d conf (%d)\n",
763 case PIN_CONFIG_BIAS_BUS_HOLD
:
764 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
765 if (conf
[1] == (MADERA_GP1_PU
| MADERA_GP1_PD
))
768 case PIN_CONFIG_BIAS_DISABLE
:
769 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
773 case PIN_CONFIG_BIAS_PULL_DOWN
:
774 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
775 if (conf
[1] == MADERA_GP1_PD_MASK
)
778 case PIN_CONFIG_BIAS_PULL_UP
:
779 conf
[1] &= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
780 if (conf
[1] == MADERA_GP1_PU_MASK
)
783 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
784 if (conf
[0] & MADERA_GP1_OP_CFG_MASK
)
787 case PIN_CONFIG_DRIVE_PUSH_PULL
:
788 if (!(conf
[0] & MADERA_GP1_OP_CFG_MASK
))
791 case PIN_CONFIG_DRIVE_STRENGTH
:
792 result
= madera_pin_unmake_drv_str(priv
, conf
[1]);
794 case PIN_CONFIG_INPUT_DEBOUNCE
:
795 if (conf
[0] & MADERA_GP1_DB_MASK
)
798 case PIN_CONFIG_INPUT_ENABLE
:
799 if (conf
[0] & MADERA_GP1_DIR_MASK
)
802 case PIN_CONFIG_INPUT_SCHMITT
:
803 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
804 if (conf
[0] & MADERA_GP1_IP_CFG_MASK
)
807 case PIN_CONFIG_OUTPUT
:
808 if ((conf
[1] & MADERA_GP1_DIR_MASK
) &&
809 (conf
[0] & MADERA_GP1_LVL_MASK
))
816 *config
= pinconf_to_config_packed(param
, result
);
821 static int madera_pin_conf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
822 unsigned long *configs
, unsigned int num_configs
)
824 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
825 u16 conf
[2] = {0, 0};
826 u16 mask
[2] = {0, 0};
827 unsigned int reg
= MADERA_GPIO1_CTRL_1
+ (2 * pin
);
831 while (num_configs
) {
832 dev_dbg(priv
->dev
, "%s config 0x%lx\n", __func__
, *configs
);
834 switch (pinconf_to_config_param(*configs
)) {
835 case PIN_CONFIG_BIAS_BUS_HOLD
:
836 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
837 conf
[1] |= MADERA_GP1_PU
| MADERA_GP1_PD
;
839 case PIN_CONFIG_BIAS_DISABLE
:
840 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
841 conf
[1] &= ~(MADERA_GP1_PU
| MADERA_GP1_PD
);
843 case PIN_CONFIG_BIAS_PULL_DOWN
:
844 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
845 conf
[1] |= MADERA_GP1_PD
;
846 conf
[1] &= ~MADERA_GP1_PU
;
848 case PIN_CONFIG_BIAS_PULL_UP
:
849 mask
[1] |= MADERA_GP1_PU_MASK
| MADERA_GP1_PD_MASK
;
850 conf
[1] |= MADERA_GP1_PU
;
851 conf
[1] &= ~MADERA_GP1_PD
;
853 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
854 mask
[0] |= MADERA_GP1_OP_CFG_MASK
;
855 conf
[0] |= MADERA_GP1_OP_CFG
;
857 case PIN_CONFIG_DRIVE_PUSH_PULL
:
858 mask
[0] |= MADERA_GP1_OP_CFG_MASK
;
859 conf
[0] &= ~MADERA_GP1_OP_CFG
;
861 case PIN_CONFIG_DRIVE_STRENGTH
:
862 val
= pinconf_to_config_argument(*configs
);
863 mask
[1] |= MADERA_GP1_DRV_STR_MASK
;
864 conf
[1] &= ~MADERA_GP1_DRV_STR_MASK
;
865 conf
[1] |= madera_pin_make_drv_str(priv
, val
);
867 case PIN_CONFIG_INPUT_DEBOUNCE
:
868 mask
[0] |= MADERA_GP1_DB_MASK
;
871 * we can't configure debounce time per-pin so value
874 val
= pinconf_to_config_argument(*configs
);
876 conf
[0] |= MADERA_GP1_DB
;
878 conf
[0] &= ~MADERA_GP1_DB
;
880 case PIN_CONFIG_INPUT_ENABLE
:
881 val
= pinconf_to_config_argument(*configs
);
882 mask
[1] |= MADERA_GP1_DIR_MASK
;
884 conf
[1] |= MADERA_GP1_DIR
;
886 conf
[1] &= ~MADERA_GP1_DIR
;
888 case PIN_CONFIG_INPUT_SCHMITT
:
889 val
= pinconf_to_config_argument(*configs
);
890 mask
[0] |= MADERA_GP1_IP_CFG
;
892 conf
[0] |= MADERA_GP1_IP_CFG
;
894 conf
[0] &= ~MADERA_GP1_IP_CFG
;
896 mask
[1] |= MADERA_GP1_DIR_MASK
;
897 conf
[1] |= MADERA_GP1_DIR
;
899 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
900 mask
[0] |= MADERA_GP1_IP_CFG
;
901 conf
[0] |= MADERA_GP1_IP_CFG
;
902 mask
[1] |= MADERA_GP1_DIR_MASK
;
903 conf
[1] |= MADERA_GP1_DIR
;
905 case PIN_CONFIG_OUTPUT
:
906 val
= pinconf_to_config_argument(*configs
);
907 mask
[0] |= MADERA_GP1_LVL_MASK
;
909 conf
[0] |= MADERA_GP1_LVL
;
911 conf
[0] &= ~MADERA_GP1_LVL
;
913 mask
[1] |= MADERA_GP1_DIR_MASK
;
914 conf
[1] &= ~MADERA_GP1_DIR
;
925 "%s gpio%d 0x%x:0x%x 0x%x:0x%x\n",
926 __func__
, pin
+ 1, reg
, conf
[0], reg
+ 1, conf
[1]);
928 ret
= regmap_update_bits(priv
->madera
->regmap
, reg
, mask
[0], conf
[0]);
933 ret
= regmap_update_bits(priv
->madera
->regmap
, reg
, mask
[1], conf
[1]);
941 "Failed to write GPIO%d conf (%d) reg 0x%x\n",
947 static int madera_pin_conf_group_set(struct pinctrl_dev
*pctldev
,
948 unsigned int selector
,
949 unsigned long *configs
,
950 unsigned int num_configs
)
952 struct madera_pin_private
*priv
= pinctrl_dev_get_drvdata(pctldev
);
953 const struct madera_pin_groups
*pin_group
;
954 unsigned int n_groups
= priv
->chip
->n_pin_groups
;
957 dev_dbg(priv
->dev
, "%s setting group %s\n", __func__
,
958 madera_get_group_name(pctldev
, selector
));
960 if (selector
>= n_groups
) {
961 /* group is a single pin, convert to pin number and set */
962 return madera_pin_conf_set(pctldev
,
967 pin_group
= &priv
->chip
->pin_groups
[selector
];
969 for (i
= 0; i
< pin_group
->n_pins
; ++i
) {
970 ret
= madera_pin_conf_set(pctldev
,
982 static const struct pinconf_ops madera_pin_conf_ops
= {
984 .pin_config_get
= madera_pin_conf_get
,
985 .pin_config_set
= madera_pin_conf_set
,
986 .pin_config_group_set
= madera_pin_conf_group_set
,
989 static struct pinctrl_desc madera_pin_desc
= {
990 .name
= "madera-pinctrl",
992 .pctlops
= &madera_pin_group_ops
,
993 .pmxops
= &madera_pin_mux_ops
,
994 .confops
= &madera_pin_conf_ops
,
995 .owner
= THIS_MODULE
,
998 static int madera_pin_probe(struct platform_device
*pdev
)
1000 struct madera
*madera
= dev_get_drvdata(pdev
->dev
.parent
);
1001 const struct madera_pdata
*pdata
= &madera
->pdata
;
1002 struct madera_pin_private
*priv
;
1005 BUILD_BUG_ON(ARRAY_SIZE(madera_pin_single_group_names
) !=
1006 ARRAY_SIZE(madera_pin_single_group_pins
));
1008 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1010 device_set_node(&pdev
->dev
, dev_fwnode(pdev
->dev
.parent
));
1012 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
1016 priv
->dev
= &pdev
->dev
;
1017 priv
->madera
= madera
;
1019 switch (madera
->type
) {
1021 if (IS_ENABLED(CONFIG_PINCTRL_CS47L15
))
1022 priv
->chip
= &cs47l15_pin_chip
;
1025 if (IS_ENABLED(CONFIG_PINCTRL_CS47L35
))
1026 priv
->chip
= &cs47l35_pin_chip
;
1030 if (IS_ENABLED(CONFIG_PINCTRL_CS47L85
))
1031 priv
->chip
= &cs47l85_pin_chip
;
1035 if (IS_ENABLED(CONFIG_PINCTRL_CS47L90
))
1036 priv
->chip
= &cs47l90_pin_chip
;
1041 if (IS_ENABLED(CONFIG_PINCTRL_CS47L92
))
1042 priv
->chip
= &cs47l92_pin_chip
;
1051 madera_pin_desc
.npins
= priv
->chip
->n_pins
;
1053 ret
= devm_pinctrl_register_and_init(&pdev
->dev
,
1058 dev_err(priv
->dev
, "Failed pinctrl register (%d)\n", ret
);
1062 /* if the configuration is provided through pdata, apply it */
1063 if (pdata
->gpio_configs
) {
1064 ret
= pinctrl_register_mappings(pdata
->gpio_configs
,
1065 pdata
->n_gpio_configs
);
1067 return dev_err_probe(priv
->dev
, ret
,
1068 "Failed to register pdata mappings\n");
1071 ret
= pinctrl_enable(priv
->pctl
);
1073 dev_err(priv
->dev
, "Failed to enable pinctrl (%d)\n", ret
);
1077 platform_set_drvdata(pdev
, priv
);
1079 dev_dbg(priv
->dev
, "pinctrl probed ok\n");
1084 static void madera_pin_remove(struct platform_device
*pdev
)
1086 struct madera_pin_private
*priv
= platform_get_drvdata(pdev
);
1088 if (priv
->madera
->pdata
.gpio_configs
)
1089 pinctrl_unregister_mappings(priv
->madera
->pdata
.gpio_configs
);
1092 static struct platform_driver madera_pin_driver
= {
1093 .probe
= madera_pin_probe
,
1094 .remove
= madera_pin_remove
,
1096 .name
= "madera-pinctrl",
1100 module_platform_driver(madera_pin_driver
);
1102 MODULE_DESCRIPTION("Madera pinctrl driver");
1103 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1104 MODULE_LICENSE("GPL v2");