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/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/slab.h>
24 #include "../pinmux.h"
25 #include "../pinconf.h"
26 #include "../pinctrl-utils.h"
27 #include "pinctrl-sprd.h"
29 #define PINCTRL_BIT_MASK(width) (~(~0UL << (width)))
30 #define PINCTRL_REG_OFFSET 0x20
31 #define PINCTRL_REG_MISC_OFFSET 0x4020
32 #define PINCTRL_REG_LEN 0x4
34 #define PIN_FUNC_MASK (BIT(4) | BIT(5))
35 #define PIN_FUNC_SEL_1 ~PIN_FUNC_MASK
36 #define PIN_FUNC_SEL_2 BIT(4)
37 #define PIN_FUNC_SEL_3 BIT(5)
38 #define PIN_FUNC_SEL_4 PIN_FUNC_MASK
40 #define AP_SLEEP_MODE BIT(13)
41 #define PUBCP_SLEEP_MODE BIT(14)
42 #define TGLDSP_SLEEP_MODE BIT(15)
43 #define AGDSP_SLEEP_MODE BIT(16)
44 #define CM4_SLEEP_MODE BIT(17)
45 #define SLEEP_MODE_MASK GENMASK(5, 0)
46 #define SLEEP_MODE_SHIFT 13
48 #define SLEEP_INPUT BIT(1)
49 #define SLEEP_INPUT_MASK 0x1
50 #define SLEEP_INPUT_SHIFT 1
52 #define SLEEP_OUTPUT BIT(0)
53 #define SLEEP_OUTPUT_MASK 0x1
54 #define SLEEP_OUTPUT_SHIFT 0
56 #define DRIVE_STRENGTH_MASK GENMASK(3, 0)
57 #define DRIVE_STRENGTH_SHIFT 19
59 #define SLEEP_PULL_DOWN BIT(2)
60 #define SLEEP_PULL_DOWN_MASK 0x1
61 #define SLEEP_PULL_DOWN_SHIFT 2
63 #define PULL_DOWN BIT(6)
64 #define PULL_DOWN_MASK 0x1
65 #define PULL_DOWN_SHIFT 6
67 #define SLEEP_PULL_UP BIT(3)
68 #define SLEEP_PULL_UP_MASK 0x1
69 #define SLEEP_PULL_UP_SHIFT 3
71 #define PULL_UP_20K (BIT(12) | BIT(7))
72 #define PULL_UP_4_7K BIT(12)
73 #define PULL_UP_MASK 0x21
74 #define PULL_UP_SHIFT 7
76 #define INPUT_SCHMITT BIT(11)
77 #define INPUT_SCHMITT_MASK 0x1
78 #define INPUT_SCHMITT_SHIFT 11
83 TGLDSP_SLEEP
= BIT(2),
97 * struct sprd_pin: represent one pin's description
100 * @type: pin type, can be GLOBAL_CTRL_PIN/COMMON_PIN/MISC_PIN
101 * @reg: pin register address
102 * @bit_offset: bit offset in pin register
103 * @bit_width: bit width in pin register
110 unsigned long bit_offset
;
111 unsigned long bit_width
;
115 * struct sprd_pin_group: represent one group's description
117 * @npins: pin numbers of this group
118 * @pins: pointer to pins array
120 struct sprd_pin_group
{
127 * struct sprd_pinctrl_soc_info: represent the SoC's pins description
128 * @groups: pointer to groups of pins
129 * @ngroups: group numbers of the whole SoC
130 * @pins: pointer to pins description
131 * @npins: pin numbers of the whole SoC
132 * @grp_names: pointer to group names array
134 struct sprd_pinctrl_soc_info
{
135 struct sprd_pin_group
*groups
;
136 unsigned int ngroups
;
137 struct sprd_pin
*pins
;
139 const char **grp_names
;
143 * struct sprd_pinctrl: represent the pin controller device
144 * @dev: pointer to the device structure
145 * @pctl: pointer to the pinctrl handle
146 * @base: base address of the controller
147 * @info: pointer to SoC's pins description information
149 struct sprd_pinctrl
{
151 struct pinctrl_dev
*pctl
;
153 struct sprd_pinctrl_soc_info
*info
;
156 #define SPRD_PIN_CONFIG_CONTROL (PIN_CONFIG_END + 1)
157 #define SPRD_PIN_CONFIG_SLEEP_MODE (PIN_CONFIG_END + 2)
159 static int sprd_pinctrl_get_id_by_name(struct sprd_pinctrl
*sprd_pctl
,
162 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
165 for (i
= 0; i
< info
->npins
; i
++) {
166 if (!strcmp(info
->pins
[i
].name
, name
))
167 return info
->pins
[i
].number
;
173 static struct sprd_pin
*
174 sprd_pinctrl_get_pin_by_id(struct sprd_pinctrl
*sprd_pctl
, unsigned int id
)
176 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
177 struct sprd_pin
*pin
= NULL
;
180 for (i
= 0; i
< info
->npins
; i
++) {
181 if (info
->pins
[i
].number
== id
) {
182 pin
= &info
->pins
[i
];
190 static const struct sprd_pin_group
*
191 sprd_pinctrl_find_group_by_name(struct sprd_pinctrl
*sprd_pctl
,
194 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
195 const struct sprd_pin_group
*grp
= NULL
;
198 for (i
= 0; i
< info
->ngroups
; i
++) {
199 if (!strcmp(info
->groups
[i
].name
, name
)) {
200 grp
= &info
->groups
[i
];
208 static int sprd_pctrl_group_count(struct pinctrl_dev
*pctldev
)
210 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
211 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
213 return info
->ngroups
;
216 static const char *sprd_pctrl_group_name(struct pinctrl_dev
*pctldev
,
217 unsigned int selector
)
219 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
220 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
222 return info
->groups
[selector
].name
;
225 static int sprd_pctrl_group_pins(struct pinctrl_dev
*pctldev
,
226 unsigned int selector
,
227 const unsigned int **pins
,
230 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
231 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
233 if (selector
>= info
->ngroups
)
236 *pins
= info
->groups
[selector
].pins
;
237 *npins
= info
->groups
[selector
].npins
;
242 static int sprd_dt_node_to_map(struct pinctrl_dev
*pctldev
,
243 struct device_node
*np
,
244 struct pinctrl_map
**map
,
245 unsigned int *num_maps
)
247 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
248 const struct sprd_pin_group
*grp
;
249 unsigned long *configs
= NULL
;
250 unsigned int num_configs
= 0;
251 unsigned int reserved_maps
= 0;
252 unsigned int reserve
= 0;
253 const char *function
;
254 enum pinctrl_map_type type
;
257 grp
= sprd_pinctrl_find_group_by_name(pctl
, np
->name
);
259 dev_err(pctl
->dev
, "unable to find group for node %s\n",
260 of_node_full_name(np
));
264 ret
= of_property_count_strings(np
, "pins");
269 type
= PIN_MAP_TYPE_CONFIGS_PIN
;
271 type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
273 ret
= of_property_read_string(np
, "function", &function
);
277 "%s: could not parse property function\n",
278 of_node_full_name(np
));
282 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
285 dev_err(pctl
->dev
, "%s: could not parse node property\n",
286 of_node_full_name(np
));
293 if (function
!= NULL
)
298 ret
= pinctrl_utils_reserve_map(pctldev
, map
, &reserved_maps
,
304 ret
= pinctrl_utils_add_map_mux(pctldev
, map
,
305 &reserved_maps
, num_maps
,
306 grp
->name
, function
);
312 const char *group_or_pin
;
315 if (type
== PIN_MAP_TYPE_CONFIGS_PIN
) {
316 pin_id
= grp
->pins
[0];
317 group_or_pin
= pin_get_name(pctldev
, pin_id
);
319 group_or_pin
= grp
->name
;
322 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
323 &reserved_maps
, num_maps
,
324 group_or_pin
, configs
,
333 static void sprd_pctrl_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
336 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
339 static const struct pinctrl_ops sprd_pctrl_ops
= {
340 .get_groups_count
= sprd_pctrl_group_count
,
341 .get_group_name
= sprd_pctrl_group_name
,
342 .get_group_pins
= sprd_pctrl_group_pins
,
343 .pin_dbg_show
= sprd_pctrl_dbg_show
,
344 .dt_node_to_map
= sprd_dt_node_to_map
,
345 .dt_free_map
= pinctrl_utils_free_map
,
348 static int sprd_pmx_get_function_count(struct pinctrl_dev
*pctldev
)
353 static const char *sprd_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
354 unsigned int selector
)
370 static int sprd_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
371 unsigned int selector
,
372 const char * const **groups
,
373 unsigned int * const num_groups
)
375 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
376 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
378 *groups
= info
->grp_names
;
379 *num_groups
= info
->ngroups
;
384 static int sprd_pmx_set_mux(struct pinctrl_dev
*pctldev
,
385 unsigned int func_selector
,
386 unsigned int group_selector
)
388 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
389 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
390 struct sprd_pin_group
*grp
= &info
->groups
[group_selector
];
391 unsigned int i
, grp_pins
= grp
->npins
;
393 unsigned int val
= 0;
395 if (group_selector
>= info
->ngroups
)
398 switch (func_selector
) {
400 val
&= PIN_FUNC_SEL_1
;
403 val
|= PIN_FUNC_SEL_2
;
406 val
|= PIN_FUNC_SEL_3
;
409 val
|= PIN_FUNC_SEL_4
;
415 for (i
= 0; i
< grp_pins
; i
++) {
416 unsigned int pin_id
= grp
->pins
[i
];
417 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
419 if (!pin
|| pin
->type
!= COMMON_PIN
)
422 reg
= readl((void __iomem
*)pin
->reg
);
423 reg
&= ~PIN_FUNC_MASK
;
425 writel(reg
, (void __iomem
*)pin
->reg
);
431 static const struct pinmux_ops sprd_pmx_ops
= {
432 .get_functions_count
= sprd_pmx_get_function_count
,
433 .get_function_name
= sprd_pmx_get_function_name
,
434 .get_function_groups
= sprd_pmx_get_function_groups
,
435 .set_mux
= sprd_pmx_set_mux
,
438 static int sprd_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
439 unsigned long *config
)
441 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
442 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
443 unsigned int param
= pinconf_to_config_param(*config
);
444 unsigned int reg
, arg
;
449 if (pin
->type
== GLOBAL_CTRL_PIN
) {
450 reg
= (readl((void __iomem
*)pin
->reg
) >>
451 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
453 reg
= readl((void __iomem
*)pin
->reg
);
456 if (pin
->type
== GLOBAL_CTRL_PIN
&&
457 param
== SPRD_PIN_CONFIG_CONTROL
) {
459 } else if (pin
->type
== COMMON_PIN
|| pin
->type
== MISC_PIN
) {
461 case SPRD_PIN_CONFIG_SLEEP_MODE
:
462 arg
= (reg
>> SLEEP_MODE_SHIFT
) & SLEEP_MODE_MASK
;
464 case PIN_CONFIG_INPUT_ENABLE
:
465 arg
= (reg
>> SLEEP_INPUT_SHIFT
) & SLEEP_INPUT_MASK
;
467 case PIN_CONFIG_OUTPUT
:
468 arg
= reg
& SLEEP_OUTPUT_MASK
;
470 case PIN_CONFIG_DRIVE_STRENGTH
:
471 arg
= (reg
>> DRIVE_STRENGTH_SHIFT
) &
474 case PIN_CONFIG_BIAS_PULL_DOWN
:
475 /* combine sleep pull down and pull down config */
476 arg
= ((reg
>> SLEEP_PULL_DOWN_SHIFT
) &
477 SLEEP_PULL_DOWN_MASK
) << 16;
478 arg
|= (reg
>> PULL_DOWN_SHIFT
) & PULL_DOWN_MASK
;
480 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
481 arg
= (reg
>> INPUT_SCHMITT_SHIFT
) & INPUT_SCHMITT_MASK
;
483 case PIN_CONFIG_BIAS_PULL_UP
:
484 /* combine sleep pull up and pull up config */
485 arg
= ((reg
>> SLEEP_PULL_UP_SHIFT
) &
486 SLEEP_PULL_UP_MASK
) << 16;
487 arg
|= (reg
>> PULL_UP_SHIFT
) & PULL_UP_MASK
;
489 case PIN_CONFIG_BIAS_DISABLE
:
490 if ((reg
& (SLEEP_PULL_DOWN
| SLEEP_PULL_UP
)) ||
491 (reg
& (PULL_DOWN
| PULL_UP_4_7K
| PULL_UP_20K
)))
496 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
506 *config
= pinconf_to_config_packed(param
, arg
);
510 static unsigned int sprd_pinconf_drive(unsigned int mA
)
512 unsigned int val
= 0;
524 val
|= BIT(19) | BIT(20);
530 val
|= BIT(21) | BIT(19);
533 val
|= BIT(21) | BIT(20);
536 val
|= BIT(19) | BIT(20) | BIT(21);
542 val
|= BIT(22) | BIT(19);
545 val
|= BIT(22) | BIT(20);
548 val
|= BIT(22) | BIT(20) | BIT(19);
551 val
|= BIT(22) | BIT(21);
554 val
|= BIT(22) | BIT(21) | BIT(19);
557 val
|= BIT(22) | BIT(21) | BIT(20);
560 val
|= BIT(22) | BIT(21) | BIT(20) | BIT(19);
569 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs
,
570 unsigned int num_configs
)
575 for (i
= 0; i
< num_configs
; i
++) {
576 param
= pinconf_to_config_param(configs
[i
]);
577 if (param
== PIN_CONFIG_SLEEP_HARDWARE_STATE
)
584 static int sprd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
585 unsigned long *configs
, unsigned int num_configs
)
587 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
588 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
589 bool is_sleep_config
;
596 is_sleep_config
= sprd_pinctrl_check_sleep_config(configs
, num_configs
);
598 for (i
= 0; i
< num_configs
; i
++) {
599 unsigned int param
, arg
, shift
, mask
, val
;
601 param
= pinconf_to_config_param(configs
[i
]);
602 arg
= pinconf_to_config_argument(configs
[i
]);
607 if (pin
->type
== GLOBAL_CTRL_PIN
&&
608 param
== SPRD_PIN_CONFIG_CONTROL
) {
610 } else if (pin
->type
== COMMON_PIN
|| pin
->type
== MISC_PIN
) {
612 case SPRD_PIN_CONFIG_SLEEP_MODE
:
614 val
|= AP_SLEEP_MODE
;
615 if (arg
& PUBCP_SLEEP
)
616 val
|= PUBCP_SLEEP_MODE
;
617 if (arg
& TGLDSP_SLEEP
)
618 val
|= TGLDSP_SLEEP_MODE
;
619 if (arg
& AGDSP_SLEEP
)
620 val
|= AGDSP_SLEEP_MODE
;
622 val
|= CM4_SLEEP_MODE
;
624 mask
= SLEEP_MODE_MASK
;
625 shift
= SLEEP_MODE_SHIFT
;
627 case PIN_CONFIG_INPUT_ENABLE
:
628 if (is_sleep_config
== true) {
634 mask
= SLEEP_INPUT_MASK
;
635 shift
= SLEEP_INPUT_SHIFT
;
638 case PIN_CONFIG_OUTPUT
:
639 if (is_sleep_config
== true) {
641 mask
= SLEEP_OUTPUT_MASK
;
642 shift
= SLEEP_OUTPUT_SHIFT
;
645 case PIN_CONFIG_DRIVE_STRENGTH
:
646 if (arg
< 2 || arg
> 60)
649 val
= sprd_pinconf_drive(arg
);
650 mask
= DRIVE_STRENGTH_MASK
;
651 shift
= DRIVE_STRENGTH_SHIFT
;
653 case PIN_CONFIG_BIAS_PULL_DOWN
:
654 if (is_sleep_config
== true) {
655 val
|= SLEEP_PULL_DOWN
;
656 mask
= SLEEP_PULL_DOWN_MASK
;
657 shift
= SLEEP_PULL_DOWN_SHIFT
;
660 mask
= PULL_DOWN_MASK
;
661 shift
= PULL_DOWN_SHIFT
;
664 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
666 val
|= INPUT_SCHMITT
;
668 val
&= ~INPUT_SCHMITT
;
670 mask
= INPUT_SCHMITT_MASK
;
671 shift
= INPUT_SCHMITT_SHIFT
;
673 case PIN_CONFIG_BIAS_PULL_UP
:
674 if (is_sleep_config
== true) {
675 val
|= SLEEP_PULL_UP
;
676 mask
= SLEEP_PULL_UP_MASK
;
677 shift
= SLEEP_PULL_UP_SHIFT
;
681 else if (arg
== 4700)
685 shift
= PULL_UP_SHIFT
;
688 case PIN_CONFIG_BIAS_DISABLE
:
689 if (is_sleep_config
== true) {
691 mask
= SLEEP_PULL_DOWN
| SLEEP_PULL_UP
;
694 mask
= PULL_DOWN
| PULL_UP_20K
|
698 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
707 if (pin
->type
== GLOBAL_CTRL_PIN
) {
708 reg
= readl((void __iomem
*)pin
->reg
);
709 reg
&= ~(PINCTRL_BIT_MASK(pin
->bit_width
)
711 reg
|= (val
& PINCTRL_BIT_MASK(pin
->bit_width
))
713 writel(reg
, (void __iomem
*)pin
->reg
);
715 reg
= readl((void __iomem
*)pin
->reg
);
716 reg
&= ~(mask
<< shift
);
718 writel(reg
, (void __iomem
*)pin
->reg
);
725 static int sprd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
726 unsigned int selector
, unsigned long *config
)
728 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
729 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
730 struct sprd_pin_group
*grp
;
733 if (selector
>= info
->ngroups
)
736 grp
= &info
->groups
[selector
];
737 pin_id
= grp
->pins
[0];
739 return sprd_pinconf_get(pctldev
, pin_id
, config
);
742 static int sprd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
743 unsigned int selector
,
744 unsigned long *configs
,
745 unsigned int num_configs
)
747 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
748 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
749 struct sprd_pin_group
*grp
;
752 if (selector
>= info
->ngroups
)
755 grp
= &info
->groups
[selector
];
757 for (i
= 0; i
< grp
->npins
; i
++) {
758 unsigned int pin_id
= grp
->pins
[i
];
760 ret
= sprd_pinconf_set(pctldev
, pin_id
, configs
, num_configs
);
768 static int sprd_pinconf_get_config(struct pinctrl_dev
*pctldev
,
770 unsigned long *config
)
772 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
773 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
778 if (pin
->type
== GLOBAL_CTRL_PIN
) {
779 *config
= (readl((void __iomem
*)pin
->reg
) >>
780 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
782 *config
= readl((void __iomem
*)pin
->reg
);
788 static void sprd_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
789 struct seq_file
*s
, unsigned int pin_id
)
791 unsigned long config
;
794 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
798 seq_printf(s
, "0x%lx", config
);
801 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
803 unsigned int selector
)
805 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
806 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
807 struct sprd_pin_group
*grp
;
808 unsigned long config
;
812 if (selector
>= info
->ngroups
)
815 grp
= &info
->groups
[selector
];
818 for (i
= 0; i
< grp
->npins
; i
++, config
++) {
819 unsigned int pin_id
= grp
->pins
[i
];
821 name
= pin_get_name(pctldev
, pin_id
);
822 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
826 seq_printf(s
, "%s: 0x%lx ", name
, config
);
830 static const struct pinconf_ops sprd_pinconf_ops
= {
832 .pin_config_get
= sprd_pinconf_get
,
833 .pin_config_set
= sprd_pinconf_set
,
834 .pin_config_group_get
= sprd_pinconf_group_get
,
835 .pin_config_group_set
= sprd_pinconf_group_set
,
836 .pin_config_dbg_show
= sprd_pinconf_dbg_show
,
837 .pin_config_group_dbg_show
= sprd_pinconf_group_dbg_show
,
840 static const struct pinconf_generic_params sprd_dt_params
[] = {
841 {"sprd,control", SPRD_PIN_CONFIG_CONTROL
, 0},
842 {"sprd,sleep-mode", SPRD_PIN_CONFIG_SLEEP_MODE
, 0},
845 #ifdef CONFIG_DEBUG_FS
846 static const struct pin_config_item sprd_conf_items
[] = {
847 PCONFDUMP(SPRD_PIN_CONFIG_CONTROL
, "global control", NULL
, true),
848 PCONFDUMP(SPRD_PIN_CONFIG_SLEEP_MODE
, "sleep mode", NULL
, true),
852 static struct pinctrl_desc sprd_pinctrl_desc
= {
853 .pctlops
= &sprd_pctrl_ops
,
854 .pmxops
= &sprd_pmx_ops
,
855 .confops
= &sprd_pinconf_ops
,
856 .num_custom_params
= ARRAY_SIZE(sprd_dt_params
),
857 .custom_params
= sprd_dt_params
,
858 #ifdef CONFIG_DEBUG_FS
859 .custom_conf_items
= sprd_conf_items
,
861 .owner
= THIS_MODULE
,
864 static int sprd_pinctrl_parse_groups(struct device_node
*np
,
865 struct sprd_pinctrl
*sprd_pctl
,
866 struct sprd_pin_group
*grp
)
868 struct property
*prop
;
869 const char *pin_name
;
872 ret
= of_property_count_strings(np
, "pins");
876 grp
->name
= np
->name
;
878 grp
->pins
= devm_kcalloc(sprd_pctl
->dev
,
879 grp
->npins
, sizeof(unsigned int),
884 of_property_for_each_string(np
, "pins", prop
, pin_name
) {
885 ret
= sprd_pinctrl_get_id_by_name(sprd_pctl
, pin_name
);
887 grp
->pins
[i
++] = ret
;
890 for (i
= 0; i
< grp
->npins
; i
++) {
891 dev_dbg(sprd_pctl
->dev
,
892 "Group[%s] contains [%d] pins: id = %d\n",
893 grp
->name
, grp
->npins
, grp
->pins
[i
]);
899 static unsigned int sprd_pinctrl_get_groups(struct device_node
*np
)
901 struct device_node
*child
;
902 unsigned int group_cnt
, cnt
;
904 group_cnt
= of_get_child_count(np
);
906 for_each_child_of_node(np
, child
) {
907 cnt
= of_get_child_count(child
);
915 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl
*sprd_pctl
)
917 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
918 struct device_node
*np
= sprd_pctl
->dev
->of_node
;
919 struct device_node
*child
, *sub_child
;
920 struct sprd_pin_group
*grp
;
927 info
->ngroups
= sprd_pinctrl_get_groups(np
);
931 info
->groups
= devm_kcalloc(sprd_pctl
->dev
,
933 sizeof(struct sprd_pin_group
),
938 info
->grp_names
= devm_kcalloc(sprd_pctl
->dev
,
939 info
->ngroups
, sizeof(char *),
941 if (!info
->grp_names
)
944 temp
= info
->grp_names
;
947 for_each_child_of_node(np
, child
) {
948 ret
= sprd_pinctrl_parse_groups(child
, sprd_pctl
, grp
);
957 if (of_get_child_count(child
) > 0) {
958 for_each_child_of_node(child
, sub_child
) {
959 ret
= sprd_pinctrl_parse_groups(sub_child
,
962 of_node_put(sub_child
);
976 static int sprd_pinctrl_add_pins(struct sprd_pinctrl
*sprd_pctl
,
977 struct sprd_pins_info
*sprd_soc_pin_info
,
980 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
981 unsigned int ctrl_pin
= 0, com_pin
= 0;
982 struct sprd_pin
*pin
;
985 info
->npins
= pins_cnt
;
986 info
->pins
= devm_kcalloc(sprd_pctl
->dev
,
987 info
->npins
, sizeof(struct sprd_pin
),
992 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; i
++, pin
++) {
995 pin
->name
= sprd_soc_pin_info
[i
].name
;
996 pin
->type
= sprd_soc_pin_info
[i
].type
;
997 pin
->number
= sprd_soc_pin_info
[i
].num
;
998 reg
= sprd_soc_pin_info
[i
].reg
;
999 if (pin
->type
== GLOBAL_CTRL_PIN
) {
1000 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1001 PINCTRL_REG_LEN
* reg
;
1002 pin
->bit_offset
= sprd_soc_pin_info
[i
].bit_offset
;
1003 pin
->bit_width
= sprd_soc_pin_info
[i
].bit_width
;
1005 } else if (pin
->type
== COMMON_PIN
) {
1006 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1007 PINCTRL_REG_OFFSET
+ PINCTRL_REG_LEN
*
1010 } else if (pin
->type
== MISC_PIN
) {
1011 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1012 PINCTRL_REG_MISC_OFFSET
+ PINCTRL_REG_LEN
*
1013 (i
- ctrl_pin
- com_pin
);
1017 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; pin
++, i
++) {
1018 dev_dbg(sprd_pctl
->dev
, "pin name[%s-%d], type = %d, "
1019 "bit offset = %ld, bit width = %ld, reg = 0x%lx\n",
1020 pin
->name
, pin
->number
, pin
->type
,
1021 pin
->bit_offset
, pin
->bit_width
, pin
->reg
);
1027 int sprd_pinctrl_core_probe(struct platform_device
*pdev
,
1028 struct sprd_pins_info
*sprd_soc_pin_info
,
1031 struct sprd_pinctrl
*sprd_pctl
;
1032 struct sprd_pinctrl_soc_info
*pinctrl_info
;
1033 struct pinctrl_pin_desc
*pin_desc
;
1036 sprd_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct sprd_pinctrl
),
1041 sprd_pctl
->base
= devm_platform_ioremap_resource(pdev
, 0);
1042 if (IS_ERR(sprd_pctl
->base
))
1043 return PTR_ERR(sprd_pctl
->base
);
1045 pinctrl_info
= devm_kzalloc(&pdev
->dev
,
1046 sizeof(struct sprd_pinctrl_soc_info
),
1051 sprd_pctl
->info
= pinctrl_info
;
1052 sprd_pctl
->dev
= &pdev
->dev
;
1053 platform_set_drvdata(pdev
, sprd_pctl
);
1055 ret
= sprd_pinctrl_add_pins(sprd_pctl
, sprd_soc_pin_info
, pins_cnt
);
1057 dev_err(&pdev
->dev
, "fail to add pins information\n");
1061 ret
= sprd_pinctrl_parse_dt(sprd_pctl
);
1063 dev_err(&pdev
->dev
, "fail to parse dt properties\n");
1067 pin_desc
= devm_kcalloc(&pdev
->dev
,
1068 pinctrl_info
->npins
,
1069 sizeof(struct pinctrl_pin_desc
),
1074 for (i
= 0; i
< pinctrl_info
->npins
; i
++) {
1075 pin_desc
[i
].number
= pinctrl_info
->pins
[i
].number
;
1076 pin_desc
[i
].name
= pinctrl_info
->pins
[i
].name
;
1077 pin_desc
[i
].drv_data
= pinctrl_info
;
1080 sprd_pinctrl_desc
.pins
= pin_desc
;
1081 sprd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1082 sprd_pinctrl_desc
.npins
= pinctrl_info
->npins
;
1084 sprd_pctl
->pctl
= pinctrl_register(&sprd_pinctrl_desc
,
1085 &pdev
->dev
, (void *)sprd_pctl
);
1086 if (IS_ERR(sprd_pctl
->pctl
)) {
1087 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
1088 return PTR_ERR(sprd_pctl
->pctl
);
1094 int sprd_pinctrl_remove(struct platform_device
*pdev
)
1096 struct sprd_pinctrl
*sprd_pctl
= platform_get_drvdata(pdev
);
1098 pinctrl_unregister(sprd_pctl
->pctl
);
1102 void sprd_pinctrl_shutdown(struct platform_device
*pdev
)
1104 struct pinctrl
*pinctl
;
1105 struct pinctrl_state
*state
;
1107 pinctl
= devm_pinctrl_get(&pdev
->dev
);
1110 state
= pinctrl_lookup_state(pinctl
, "shutdown");
1113 pinctrl_select_state(pinctl
, state
);
1116 MODULE_DESCRIPTION("SPREADTRUM Pin Controller Driver");
1117 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1118 MODULE_LICENSE("GPL v2");