1 // SPDX-License-Identifier: GPL-2.0-only
3 * Spreadtrum pin controller driver
4 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
7 #include <linux/debugfs.h>
9 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
26 #include "../pinmux.h"
27 #include "../pinconf.h"
28 #include "../pinctrl-utils.h"
29 #include "pinctrl-sprd.h"
31 #define PINCTRL_BIT_MASK(width) (~(~0UL << (width)))
32 #define PINCTRL_REG_OFFSET 0x20
33 #define PINCTRL_REG_MISC_OFFSET 0x4020
34 #define PINCTRL_REG_LEN 0x4
36 #define PIN_FUNC_MASK (BIT(4) | BIT(5))
37 #define PIN_FUNC_SEL_1 ~PIN_FUNC_MASK
38 #define PIN_FUNC_SEL_2 BIT(4)
39 #define PIN_FUNC_SEL_3 BIT(5)
40 #define PIN_FUNC_SEL_4 PIN_FUNC_MASK
42 #define AP_SLEEP_MODE BIT(13)
43 #define PUBCP_SLEEP_MODE BIT(14)
44 #define TGLDSP_SLEEP_MODE BIT(15)
45 #define AGDSP_SLEEP_MODE BIT(16)
46 #define CM4_SLEEP_MODE BIT(17)
47 #define SLEEP_MODE_MASK GENMASK(5, 0)
48 #define SLEEP_MODE_SHIFT 13
50 #define SLEEP_INPUT BIT(1)
51 #define SLEEP_INPUT_MASK 0x1
52 #define SLEEP_INPUT_SHIFT 1
54 #define SLEEP_OUTPUT BIT(0)
55 #define SLEEP_OUTPUT_MASK 0x1
56 #define SLEEP_OUTPUT_SHIFT 0
58 #define DRIVE_STRENGTH_MASK GENMASK(3, 0)
59 #define DRIVE_STRENGTH_SHIFT 19
61 #define SLEEP_PULL_DOWN BIT(2)
62 #define SLEEP_PULL_DOWN_MASK 0x1
63 #define SLEEP_PULL_DOWN_SHIFT 2
65 #define PULL_DOWN BIT(6)
66 #define PULL_DOWN_MASK 0x1
67 #define PULL_DOWN_SHIFT 6
69 #define SLEEP_PULL_UP BIT(3)
70 #define SLEEP_PULL_UP_MASK 0x1
71 #define SLEEP_PULL_UP_SHIFT 3
73 #define PULL_UP_4_7K (BIT(12) | BIT(7))
74 #define PULL_UP_20K BIT(7)
75 #define PULL_UP_MASK 0x21
76 #define PULL_UP_SHIFT 7
78 #define INPUT_SCHMITT BIT(11)
79 #define INPUT_SCHMITT_MASK 0x1
80 #define INPUT_SCHMITT_SHIFT 11
85 TGLDSP_SLEEP
= BIT(2),
99 * struct sprd_pin: represent one pin's description
101 * @number: pin number
102 * @type: pin type, can be GLOBAL_CTRL_PIN/COMMON_PIN/MISC_PIN
103 * @reg: pin register address
104 * @bit_offset: bit offset in pin register
105 * @bit_width: bit width in pin register
112 unsigned long bit_offset
;
113 unsigned long bit_width
;
117 * struct sprd_pin_group: represent one group's description
119 * @npins: pin numbers of this group
120 * @pins: pointer to pins array
122 struct sprd_pin_group
{
129 * struct sprd_pinctrl_soc_info: represent the SoC's pins description
130 * @groups: pointer to groups of pins
131 * @ngroups: group numbers of the whole SoC
132 * @pins: pointer to pins description
133 * @npins: pin numbers of the whole SoC
134 * @grp_names: pointer to group names array
136 struct sprd_pinctrl_soc_info
{
137 struct sprd_pin_group
*groups
;
138 unsigned int ngroups
;
139 struct sprd_pin
*pins
;
141 const char **grp_names
;
145 * struct sprd_pinctrl: represent the pin controller device
146 * @dev: pointer to the device structure
147 * @pctl: pointer to the pinctrl handle
148 * @base: base address of the controller
149 * @info: pointer to SoC's pins description information
151 struct sprd_pinctrl
{
153 struct pinctrl_dev
*pctl
;
155 struct sprd_pinctrl_soc_info
*info
;
158 #define SPRD_PIN_CONFIG_CONTROL (PIN_CONFIG_END + 1)
159 #define SPRD_PIN_CONFIG_SLEEP_MODE (PIN_CONFIG_END + 2)
161 static int sprd_pinctrl_get_id_by_name(struct sprd_pinctrl
*sprd_pctl
,
164 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
167 for (i
= 0; i
< info
->npins
; i
++) {
168 if (!strcmp(info
->pins
[i
].name
, name
))
169 return info
->pins
[i
].number
;
175 static struct sprd_pin
*
176 sprd_pinctrl_get_pin_by_id(struct sprd_pinctrl
*sprd_pctl
, unsigned int id
)
178 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
179 struct sprd_pin
*pin
= NULL
;
182 for (i
= 0; i
< info
->npins
; i
++) {
183 if (info
->pins
[i
].number
== id
) {
184 pin
= &info
->pins
[i
];
192 static const struct sprd_pin_group
*
193 sprd_pinctrl_find_group_by_name(struct sprd_pinctrl
*sprd_pctl
,
196 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
197 const struct sprd_pin_group
*grp
= NULL
;
200 for (i
= 0; i
< info
->ngroups
; i
++) {
201 if (!strcmp(info
->groups
[i
].name
, name
)) {
202 grp
= &info
->groups
[i
];
210 static int sprd_pctrl_group_count(struct pinctrl_dev
*pctldev
)
212 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
213 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
215 return info
->ngroups
;
218 static const char *sprd_pctrl_group_name(struct pinctrl_dev
*pctldev
,
219 unsigned int selector
)
221 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
222 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
224 return info
->groups
[selector
].name
;
227 static int sprd_pctrl_group_pins(struct pinctrl_dev
*pctldev
,
228 unsigned int selector
,
229 const unsigned int **pins
,
232 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
233 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
235 if (selector
>= info
->ngroups
)
238 *pins
= info
->groups
[selector
].pins
;
239 *npins
= info
->groups
[selector
].npins
;
244 static int sprd_dt_node_to_map(struct pinctrl_dev
*pctldev
,
245 struct device_node
*np
,
246 struct pinctrl_map
**map
,
247 unsigned int *num_maps
)
249 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
250 const struct sprd_pin_group
*grp
;
251 unsigned long *configs
= NULL
;
252 unsigned int num_configs
= 0;
253 unsigned int reserved_maps
= 0;
254 unsigned int reserve
= 0;
255 const char *function
;
256 enum pinctrl_map_type type
;
259 grp
= sprd_pinctrl_find_group_by_name(pctl
, np
->name
);
261 dev_err(pctl
->dev
, "unable to find group for node %s\n",
262 of_node_full_name(np
));
266 ret
= of_property_count_strings(np
, "pins");
271 type
= PIN_MAP_TYPE_CONFIGS_PIN
;
273 type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
275 ret
= of_property_read_string(np
, "function", &function
);
279 "%s: could not parse property function\n",
280 of_node_full_name(np
));
284 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
287 dev_err(pctl
->dev
, "%s: could not parse node property\n",
288 of_node_full_name(np
));
295 if (function
!= NULL
)
300 ret
= pinctrl_utils_reserve_map(pctldev
, map
, &reserved_maps
,
306 ret
= pinctrl_utils_add_map_mux(pctldev
, map
,
307 &reserved_maps
, num_maps
,
308 grp
->name
, function
);
314 const char *group_or_pin
;
317 if (type
== PIN_MAP_TYPE_CONFIGS_PIN
) {
318 pin_id
= grp
->pins
[0];
319 group_or_pin
= pin_get_name(pctldev
, pin_id
);
321 group_or_pin
= grp
->name
;
324 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
325 &reserved_maps
, num_maps
,
326 group_or_pin
, configs
,
335 static void sprd_pctrl_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
338 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
341 static const struct pinctrl_ops sprd_pctrl_ops
= {
342 .get_groups_count
= sprd_pctrl_group_count
,
343 .get_group_name
= sprd_pctrl_group_name
,
344 .get_group_pins
= sprd_pctrl_group_pins
,
345 .pin_dbg_show
= sprd_pctrl_dbg_show
,
346 .dt_node_to_map
= sprd_dt_node_to_map
,
347 .dt_free_map
= pinctrl_utils_free_map
,
350 static int sprd_pmx_get_function_count(struct pinctrl_dev
*pctldev
)
355 static const char *sprd_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
356 unsigned int selector
)
372 static int sprd_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
373 unsigned int selector
,
374 const char * const **groups
,
375 unsigned int * const num_groups
)
377 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
378 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
380 *groups
= info
->grp_names
;
381 *num_groups
= info
->ngroups
;
386 static int sprd_pmx_set_mux(struct pinctrl_dev
*pctldev
,
387 unsigned int func_selector
,
388 unsigned int group_selector
)
390 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
391 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
392 struct sprd_pin_group
*grp
= &info
->groups
[group_selector
];
393 unsigned int i
, grp_pins
= grp
->npins
;
395 unsigned int val
= 0;
397 if (group_selector
>= info
->ngroups
)
400 switch (func_selector
) {
402 val
&= PIN_FUNC_SEL_1
;
405 val
|= PIN_FUNC_SEL_2
;
408 val
|= PIN_FUNC_SEL_3
;
411 val
|= PIN_FUNC_SEL_4
;
417 for (i
= 0; i
< grp_pins
; i
++) {
418 unsigned int pin_id
= grp
->pins
[i
];
419 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
421 if (!pin
|| pin
->type
!= COMMON_PIN
)
424 reg
= readl((void __iomem
*)pin
->reg
);
425 reg
&= ~PIN_FUNC_MASK
;
427 writel(reg
, (void __iomem
*)pin
->reg
);
433 static const struct pinmux_ops sprd_pmx_ops
= {
434 .get_functions_count
= sprd_pmx_get_function_count
,
435 .get_function_name
= sprd_pmx_get_function_name
,
436 .get_function_groups
= sprd_pmx_get_function_groups
,
437 .set_mux
= sprd_pmx_set_mux
,
440 static int sprd_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
441 unsigned long *config
)
443 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
444 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
445 unsigned int param
= pinconf_to_config_param(*config
);
446 unsigned int reg
, arg
;
451 if (pin
->type
== GLOBAL_CTRL_PIN
) {
452 reg
= (readl((void __iomem
*)pin
->reg
) >>
453 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
455 reg
= readl((void __iomem
*)pin
->reg
);
458 if (pin
->type
== GLOBAL_CTRL_PIN
&&
459 param
== SPRD_PIN_CONFIG_CONTROL
) {
461 } else if (pin
->type
== COMMON_PIN
|| pin
->type
== MISC_PIN
) {
463 case SPRD_PIN_CONFIG_SLEEP_MODE
:
464 arg
= (reg
>> SLEEP_MODE_SHIFT
) & SLEEP_MODE_MASK
;
466 case PIN_CONFIG_INPUT_ENABLE
:
467 arg
= (reg
>> SLEEP_INPUT_SHIFT
) & SLEEP_INPUT_MASK
;
469 case PIN_CONFIG_OUTPUT_ENABLE
:
470 arg
= reg
& SLEEP_OUTPUT_MASK
;
472 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
473 if ((reg
& SLEEP_OUTPUT
) || (reg
& SLEEP_INPUT
))
478 case PIN_CONFIG_DRIVE_STRENGTH
:
479 arg
= (reg
>> DRIVE_STRENGTH_SHIFT
) &
482 case PIN_CONFIG_BIAS_PULL_DOWN
:
483 /* combine sleep pull down and pull down config */
484 arg
= ((reg
>> SLEEP_PULL_DOWN_SHIFT
) &
485 SLEEP_PULL_DOWN_MASK
) << 16;
486 arg
|= (reg
>> PULL_DOWN_SHIFT
) & PULL_DOWN_MASK
;
488 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
489 arg
= (reg
>> INPUT_SCHMITT_SHIFT
) & INPUT_SCHMITT_MASK
;
491 case PIN_CONFIG_BIAS_PULL_UP
:
492 /* combine sleep pull up and pull up config */
493 arg
= ((reg
>> SLEEP_PULL_UP_SHIFT
) &
494 SLEEP_PULL_UP_MASK
) << 16;
495 arg
|= (reg
>> PULL_UP_SHIFT
) & PULL_UP_MASK
;
497 case PIN_CONFIG_BIAS_DISABLE
:
498 if ((reg
& (SLEEP_PULL_DOWN
| SLEEP_PULL_UP
)) ||
499 (reg
& (PULL_DOWN
| PULL_UP_4_7K
| PULL_UP_20K
)))
504 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
514 *config
= pinconf_to_config_packed(param
, arg
);
518 static unsigned int sprd_pinconf_drive(unsigned int mA
)
520 unsigned int val
= 0;
532 val
|= BIT(19) | BIT(20);
538 val
|= BIT(21) | BIT(19);
541 val
|= BIT(21) | BIT(20);
544 val
|= BIT(19) | BIT(20) | BIT(21);
550 val
|= BIT(22) | BIT(19);
553 val
|= BIT(22) | BIT(20);
556 val
|= BIT(22) | BIT(20) | BIT(19);
559 val
|= BIT(22) | BIT(21);
562 val
|= BIT(22) | BIT(21) | BIT(19);
565 val
|= BIT(22) | BIT(21) | BIT(20);
568 val
|= BIT(22) | BIT(21) | BIT(20) | BIT(19);
577 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs
,
578 unsigned int num_configs
)
583 for (i
= 0; i
< num_configs
; i
++) {
584 param
= pinconf_to_config_param(configs
[i
]);
585 if (param
== PIN_CONFIG_SLEEP_HARDWARE_STATE
)
592 static int sprd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
593 unsigned long *configs
, unsigned int num_configs
)
595 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
596 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
597 bool is_sleep_config
;
604 is_sleep_config
= sprd_pinctrl_check_sleep_config(configs
, num_configs
);
606 for (i
= 0; i
< num_configs
; i
++) {
607 unsigned int param
, arg
, shift
, mask
, val
;
609 param
= pinconf_to_config_param(configs
[i
]);
610 arg
= pinconf_to_config_argument(configs
[i
]);
615 if (pin
->type
== GLOBAL_CTRL_PIN
&&
616 param
== SPRD_PIN_CONFIG_CONTROL
) {
618 } else if (pin
->type
== COMMON_PIN
|| pin
->type
== MISC_PIN
) {
620 case SPRD_PIN_CONFIG_SLEEP_MODE
:
622 val
|= AP_SLEEP_MODE
;
623 if (arg
& PUBCP_SLEEP
)
624 val
|= PUBCP_SLEEP_MODE
;
625 if (arg
& TGLDSP_SLEEP
)
626 val
|= TGLDSP_SLEEP_MODE
;
627 if (arg
& AGDSP_SLEEP
)
628 val
|= AGDSP_SLEEP_MODE
;
630 val
|= CM4_SLEEP_MODE
;
632 mask
= SLEEP_MODE_MASK
;
633 shift
= SLEEP_MODE_SHIFT
;
635 case PIN_CONFIG_INPUT_ENABLE
:
636 if (is_sleep_config
== true) {
642 mask
= SLEEP_INPUT_MASK
;
643 shift
= SLEEP_INPUT_SHIFT
;
646 case PIN_CONFIG_OUTPUT_ENABLE
:
647 if (is_sleep_config
== true) {
651 val
&= ~SLEEP_OUTPUT
;
653 mask
= SLEEP_OUTPUT_MASK
;
654 shift
= SLEEP_OUTPUT_SHIFT
;
657 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
658 if (is_sleep_config
== true) {
660 mask
= SLEEP_OUTPUT
| SLEEP_INPUT
;
663 case PIN_CONFIG_DRIVE_STRENGTH
:
664 if (arg
< 2 || arg
> 60)
667 val
= sprd_pinconf_drive(arg
);
668 mask
= DRIVE_STRENGTH_MASK
;
669 shift
= DRIVE_STRENGTH_SHIFT
;
671 case PIN_CONFIG_BIAS_PULL_DOWN
:
672 if (is_sleep_config
== true) {
673 val
|= SLEEP_PULL_DOWN
;
674 mask
= SLEEP_PULL_DOWN_MASK
;
675 shift
= SLEEP_PULL_DOWN_SHIFT
;
678 mask
= PULL_DOWN_MASK
;
679 shift
= PULL_DOWN_SHIFT
;
682 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
684 val
|= INPUT_SCHMITT
;
686 val
&= ~INPUT_SCHMITT
;
688 mask
= INPUT_SCHMITT_MASK
;
689 shift
= INPUT_SCHMITT_SHIFT
;
691 case PIN_CONFIG_BIAS_PULL_UP
:
692 if (is_sleep_config
) {
693 val
|= SLEEP_PULL_UP
;
694 mask
= SLEEP_PULL_UP_MASK
;
695 shift
= SLEEP_PULL_UP_SHIFT
;
699 else if (arg
== 4700)
703 shift
= PULL_UP_SHIFT
;
706 case PIN_CONFIG_BIAS_DISABLE
:
707 if (is_sleep_config
== true) {
709 mask
= SLEEP_PULL_DOWN
| SLEEP_PULL_UP
;
712 mask
= PULL_DOWN
| PULL_UP_20K
|
716 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
725 if (pin
->type
== GLOBAL_CTRL_PIN
) {
726 reg
= readl((void __iomem
*)pin
->reg
);
727 reg
&= ~(PINCTRL_BIT_MASK(pin
->bit_width
)
729 reg
|= (val
& PINCTRL_BIT_MASK(pin
->bit_width
))
731 writel(reg
, (void __iomem
*)pin
->reg
);
733 reg
= readl((void __iomem
*)pin
->reg
);
734 reg
&= ~(mask
<< shift
);
736 writel(reg
, (void __iomem
*)pin
->reg
);
743 static int sprd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
744 unsigned int selector
, unsigned long *config
)
746 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
747 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
748 struct sprd_pin_group
*grp
;
751 if (selector
>= info
->ngroups
)
754 grp
= &info
->groups
[selector
];
755 pin_id
= grp
->pins
[0];
757 return sprd_pinconf_get(pctldev
, pin_id
, config
);
760 static int sprd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
761 unsigned int selector
,
762 unsigned long *configs
,
763 unsigned int num_configs
)
765 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
766 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
767 struct sprd_pin_group
*grp
;
770 if (selector
>= info
->ngroups
)
773 grp
= &info
->groups
[selector
];
775 for (i
= 0; i
< grp
->npins
; i
++) {
776 unsigned int pin_id
= grp
->pins
[i
];
778 ret
= sprd_pinconf_set(pctldev
, pin_id
, configs
, num_configs
);
786 static int sprd_pinconf_get_config(struct pinctrl_dev
*pctldev
,
788 unsigned long *config
)
790 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
791 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
796 if (pin
->type
== GLOBAL_CTRL_PIN
) {
797 *config
= (readl((void __iomem
*)pin
->reg
) >>
798 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
800 *config
= readl((void __iomem
*)pin
->reg
);
806 static void sprd_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
807 struct seq_file
*s
, unsigned int pin_id
)
809 unsigned long config
;
812 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
816 seq_printf(s
, "0x%lx", config
);
819 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
821 unsigned int selector
)
823 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
824 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
825 struct sprd_pin_group
*grp
;
826 unsigned long config
;
830 if (selector
>= info
->ngroups
)
833 grp
= &info
->groups
[selector
];
836 for (i
= 0; i
< grp
->npins
; i
++, config
++) {
837 unsigned int pin_id
= grp
->pins
[i
];
839 name
= pin_get_name(pctldev
, pin_id
);
840 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
844 seq_printf(s
, "%s: 0x%lx ", name
, config
);
848 static const struct pinconf_ops sprd_pinconf_ops
= {
850 .pin_config_get
= sprd_pinconf_get
,
851 .pin_config_set
= sprd_pinconf_set
,
852 .pin_config_group_get
= sprd_pinconf_group_get
,
853 .pin_config_group_set
= sprd_pinconf_group_set
,
854 .pin_config_dbg_show
= sprd_pinconf_dbg_show
,
855 .pin_config_group_dbg_show
= sprd_pinconf_group_dbg_show
,
858 static const struct pinconf_generic_params sprd_dt_params
[] = {
859 {"sprd,control", SPRD_PIN_CONFIG_CONTROL
, 0},
860 {"sprd,sleep-mode", SPRD_PIN_CONFIG_SLEEP_MODE
, 0},
863 #ifdef CONFIG_DEBUG_FS
864 static const struct pin_config_item sprd_conf_items
[] = {
865 PCONFDUMP(SPRD_PIN_CONFIG_CONTROL
, "global control", NULL
, true),
866 PCONFDUMP(SPRD_PIN_CONFIG_SLEEP_MODE
, "sleep mode", NULL
, true),
870 static struct pinctrl_desc sprd_pinctrl_desc
= {
871 .pctlops
= &sprd_pctrl_ops
,
872 .pmxops
= &sprd_pmx_ops
,
873 .confops
= &sprd_pinconf_ops
,
874 .num_custom_params
= ARRAY_SIZE(sprd_dt_params
),
875 .custom_params
= sprd_dt_params
,
876 #ifdef CONFIG_DEBUG_FS
877 .custom_conf_items
= sprd_conf_items
,
879 .owner
= THIS_MODULE
,
882 static int sprd_pinctrl_parse_groups(struct device_node
*np
,
883 struct sprd_pinctrl
*sprd_pctl
,
884 struct sprd_pin_group
*grp
)
886 struct property
*prop
;
887 const char *pin_name
;
890 ret
= of_property_count_strings(np
, "pins");
894 grp
->name
= np
->name
;
896 grp
->pins
= devm_kcalloc(sprd_pctl
->dev
,
897 grp
->npins
, sizeof(unsigned int),
902 of_property_for_each_string(np
, "pins", prop
, pin_name
) {
903 ret
= sprd_pinctrl_get_id_by_name(sprd_pctl
, pin_name
);
905 grp
->pins
[i
++] = ret
;
908 for (i
= 0; i
< grp
->npins
; i
++) {
909 dev_dbg(sprd_pctl
->dev
,
910 "Group[%s] contains [%d] pins: id = %d\n",
911 grp
->name
, grp
->npins
, grp
->pins
[i
]);
917 static unsigned int sprd_pinctrl_get_groups(struct device_node
*np
)
919 struct device_node
*child
;
920 unsigned int group_cnt
, cnt
;
922 group_cnt
= of_get_child_count(np
);
924 for_each_child_of_node(np
, child
) {
925 cnt
= of_get_child_count(child
);
933 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl
*sprd_pctl
)
935 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
936 struct device_node
*np
= sprd_pctl
->dev
->of_node
;
937 struct sprd_pin_group
*grp
;
944 info
->ngroups
= sprd_pinctrl_get_groups(np
);
948 info
->groups
= devm_kcalloc(sprd_pctl
->dev
,
950 sizeof(struct sprd_pin_group
),
955 info
->grp_names
= devm_kcalloc(sprd_pctl
->dev
,
956 info
->ngroups
, sizeof(char *),
958 if (!info
->grp_names
)
961 temp
= info
->grp_names
;
964 for_each_child_of_node_scoped(np
, child
) {
965 ret
= sprd_pinctrl_parse_groups(child
, sprd_pctl
, grp
);
972 if (of_get_child_count(child
) > 0) {
973 for_each_child_of_node_scoped(child
, sub_child
) {
974 ret
= sprd_pinctrl_parse_groups(sub_child
,
988 static int sprd_pinctrl_add_pins(struct sprd_pinctrl
*sprd_pctl
,
989 struct sprd_pins_info
*sprd_soc_pin_info
,
992 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
993 unsigned int ctrl_pin
= 0, com_pin
= 0;
994 struct sprd_pin
*pin
;
997 info
->npins
= pins_cnt
;
998 info
->pins
= devm_kcalloc(sprd_pctl
->dev
,
999 info
->npins
, sizeof(struct sprd_pin
),
1004 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; i
++, pin
++) {
1007 pin
->name
= sprd_soc_pin_info
[i
].name
;
1008 pin
->type
= sprd_soc_pin_info
[i
].type
;
1009 pin
->number
= sprd_soc_pin_info
[i
].num
;
1010 reg
= sprd_soc_pin_info
[i
].reg
;
1011 if (pin
->type
== GLOBAL_CTRL_PIN
) {
1012 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1013 PINCTRL_REG_LEN
* reg
;
1014 pin
->bit_offset
= sprd_soc_pin_info
[i
].bit_offset
;
1015 pin
->bit_width
= sprd_soc_pin_info
[i
].bit_width
;
1017 } else if (pin
->type
== COMMON_PIN
) {
1018 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1019 PINCTRL_REG_OFFSET
+ PINCTRL_REG_LEN
*
1022 } else if (pin
->type
== MISC_PIN
) {
1023 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1024 PINCTRL_REG_MISC_OFFSET
+ PINCTRL_REG_LEN
*
1025 (i
- ctrl_pin
- com_pin
);
1029 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; pin
++, i
++) {
1030 dev_dbg(sprd_pctl
->dev
, "pin name[%s-%d], type = %d, "
1031 "bit offset = %ld, bit width = %ld, reg = 0x%lx\n",
1032 pin
->name
, pin
->number
, pin
->type
,
1033 pin
->bit_offset
, pin
->bit_width
, pin
->reg
);
1039 int sprd_pinctrl_core_probe(struct platform_device
*pdev
,
1040 struct sprd_pins_info
*sprd_soc_pin_info
,
1043 struct sprd_pinctrl
*sprd_pctl
;
1044 struct sprd_pinctrl_soc_info
*pinctrl_info
;
1045 struct pinctrl_pin_desc
*pin_desc
;
1048 sprd_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct sprd_pinctrl
),
1053 sprd_pctl
->base
= devm_platform_ioremap_resource(pdev
, 0);
1054 if (IS_ERR(sprd_pctl
->base
))
1055 return PTR_ERR(sprd_pctl
->base
);
1057 pinctrl_info
= devm_kzalloc(&pdev
->dev
,
1058 sizeof(struct sprd_pinctrl_soc_info
),
1063 sprd_pctl
->info
= pinctrl_info
;
1064 sprd_pctl
->dev
= &pdev
->dev
;
1065 platform_set_drvdata(pdev
, sprd_pctl
);
1067 ret
= sprd_pinctrl_add_pins(sprd_pctl
, sprd_soc_pin_info
, pins_cnt
);
1069 dev_err(&pdev
->dev
, "fail to add pins information\n");
1073 ret
= sprd_pinctrl_parse_dt(sprd_pctl
);
1075 dev_err(&pdev
->dev
, "fail to parse dt properties\n");
1079 pin_desc
= devm_kcalloc(&pdev
->dev
,
1080 pinctrl_info
->npins
,
1081 sizeof(struct pinctrl_pin_desc
),
1086 for (i
= 0; i
< pinctrl_info
->npins
; i
++) {
1087 pin_desc
[i
].number
= pinctrl_info
->pins
[i
].number
;
1088 pin_desc
[i
].name
= pinctrl_info
->pins
[i
].name
;
1089 pin_desc
[i
].drv_data
= pinctrl_info
;
1092 sprd_pinctrl_desc
.pins
= pin_desc
;
1093 sprd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1094 sprd_pinctrl_desc
.npins
= pinctrl_info
->npins
;
1096 sprd_pctl
->pctl
= pinctrl_register(&sprd_pinctrl_desc
,
1097 &pdev
->dev
, (void *)sprd_pctl
);
1098 if (IS_ERR(sprd_pctl
->pctl
)) {
1099 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
1100 return PTR_ERR(sprd_pctl
->pctl
);
1105 EXPORT_SYMBOL_GPL(sprd_pinctrl_core_probe
);
1107 void sprd_pinctrl_remove(struct platform_device
*pdev
)
1109 struct sprd_pinctrl
*sprd_pctl
= platform_get_drvdata(pdev
);
1111 pinctrl_unregister(sprd_pctl
->pctl
);
1113 EXPORT_SYMBOL_GPL(sprd_pinctrl_remove
);
1115 void sprd_pinctrl_shutdown(struct platform_device
*pdev
)
1117 struct pinctrl
*pinctl
;
1118 struct pinctrl_state
*state
;
1120 pinctl
= devm_pinctrl_get(&pdev
->dev
);
1123 state
= pinctrl_lookup_state(pinctl
, "shutdown");
1126 pinctrl_select_state(pinctl
, state
);
1128 EXPORT_SYMBOL_GPL(sprd_pinctrl_shutdown
);
1130 MODULE_DESCRIPTION("SPREADTRUM Pin Controller Driver");
1131 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1132 MODULE_LICENSE("GPL v2");