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_4_7K (BIT(12) | BIT(7))
72 #define PULL_UP_20K BIT(7)
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_ENABLE
:
468 arg
= reg
& SLEEP_OUTPUT_MASK
;
470 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
471 if ((reg
& SLEEP_OUTPUT
) || (reg
& SLEEP_INPUT
))
476 case PIN_CONFIG_DRIVE_STRENGTH
:
477 arg
= (reg
>> DRIVE_STRENGTH_SHIFT
) &
480 case PIN_CONFIG_BIAS_PULL_DOWN
:
481 /* combine sleep pull down and pull down config */
482 arg
= ((reg
>> SLEEP_PULL_DOWN_SHIFT
) &
483 SLEEP_PULL_DOWN_MASK
) << 16;
484 arg
|= (reg
>> PULL_DOWN_SHIFT
) & PULL_DOWN_MASK
;
486 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
487 arg
= (reg
>> INPUT_SCHMITT_SHIFT
) & INPUT_SCHMITT_MASK
;
489 case PIN_CONFIG_BIAS_PULL_UP
:
490 /* combine sleep pull up and pull up config */
491 arg
= ((reg
>> SLEEP_PULL_UP_SHIFT
) &
492 SLEEP_PULL_UP_MASK
) << 16;
493 arg
|= (reg
>> PULL_UP_SHIFT
) & PULL_UP_MASK
;
495 case PIN_CONFIG_BIAS_DISABLE
:
496 if ((reg
& (SLEEP_PULL_DOWN
| SLEEP_PULL_UP
)) ||
497 (reg
& (PULL_DOWN
| PULL_UP_4_7K
| PULL_UP_20K
)))
502 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
512 *config
= pinconf_to_config_packed(param
, arg
);
516 static unsigned int sprd_pinconf_drive(unsigned int mA
)
518 unsigned int val
= 0;
530 val
|= BIT(19) | BIT(20);
536 val
|= BIT(21) | BIT(19);
539 val
|= BIT(21) | BIT(20);
542 val
|= BIT(19) | BIT(20) | BIT(21);
548 val
|= BIT(22) | BIT(19);
551 val
|= BIT(22) | BIT(20);
554 val
|= BIT(22) | BIT(20) | BIT(19);
557 val
|= BIT(22) | BIT(21);
560 val
|= BIT(22) | BIT(21) | BIT(19);
563 val
|= BIT(22) | BIT(21) | BIT(20);
566 val
|= BIT(22) | BIT(21) | BIT(20) | BIT(19);
575 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs
,
576 unsigned int num_configs
)
581 for (i
= 0; i
< num_configs
; i
++) {
582 param
= pinconf_to_config_param(configs
[i
]);
583 if (param
== PIN_CONFIG_SLEEP_HARDWARE_STATE
)
590 static int sprd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
591 unsigned long *configs
, unsigned int num_configs
)
593 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
594 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
595 bool is_sleep_config
;
602 is_sleep_config
= sprd_pinctrl_check_sleep_config(configs
, num_configs
);
604 for (i
= 0; i
< num_configs
; i
++) {
605 unsigned int param
, arg
, shift
, mask
, val
;
607 param
= pinconf_to_config_param(configs
[i
]);
608 arg
= pinconf_to_config_argument(configs
[i
]);
613 if (pin
->type
== GLOBAL_CTRL_PIN
&&
614 param
== SPRD_PIN_CONFIG_CONTROL
) {
616 } else if (pin
->type
== COMMON_PIN
|| pin
->type
== MISC_PIN
) {
618 case SPRD_PIN_CONFIG_SLEEP_MODE
:
620 val
|= AP_SLEEP_MODE
;
621 if (arg
& PUBCP_SLEEP
)
622 val
|= PUBCP_SLEEP_MODE
;
623 if (arg
& TGLDSP_SLEEP
)
624 val
|= TGLDSP_SLEEP_MODE
;
625 if (arg
& AGDSP_SLEEP
)
626 val
|= AGDSP_SLEEP_MODE
;
628 val
|= CM4_SLEEP_MODE
;
630 mask
= SLEEP_MODE_MASK
;
631 shift
= SLEEP_MODE_SHIFT
;
633 case PIN_CONFIG_INPUT_ENABLE
:
634 if (is_sleep_config
== true) {
640 mask
= SLEEP_INPUT_MASK
;
641 shift
= SLEEP_INPUT_SHIFT
;
644 case PIN_CONFIG_OUTPUT_ENABLE
:
645 if (is_sleep_config
== true) {
649 val
&= ~SLEEP_OUTPUT
;
651 mask
= SLEEP_OUTPUT_MASK
;
652 shift
= SLEEP_OUTPUT_SHIFT
;
655 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
656 if (is_sleep_config
== true) {
658 mask
= SLEEP_OUTPUT
| SLEEP_INPUT
;
661 case PIN_CONFIG_DRIVE_STRENGTH
:
662 if (arg
< 2 || arg
> 60)
665 val
= sprd_pinconf_drive(arg
);
666 mask
= DRIVE_STRENGTH_MASK
;
667 shift
= DRIVE_STRENGTH_SHIFT
;
669 case PIN_CONFIG_BIAS_PULL_DOWN
:
670 if (is_sleep_config
== true) {
671 val
|= SLEEP_PULL_DOWN
;
672 mask
= SLEEP_PULL_DOWN_MASK
;
673 shift
= SLEEP_PULL_DOWN_SHIFT
;
676 mask
= PULL_DOWN_MASK
;
677 shift
= PULL_DOWN_SHIFT
;
680 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
682 val
|= INPUT_SCHMITT
;
684 val
&= ~INPUT_SCHMITT
;
686 mask
= INPUT_SCHMITT_MASK
;
687 shift
= INPUT_SCHMITT_SHIFT
;
689 case PIN_CONFIG_BIAS_PULL_UP
:
690 if (is_sleep_config
== true) {
691 val
|= SLEEP_PULL_UP
;
692 mask
= SLEEP_PULL_UP_MASK
;
693 shift
= SLEEP_PULL_UP_SHIFT
;
697 else if (arg
== 4700)
701 shift
= PULL_UP_SHIFT
;
704 case PIN_CONFIG_BIAS_DISABLE
:
705 if (is_sleep_config
== true) {
707 mask
= SLEEP_PULL_DOWN
| SLEEP_PULL_UP
;
710 mask
= PULL_DOWN
| PULL_UP_20K
|
714 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
723 if (pin
->type
== GLOBAL_CTRL_PIN
) {
724 reg
= readl((void __iomem
*)pin
->reg
);
725 reg
&= ~(PINCTRL_BIT_MASK(pin
->bit_width
)
727 reg
|= (val
& PINCTRL_BIT_MASK(pin
->bit_width
))
729 writel(reg
, (void __iomem
*)pin
->reg
);
731 reg
= readl((void __iomem
*)pin
->reg
);
732 reg
&= ~(mask
<< shift
);
734 writel(reg
, (void __iomem
*)pin
->reg
);
741 static int sprd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
742 unsigned int selector
, unsigned long *config
)
744 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
745 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
746 struct sprd_pin_group
*grp
;
749 if (selector
>= info
->ngroups
)
752 grp
= &info
->groups
[selector
];
753 pin_id
= grp
->pins
[0];
755 return sprd_pinconf_get(pctldev
, pin_id
, config
);
758 static int sprd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
759 unsigned int selector
,
760 unsigned long *configs
,
761 unsigned int num_configs
)
763 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
764 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
765 struct sprd_pin_group
*grp
;
768 if (selector
>= info
->ngroups
)
771 grp
= &info
->groups
[selector
];
773 for (i
= 0; i
< grp
->npins
; i
++) {
774 unsigned int pin_id
= grp
->pins
[i
];
776 ret
= sprd_pinconf_set(pctldev
, pin_id
, configs
, num_configs
);
784 static int sprd_pinconf_get_config(struct pinctrl_dev
*pctldev
,
786 unsigned long *config
)
788 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
789 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
794 if (pin
->type
== GLOBAL_CTRL_PIN
) {
795 *config
= (readl((void __iomem
*)pin
->reg
) >>
796 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
798 *config
= readl((void __iomem
*)pin
->reg
);
804 static void sprd_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
805 struct seq_file
*s
, unsigned int pin_id
)
807 unsigned long config
;
810 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
814 seq_printf(s
, "0x%lx", config
);
817 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
819 unsigned int selector
)
821 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
822 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
823 struct sprd_pin_group
*grp
;
824 unsigned long config
;
828 if (selector
>= info
->ngroups
)
831 grp
= &info
->groups
[selector
];
834 for (i
= 0; i
< grp
->npins
; i
++, config
++) {
835 unsigned int pin_id
= grp
->pins
[i
];
837 name
= pin_get_name(pctldev
, pin_id
);
838 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
842 seq_printf(s
, "%s: 0x%lx ", name
, config
);
846 static const struct pinconf_ops sprd_pinconf_ops
= {
848 .pin_config_get
= sprd_pinconf_get
,
849 .pin_config_set
= sprd_pinconf_set
,
850 .pin_config_group_get
= sprd_pinconf_group_get
,
851 .pin_config_group_set
= sprd_pinconf_group_set
,
852 .pin_config_dbg_show
= sprd_pinconf_dbg_show
,
853 .pin_config_group_dbg_show
= sprd_pinconf_group_dbg_show
,
856 static const struct pinconf_generic_params sprd_dt_params
[] = {
857 {"sprd,control", SPRD_PIN_CONFIG_CONTROL
, 0},
858 {"sprd,sleep-mode", SPRD_PIN_CONFIG_SLEEP_MODE
, 0},
861 #ifdef CONFIG_DEBUG_FS
862 static const struct pin_config_item sprd_conf_items
[] = {
863 PCONFDUMP(SPRD_PIN_CONFIG_CONTROL
, "global control", NULL
, true),
864 PCONFDUMP(SPRD_PIN_CONFIG_SLEEP_MODE
, "sleep mode", NULL
, true),
868 static struct pinctrl_desc sprd_pinctrl_desc
= {
869 .pctlops
= &sprd_pctrl_ops
,
870 .pmxops
= &sprd_pmx_ops
,
871 .confops
= &sprd_pinconf_ops
,
872 .num_custom_params
= ARRAY_SIZE(sprd_dt_params
),
873 .custom_params
= sprd_dt_params
,
874 #ifdef CONFIG_DEBUG_FS
875 .custom_conf_items
= sprd_conf_items
,
877 .owner
= THIS_MODULE
,
880 static int sprd_pinctrl_parse_groups(struct device_node
*np
,
881 struct sprd_pinctrl
*sprd_pctl
,
882 struct sprd_pin_group
*grp
)
884 struct property
*prop
;
885 const char *pin_name
;
888 ret
= of_property_count_strings(np
, "pins");
892 grp
->name
= np
->name
;
894 grp
->pins
= devm_kcalloc(sprd_pctl
->dev
,
895 grp
->npins
, sizeof(unsigned int),
900 of_property_for_each_string(np
, "pins", prop
, pin_name
) {
901 ret
= sprd_pinctrl_get_id_by_name(sprd_pctl
, pin_name
);
903 grp
->pins
[i
++] = ret
;
906 for (i
= 0; i
< grp
->npins
; i
++) {
907 dev_dbg(sprd_pctl
->dev
,
908 "Group[%s] contains [%d] pins: id = %d\n",
909 grp
->name
, grp
->npins
, grp
->pins
[i
]);
915 static unsigned int sprd_pinctrl_get_groups(struct device_node
*np
)
917 struct device_node
*child
;
918 unsigned int group_cnt
, cnt
;
920 group_cnt
= of_get_child_count(np
);
922 for_each_child_of_node(np
, child
) {
923 cnt
= of_get_child_count(child
);
931 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl
*sprd_pctl
)
933 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
934 struct device_node
*np
= sprd_pctl
->dev
->of_node
;
935 struct device_node
*child
, *sub_child
;
936 struct sprd_pin_group
*grp
;
943 info
->ngroups
= sprd_pinctrl_get_groups(np
);
947 info
->groups
= devm_kcalloc(sprd_pctl
->dev
,
949 sizeof(struct sprd_pin_group
),
954 info
->grp_names
= devm_kcalloc(sprd_pctl
->dev
,
955 info
->ngroups
, sizeof(char *),
957 if (!info
->grp_names
)
960 temp
= info
->grp_names
;
963 for_each_child_of_node(np
, child
) {
964 ret
= sprd_pinctrl_parse_groups(child
, sprd_pctl
, grp
);
973 if (of_get_child_count(child
) > 0) {
974 for_each_child_of_node(child
, sub_child
) {
975 ret
= sprd_pinctrl_parse_groups(sub_child
,
978 of_node_put(sub_child
);
992 static int sprd_pinctrl_add_pins(struct sprd_pinctrl
*sprd_pctl
,
993 struct sprd_pins_info
*sprd_soc_pin_info
,
996 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
997 unsigned int ctrl_pin
= 0, com_pin
= 0;
998 struct sprd_pin
*pin
;
1001 info
->npins
= pins_cnt
;
1002 info
->pins
= devm_kcalloc(sprd_pctl
->dev
,
1003 info
->npins
, sizeof(struct sprd_pin
),
1008 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; i
++, pin
++) {
1011 pin
->name
= sprd_soc_pin_info
[i
].name
;
1012 pin
->type
= sprd_soc_pin_info
[i
].type
;
1013 pin
->number
= sprd_soc_pin_info
[i
].num
;
1014 reg
= sprd_soc_pin_info
[i
].reg
;
1015 if (pin
->type
== GLOBAL_CTRL_PIN
) {
1016 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1017 PINCTRL_REG_LEN
* reg
;
1018 pin
->bit_offset
= sprd_soc_pin_info
[i
].bit_offset
;
1019 pin
->bit_width
= sprd_soc_pin_info
[i
].bit_width
;
1021 } else if (pin
->type
== COMMON_PIN
) {
1022 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1023 PINCTRL_REG_OFFSET
+ PINCTRL_REG_LEN
*
1026 } else if (pin
->type
== MISC_PIN
) {
1027 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1028 PINCTRL_REG_MISC_OFFSET
+ PINCTRL_REG_LEN
*
1029 (i
- ctrl_pin
- com_pin
);
1033 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; pin
++, i
++) {
1034 dev_dbg(sprd_pctl
->dev
, "pin name[%s-%d], type = %d, "
1035 "bit offset = %ld, bit width = %ld, reg = 0x%lx\n",
1036 pin
->name
, pin
->number
, pin
->type
,
1037 pin
->bit_offset
, pin
->bit_width
, pin
->reg
);
1043 int sprd_pinctrl_core_probe(struct platform_device
*pdev
,
1044 struct sprd_pins_info
*sprd_soc_pin_info
,
1047 struct sprd_pinctrl
*sprd_pctl
;
1048 struct sprd_pinctrl_soc_info
*pinctrl_info
;
1049 struct pinctrl_pin_desc
*pin_desc
;
1052 sprd_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct sprd_pinctrl
),
1057 sprd_pctl
->base
= devm_platform_ioremap_resource(pdev
, 0);
1058 if (IS_ERR(sprd_pctl
->base
))
1059 return PTR_ERR(sprd_pctl
->base
);
1061 pinctrl_info
= devm_kzalloc(&pdev
->dev
,
1062 sizeof(struct sprd_pinctrl_soc_info
),
1067 sprd_pctl
->info
= pinctrl_info
;
1068 sprd_pctl
->dev
= &pdev
->dev
;
1069 platform_set_drvdata(pdev
, sprd_pctl
);
1071 ret
= sprd_pinctrl_add_pins(sprd_pctl
, sprd_soc_pin_info
, pins_cnt
);
1073 dev_err(&pdev
->dev
, "fail to add pins information\n");
1077 ret
= sprd_pinctrl_parse_dt(sprd_pctl
);
1079 dev_err(&pdev
->dev
, "fail to parse dt properties\n");
1083 pin_desc
= devm_kcalloc(&pdev
->dev
,
1084 pinctrl_info
->npins
,
1085 sizeof(struct pinctrl_pin_desc
),
1090 for (i
= 0; i
< pinctrl_info
->npins
; i
++) {
1091 pin_desc
[i
].number
= pinctrl_info
->pins
[i
].number
;
1092 pin_desc
[i
].name
= pinctrl_info
->pins
[i
].name
;
1093 pin_desc
[i
].drv_data
= pinctrl_info
;
1096 sprd_pinctrl_desc
.pins
= pin_desc
;
1097 sprd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1098 sprd_pinctrl_desc
.npins
= pinctrl_info
->npins
;
1100 sprd_pctl
->pctl
= pinctrl_register(&sprd_pinctrl_desc
,
1101 &pdev
->dev
, (void *)sprd_pctl
);
1102 if (IS_ERR(sprd_pctl
->pctl
)) {
1103 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
1104 return PTR_ERR(sprd_pctl
->pctl
);
1109 EXPORT_SYMBOL_GPL(sprd_pinctrl_core_probe
);
1111 int sprd_pinctrl_remove(struct platform_device
*pdev
)
1113 struct sprd_pinctrl
*sprd_pctl
= platform_get_drvdata(pdev
);
1115 pinctrl_unregister(sprd_pctl
->pctl
);
1118 EXPORT_SYMBOL_GPL(sprd_pinctrl_remove
);
1120 void sprd_pinctrl_shutdown(struct platform_device
*pdev
)
1122 struct pinctrl
*pinctl
;
1123 struct pinctrl_state
*state
;
1125 pinctl
= devm_pinctrl_get(&pdev
->dev
);
1128 state
= pinctrl_lookup_state(pinctl
, "shutdown");
1131 pinctrl_select_state(pinctl
, state
);
1133 EXPORT_SYMBOL_GPL(sprd_pinctrl_shutdown
);
1135 MODULE_DESCRIPTION("SPREADTRUM Pin Controller Driver");
1136 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1137 MODULE_LICENSE("GPL v2");