1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Core driver for the S32 CC (Common Chassis) pin controller
5 * Copyright 2017-2022,2024 NXP
6 * Copyright (C) 2022 SUSE LLC
7 * Copyright 2015-2016 Freescale Semiconductor, Inc.
10 #include <linux/bitops.h>
11 #include <linux/err.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pinctrl/machine.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/regmap.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
27 #include "../pinconf.h"
28 #include "../pinctrl-utils.h"
29 #include "pinctrl-s32.h"
31 #define S32_PIN_ID_SHIFT 4
32 #define S32_PIN_ID_MASK GENMASK(31, S32_PIN_ID_SHIFT)
34 #define S32_MSCR_SSS_MASK GENMASK(2, 0)
35 #define S32_MSCR_PUS BIT(12)
36 #define S32_MSCR_PUE BIT(13)
37 #define S32_MSCR_SRE(X) (((X) & GENMASK(3, 0)) << 14)
38 #define S32_MSCR_IBE BIT(19)
39 #define S32_MSCR_ODE BIT(20)
40 #define S32_MSCR_OBE BIT(21)
43 S32_PINCONF_UPDATE_ONLY
,
44 S32_PINCONF_OVERWRITE
,
47 static struct regmap_config s32_regmap_config
= {
53 static u32
get_pin_no(u32 pinmux
)
55 return (pinmux
& S32_PIN_ID_MASK
) >> S32_PIN_ID_SHIFT
;
58 static u32
get_pin_func(u32 pinmux
)
60 return pinmux
& GENMASK(3, 0);
63 struct s32_pinctrl_mem_region
{
65 const struct s32_pin_range
*pin_range
;
70 * Holds pin configuration for GPIO's.
71 * @pin_id: Pin ID for this GPIO
72 * @config: Pin settings
73 * @list: Linked list entry for each gpio pin
75 struct gpio_pin_config
{
78 struct list_head list
;
82 * Pad config save/restore for power suspend/resume.
84 struct s32_pinctrl_context
{
89 * @dev: a pointer back to containing device
90 * @pctl: a pointer to the pinctrl device structure
91 * @regions: reserved memory regions with start/end pin
92 * @info: structure containing information about the pin
93 * @gpio_configs: Saved configurations for GPIO pins
94 * @gpiop_configs_lock: lock for the `gpio_configs` list
95 * @s32_pinctrl_context: Configuration saved over system sleep
99 struct pinctrl_dev
*pctl
;
100 struct s32_pinctrl_mem_region
*regions
;
101 struct s32_pinctrl_soc_info
*info
;
102 struct list_head gpio_configs
;
103 spinlock_t gpio_configs_lock
;
104 #ifdef CONFIG_PM_SLEEP
105 struct s32_pinctrl_context saved_context
;
109 static struct s32_pinctrl_mem_region
*
110 s32_get_region(struct pinctrl_dev
*pctldev
, unsigned int pin
)
112 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
113 const struct s32_pin_range
*pin_range
;
114 unsigned int mem_regions
= ipctl
->info
->soc_data
->mem_regions
;
117 for (i
= 0; i
< mem_regions
; i
++) {
118 pin_range
= ipctl
->regions
[i
].pin_range
;
119 if (pin
>= pin_range
->start
&& pin
<= pin_range
->end
)
120 return &ipctl
->regions
[i
];
126 static inline int s32_check_pin(struct pinctrl_dev
*pctldev
,
129 return s32_get_region(pctldev
, pin
) ? 0 : -EINVAL
;
132 static inline int s32_regmap_read(struct pinctrl_dev
*pctldev
,
133 unsigned int pin
, unsigned int *val
)
135 struct s32_pinctrl_mem_region
*region
;
138 region
= s32_get_region(pctldev
, pin
);
142 offset
= (pin
- region
->pin_range
->start
) *
143 regmap_get_reg_stride(region
->map
);
145 return regmap_read(region
->map
, offset
, val
);
148 static inline int s32_regmap_write(struct pinctrl_dev
*pctldev
,
152 struct s32_pinctrl_mem_region
*region
;
155 region
= s32_get_region(pctldev
, pin
);
159 offset
= (pin
- region
->pin_range
->start
) *
160 regmap_get_reg_stride(region
->map
);
162 return regmap_write(region
->map
, offset
, val
);
166 static inline int s32_regmap_update(struct pinctrl_dev
*pctldev
, unsigned int pin
,
167 unsigned int mask
, unsigned int val
)
169 struct s32_pinctrl_mem_region
*region
;
172 region
= s32_get_region(pctldev
, pin
);
176 offset
= (pin
- region
->pin_range
->start
) *
177 regmap_get_reg_stride(region
->map
);
179 return regmap_update_bits(region
->map
, offset
, mask
, val
);
182 static int s32_get_groups_count(struct pinctrl_dev
*pctldev
)
184 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
185 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
187 return info
->ngroups
;
190 static const char *s32_get_group_name(struct pinctrl_dev
*pctldev
,
191 unsigned int selector
)
193 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
194 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
196 return info
->groups
[selector
].data
.name
;
199 static int s32_get_group_pins(struct pinctrl_dev
*pctldev
,
200 unsigned int selector
, const unsigned int **pins
,
203 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
204 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
206 *pins
= info
->groups
[selector
].data
.pins
;
207 *npins
= info
->groups
[selector
].data
.npins
;
212 static void s32_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
215 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
218 static int s32_dt_group_node_to_map(struct pinctrl_dev
*pctldev
,
219 struct device_node
*np
,
220 struct pinctrl_map
**map
,
221 unsigned int *reserved_maps
,
222 unsigned int *num_maps
,
223 const char *func_name
)
225 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
226 struct device
*dev
= ipctl
->dev
;
227 unsigned long *cfgs
= NULL
;
228 unsigned int n_cfgs
, reserve
= 1;
231 n_pins
= of_property_count_elems_of_size(np
, "pinmux", sizeof(u32
));
233 dev_warn(dev
, "Can't find 'pinmux' property in node %pOFn\n", np
);
234 } else if (!n_pins
) {
238 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &cfgs
, &n_cfgs
);
240 dev_err(dev
, "%pOF: could not parse node property\n", np
);
247 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
, num_maps
,
252 ret
= pinctrl_utils_add_map_mux(pctldev
, map
, reserved_maps
, num_maps
,
253 np
->name
, func_name
);
258 ret
= pinctrl_utils_add_map_configs(pctldev
, map
, reserved_maps
,
259 num_maps
, np
->name
, cfgs
, n_cfgs
,
260 PIN_MAP_TYPE_CONFIGS_GROUP
);
270 static int s32_dt_node_to_map(struct pinctrl_dev
*pctldev
,
271 struct device_node
*np_config
,
272 struct pinctrl_map
**map
,
273 unsigned int *num_maps
)
275 unsigned int reserved_maps
;
282 for_each_available_child_of_node_scoped(np_config
, np
) {
283 ret
= s32_dt_group_node_to_map(pctldev
, np
, map
,
284 &reserved_maps
, num_maps
,
287 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
295 static const struct pinctrl_ops s32_pctrl_ops
= {
296 .get_groups_count
= s32_get_groups_count
,
297 .get_group_name
= s32_get_group_name
,
298 .get_group_pins
= s32_get_group_pins
,
299 .pin_dbg_show
= s32_pin_dbg_show
,
300 .dt_node_to_map
= s32_dt_node_to_map
,
301 .dt_free_map
= pinctrl_utils_free_map
,
304 static int s32_pmx_set(struct pinctrl_dev
*pctldev
, unsigned int selector
,
307 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
308 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
310 struct s32_pin_group
*grp
;
313 * Configure the mux mode for each pin in the group for a specific
316 grp
= &info
->groups
[group
];
318 dev_dbg(ipctl
->dev
, "set mux for function %s group %s\n",
319 info
->functions
[selector
].name
, grp
->data
.name
);
321 /* Check beforehand so we don't have a partial config. */
322 for (i
= 0; i
< grp
->data
.npins
; i
++) {
323 if (s32_check_pin(pctldev
, grp
->data
.pins
[i
]) != 0) {
324 dev_err(info
->dev
, "invalid pin: %u in group: %u\n",
325 grp
->data
.pins
[i
], group
);
330 for (i
= 0, ret
= 0; i
< grp
->data
.npins
&& !ret
; i
++) {
331 ret
= s32_regmap_update(pctldev
, grp
->data
.pins
[i
],
332 S32_MSCR_SSS_MASK
, grp
->pin_sss
[i
]);
334 dev_err(info
->dev
, "Failed to set pin %u\n",
343 static int s32_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
345 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
346 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
348 return info
->nfunctions
;
351 static const char *s32_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
352 unsigned int selector
)
354 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
355 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
357 return info
->functions
[selector
].name
;
360 static int s32_pmx_get_groups(struct pinctrl_dev
*pctldev
,
361 unsigned int selector
,
362 const char * const **groups
,
363 unsigned int * const num_groups
)
365 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
366 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
368 *groups
= info
->functions
[selector
].groups
;
369 *num_groups
= info
->functions
[selector
].ngroups
;
374 static int s32_pmx_gpio_request_enable(struct pinctrl_dev
*pctldev
,
375 struct pinctrl_gpio_range
*range
,
378 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
379 struct gpio_pin_config
*gpio_pin
;
384 ret
= s32_regmap_read(pctldev
, offset
, &config
);
388 /* Save current configuration */
389 gpio_pin
= kmalloc(sizeof(*gpio_pin
), GFP_KERNEL
);
393 gpio_pin
->pin_id
= offset
;
394 gpio_pin
->config
= config
;
396 spin_lock_irqsave(&ipctl
->gpio_configs_lock
, flags
);
397 list_add(&gpio_pin
->list
, &ipctl
->gpio_configs
);
398 spin_unlock_irqrestore(&ipctl
->gpio_configs_lock
, flags
);
400 /* GPIO pin means SSS = 0 */
401 config
&= ~S32_MSCR_SSS_MASK
;
403 return s32_regmap_write(pctldev
, offset
, config
);
406 static void s32_pmx_gpio_disable_free(struct pinctrl_dev
*pctldev
,
407 struct pinctrl_gpio_range
*range
,
410 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
411 struct gpio_pin_config
*gpio_pin
, *tmp
;
415 spin_lock_irqsave(&ipctl
->gpio_configs_lock
, flags
);
417 list_for_each_entry_safe(gpio_pin
, tmp
, &ipctl
->gpio_configs
, list
) {
418 if (gpio_pin
->pin_id
== offset
) {
419 ret
= s32_regmap_write(pctldev
, gpio_pin
->pin_id
,
424 list_del(&gpio_pin
->list
);
431 spin_unlock_irqrestore(&ipctl
->gpio_configs_lock
, flags
);
434 static int s32_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
435 struct pinctrl_gpio_range
*range
,
439 /* Always enable IBE for GPIOs. This allows us to read the
440 * actual line value and compare it with the one set.
442 unsigned int config
= S32_MSCR_IBE
;
443 unsigned int mask
= S32_MSCR_IBE
| S32_MSCR_OBE
;
445 /* Enable output buffer */
447 config
|= S32_MSCR_OBE
;
449 return s32_regmap_update(pctldev
, offset
, mask
, config
);
452 static const struct pinmux_ops s32_pmx_ops
= {
453 .get_functions_count
= s32_pmx_get_funcs_count
,
454 .get_function_name
= s32_pmx_get_func_name
,
455 .get_function_groups
= s32_pmx_get_groups
,
456 .set_mux
= s32_pmx_set
,
457 .gpio_request_enable
= s32_pmx_gpio_request_enable
,
458 .gpio_disable_free
= s32_pmx_gpio_disable_free
,
459 .gpio_set_direction
= s32_pmx_gpio_set_direction
,
462 /* Set the reserved elements as -1 */
463 static const int support_slew
[] = {208, -1, -1, -1, 166, 150, 133, 83};
465 static int s32_get_slew_regval(int arg
)
469 /* Translate a real slew rate (MHz) to a register value */
470 for (i
= 0; i
< ARRAY_SIZE(support_slew
); i
++) {
471 if (arg
== support_slew
[i
])
478 static inline void s32_pin_set_pull(enum pin_config_param param
,
479 unsigned int *mask
, unsigned int *config
)
482 case PIN_CONFIG_BIAS_DISABLE
:
483 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
484 *config
&= ~(S32_MSCR_PUS
| S32_MSCR_PUE
);
486 case PIN_CONFIG_BIAS_PULL_UP
:
487 *config
|= S32_MSCR_PUS
| S32_MSCR_PUE
;
489 case PIN_CONFIG_BIAS_PULL_DOWN
:
490 *config
&= ~S32_MSCR_PUS
;
491 *config
|= S32_MSCR_PUE
;
497 *mask
|= S32_MSCR_PUS
| S32_MSCR_PUE
;
500 static int s32_parse_pincfg(unsigned long pincfg
, unsigned int *mask
,
501 unsigned int *config
)
503 enum pin_config_param param
;
507 param
= pinconf_to_config_param(pincfg
);
508 arg
= pinconf_to_config_argument(pincfg
);
511 /* All pins are persistent over suspend */
512 case PIN_CONFIG_PERSIST_STATE
:
514 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
515 *config
|= S32_MSCR_ODE
;
516 *mask
|= S32_MSCR_ODE
;
518 case PIN_CONFIG_DRIVE_PUSH_PULL
:
519 *config
&= ~S32_MSCR_ODE
;
520 *mask
|= S32_MSCR_ODE
;
522 case PIN_CONFIG_OUTPUT_ENABLE
:
524 *config
|= S32_MSCR_OBE
;
526 *config
&= ~S32_MSCR_OBE
;
527 *mask
|= S32_MSCR_OBE
;
529 case PIN_CONFIG_INPUT_ENABLE
:
531 *config
|= S32_MSCR_IBE
;
533 *config
&= ~S32_MSCR_IBE
;
534 *mask
|= S32_MSCR_IBE
;
536 case PIN_CONFIG_SLEW_RATE
:
537 ret
= s32_get_slew_regval(arg
);
540 *config
|= S32_MSCR_SRE((u32
)ret
);
541 *mask
|= S32_MSCR_SRE(~0);
543 case PIN_CONFIG_BIAS_DISABLE
:
544 case PIN_CONFIG_BIAS_PULL_UP
:
545 case PIN_CONFIG_BIAS_PULL_DOWN
:
546 s32_pin_set_pull(param
, mask
, config
);
548 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
549 *config
&= ~(S32_MSCR_ODE
| S32_MSCR_OBE
| S32_MSCR_IBE
);
550 *mask
|= S32_MSCR_ODE
| S32_MSCR_OBE
| S32_MSCR_IBE
;
551 s32_pin_set_pull(param
, mask
, config
);
560 static int s32_pinconf_mscr_write(struct pinctrl_dev
*pctldev
,
562 unsigned long *configs
,
563 unsigned int num_configs
,
564 enum s32_write_type write_type
)
566 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
567 unsigned int config
= 0, mask
= 0;
570 ret
= s32_check_pin(pctldev
, pin_id
);
574 dev_dbg(ipctl
->dev
, "pinconf set pin %s with %u configs\n",
575 pin_get_name(pctldev
, pin_id
), num_configs
);
577 for (i
= 0; i
< num_configs
; i
++) {
578 ret
= s32_parse_pincfg(configs
[i
], &mask
, &config
);
583 /* If the MSCR configuration has to be written,
584 * the SSS field should not be touched.
586 if (write_type
== S32_PINCONF_OVERWRITE
)
587 mask
= (unsigned int)~S32_MSCR_SSS_MASK
;
589 if (!config
&& !mask
)
592 if (write_type
== S32_PINCONF_OVERWRITE
)
593 dev_dbg(ipctl
->dev
, "set: pin %u cfg 0x%x\n", pin_id
, config
);
595 dev_dbg(ipctl
->dev
, "update: pin %u cfg 0x%x\n", pin_id
,
598 return s32_regmap_update(pctldev
, pin_id
, mask
, config
);
601 static int s32_pinconf_get(struct pinctrl_dev
*pctldev
,
603 unsigned long *config
)
605 return s32_regmap_read(pctldev
, pin_id
, (unsigned int *)config
);
608 static int s32_pinconf_set(struct pinctrl_dev
*pctldev
,
609 unsigned int pin_id
, unsigned long *configs
,
610 unsigned int num_configs
)
612 return s32_pinconf_mscr_write(pctldev
, pin_id
, configs
,
613 num_configs
, S32_PINCONF_UPDATE_ONLY
);
616 static int s32_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned int selector
,
617 unsigned long *configs
, unsigned int num_configs
)
619 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
620 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
621 struct s32_pin_group
*grp
;
624 grp
= &info
->groups
[selector
];
625 for (i
= 0; i
< grp
->data
.npins
; i
++) {
626 ret
= s32_pinconf_mscr_write(pctldev
, grp
->data
.pins
[i
],
627 configs
, num_configs
, S32_PINCONF_OVERWRITE
);
635 static void s32_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
636 struct seq_file
*s
, unsigned int pin_id
)
641 ret
= s32_regmap_read(pctldev
, pin_id
, &config
);
645 seq_printf(s
, "0x%x", config
);
648 static void s32_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
649 struct seq_file
*s
, unsigned int selector
)
651 struct s32_pinctrl
*ipctl
= pinctrl_dev_get_drvdata(pctldev
);
652 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
653 struct s32_pin_group
*grp
;
659 grp
= &info
->groups
[selector
];
660 for (i
= 0; i
< grp
->data
.npins
; i
++) {
661 name
= pin_get_name(pctldev
, grp
->data
.pins
[i
]);
662 ret
= s32_regmap_read(pctldev
, grp
->data
.pins
[i
], &config
);
665 seq_printf(s
, "%s: 0x%x\n", name
, config
);
669 static const struct pinconf_ops s32_pinconf_ops
= {
670 .pin_config_get
= s32_pinconf_get
,
671 .pin_config_set
= s32_pinconf_set
,
672 .pin_config_group_set
= s32_pconf_group_set
,
673 .pin_config_dbg_show
= s32_pinconf_dbg_show
,
674 .pin_config_group_dbg_show
= s32_pinconf_group_dbg_show
,
677 #ifdef CONFIG_PM_SLEEP
678 static bool s32_pinctrl_should_save(struct s32_pinctrl
*ipctl
,
681 const struct pin_desc
*pd
= pin_desc_get(ipctl
->pctl
, pin
);
687 * Only restore the pin if it is actually in use by the kernel (or
690 if (pd
->mux_owner
|| pd
->gpio_owner
)
696 int s32_pinctrl_suspend(struct device
*dev
)
698 struct platform_device
*pdev
= to_platform_device(dev
);
699 struct s32_pinctrl
*ipctl
= platform_get_drvdata(pdev
);
700 const struct pinctrl_pin_desc
*pin
;
701 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
702 struct s32_pinctrl_context
*saved_context
= &ipctl
->saved_context
;
707 for (i
= 0; i
< info
->soc_data
->npins
; i
++) {
708 pin
= &info
->soc_data
->pins
[i
];
710 if (!s32_pinctrl_should_save(ipctl
, pin
->number
))
713 ret
= s32_regmap_read(ipctl
->pctl
, pin
->number
, &config
);
717 saved_context
->pads
[i
] = config
;
723 int s32_pinctrl_resume(struct device
*dev
)
725 struct platform_device
*pdev
= to_platform_device(dev
);
726 struct s32_pinctrl
*ipctl
= platform_get_drvdata(pdev
);
727 const struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
728 const struct pinctrl_pin_desc
*pin
;
729 struct s32_pinctrl_context
*saved_context
= &ipctl
->saved_context
;
732 for (i
= 0; i
< info
->soc_data
->npins
; i
++) {
733 pin
= &info
->soc_data
->pins
[i
];
735 if (!s32_pinctrl_should_save(ipctl
, pin
->number
))
738 ret
= s32_regmap_write(ipctl
->pctl
, pin
->number
,
739 saved_context
->pads
[i
]);
748 static int s32_pinctrl_parse_groups(struct device_node
*np
,
749 struct s32_pin_group
*grp
,
750 struct s32_pinctrl_soc_info
*info
)
753 unsigned int *pins
, *sss
;
759 dev_dbg(dev
, "group: %pOFn\n", np
);
761 /* Initialise group */
762 grp
->data
.name
= np
->name
;
764 npins
= of_property_count_elems_of_size(np
, "pinmux", sizeof(u32
));
766 dev_err(dev
, "Failed to read 'pinmux' property in node %s.\n",
771 dev_err(dev
, "The group %s has no pins.\n", grp
->data
.name
);
775 grp
->data
.npins
= npins
;
777 pins
= devm_kcalloc(info
->dev
, npins
, sizeof(*pins
), GFP_KERNEL
);
778 sss
= devm_kcalloc(info
->dev
, npins
, sizeof(*sss
), GFP_KERNEL
);
783 of_property_for_each_u32(np
, "pinmux", pinmux
) {
784 pins
[i
] = get_pin_no(pinmux
);
785 sss
[i
] = get_pin_func(pinmux
);
787 dev_dbg(info
->dev
, "pin: 0x%x, sss: 0x%x", pins
[i
], sss
[i
]);
791 grp
->data
.pins
= pins
;
797 static int s32_pinctrl_parse_functions(struct device_node
*np
,
798 struct s32_pinctrl_soc_info
*info
,
801 struct pinfunction
*func
;
802 struct s32_pin_group
*grp
;
807 dev_dbg(info
->dev
, "parse function(%u): %pOFn\n", index
, np
);
809 func
= &info
->functions
[index
];
811 /* Initialise function */
812 func
->name
= np
->name
;
813 func
->ngroups
= of_get_child_count(np
);
814 if (func
->ngroups
== 0) {
815 dev_err(info
->dev
, "no groups defined in %pOF\n", np
);
819 groups
= devm_kcalloc(info
->dev
, func
->ngroups
,
820 sizeof(*func
->groups
), GFP_KERNEL
);
824 for_each_child_of_node_scoped(np
, child
) {
825 groups
[i
] = child
->name
;
826 grp
= &info
->groups
[info
->grp_index
++];
827 ret
= s32_pinctrl_parse_groups(child
, grp
, info
);
833 func
->groups
= groups
;
838 static int s32_pinctrl_probe_dt(struct platform_device
*pdev
,
839 struct s32_pinctrl
*ipctl
)
841 struct s32_pinctrl_soc_info
*info
= ipctl
->info
;
842 struct device_node
*np
= pdev
->dev
.of_node
;
843 struct resource
*res
;
846 unsigned int mem_regions
= info
->soc_data
->mem_regions
;
854 if (mem_regions
== 0 || mem_regions
>= 10000) {
855 dev_err(&pdev
->dev
, "mem_regions is invalid: %u\n", mem_regions
);
859 ipctl
->regions
= devm_kcalloc(&pdev
->dev
, mem_regions
,
860 sizeof(*ipctl
->regions
), GFP_KERNEL
);
864 for (i
= 0; i
< mem_regions
; i
++) {
865 base
= devm_platform_get_and_ioremap_resource(pdev
, i
, &res
);
867 return PTR_ERR(base
);
869 snprintf(ipctl
->regions
[i
].name
,
870 sizeof(ipctl
->regions
[i
].name
), "map%u", i
);
872 s32_regmap_config
.name
= ipctl
->regions
[i
].name
;
873 s32_regmap_config
.max_register
= resource_size(res
) -
874 s32_regmap_config
.reg_stride
;
876 map
= devm_regmap_init_mmio(&pdev
->dev
, base
,
879 dev_err(&pdev
->dev
, "Failed to init regmap[%u]\n", i
);
883 ipctl
->regions
[i
].map
= map
;
884 ipctl
->regions
[i
].pin_range
= &info
->soc_data
->mem_pin_ranges
[i
];
887 nfuncs
= of_get_child_count(np
);
889 dev_err(&pdev
->dev
, "no functions defined\n");
893 info
->nfunctions
= nfuncs
;
894 info
->functions
= devm_kcalloc(&pdev
->dev
, nfuncs
,
895 sizeof(*info
->functions
), GFP_KERNEL
);
896 if (!info
->functions
)
900 for_each_child_of_node_scoped(np
, child
)
901 info
->ngroups
+= of_get_child_count(child
);
903 info
->groups
= devm_kcalloc(&pdev
->dev
, info
->ngroups
,
904 sizeof(*info
->groups
), GFP_KERNEL
);
909 for_each_child_of_node_scoped(np
, child
) {
910 ret
= s32_pinctrl_parse_functions(child
, info
, i
++);
918 int s32_pinctrl_probe(struct platform_device
*pdev
,
919 const struct s32_pinctrl_soc_data
*soc_data
)
921 struct s32_pinctrl
*ipctl
;
923 struct pinctrl_desc
*s32_pinctrl_desc
;
924 struct s32_pinctrl_soc_info
*info
;
925 #ifdef CONFIG_PM_SLEEP
926 struct s32_pinctrl_context
*saved_context
;
929 if (!soc_data
|| !soc_data
->pins
|| !soc_data
->npins
) {
930 dev_err(&pdev
->dev
, "wrong pinctrl info\n");
934 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
938 info
->soc_data
= soc_data
;
939 info
->dev
= &pdev
->dev
;
941 /* Create state holders etc for this driver */
942 ipctl
= devm_kzalloc(&pdev
->dev
, sizeof(*ipctl
), GFP_KERNEL
);
947 ipctl
->dev
= info
->dev
;
948 platform_set_drvdata(pdev
, ipctl
);
950 INIT_LIST_HEAD(&ipctl
->gpio_configs
);
951 spin_lock_init(&ipctl
->gpio_configs_lock
);
954 devm_kmalloc(&pdev
->dev
, sizeof(*s32_pinctrl_desc
), GFP_KERNEL
);
955 if (!s32_pinctrl_desc
)
958 s32_pinctrl_desc
->name
= dev_name(&pdev
->dev
);
959 s32_pinctrl_desc
->pins
= info
->soc_data
->pins
;
960 s32_pinctrl_desc
->npins
= info
->soc_data
->npins
;
961 s32_pinctrl_desc
->pctlops
= &s32_pctrl_ops
;
962 s32_pinctrl_desc
->pmxops
= &s32_pmx_ops
;
963 s32_pinctrl_desc
->confops
= &s32_pinconf_ops
;
964 s32_pinctrl_desc
->owner
= THIS_MODULE
;
966 ret
= s32_pinctrl_probe_dt(pdev
, ipctl
);
968 dev_err(&pdev
->dev
, "fail to probe dt properties\n");
972 ipctl
->pctl
= devm_pinctrl_register(&pdev
->dev
, s32_pinctrl_desc
,
974 if (IS_ERR(ipctl
->pctl
))
975 return dev_err_probe(&pdev
->dev
, PTR_ERR(ipctl
->pctl
),
976 "could not register s32 pinctrl driver\n");
978 #ifdef CONFIG_PM_SLEEP
979 saved_context
= &ipctl
->saved_context
;
980 saved_context
->pads
=
981 devm_kcalloc(&pdev
->dev
, info
->soc_data
->npins
,
982 sizeof(*saved_context
->pads
),
984 if (!saved_context
->pads
)
988 dev_info(&pdev
->dev
, "initialized s32 pinctrl driver\n");