2 * Spreadtrum pin controller driver
3 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include <linux/debugfs.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinconf.h>
26 #include <linux/pinctrl/pinconf-generic.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/slab.h>
32 #include "../pinmux.h"
33 #include "../pinconf.h"
34 #include "../pinctrl-utils.h"
35 #include "pinctrl-sprd.h"
37 #define PINCTRL_BIT_MASK(width) (~(~0UL << (width)))
38 #define PINCTRL_REG_OFFSET 0x20
39 #define PINCTRL_REG_MISC_OFFSET 0x4020
40 #define PINCTRL_REG_LEN 0x4
42 #define PIN_FUNC_MASK (BIT(4) | BIT(5))
43 #define PIN_FUNC_SEL_1 ~PIN_FUNC_MASK
44 #define PIN_FUNC_SEL_2 BIT(4)
45 #define PIN_FUNC_SEL_3 BIT(5)
46 #define PIN_FUNC_SEL_4 PIN_FUNC_MASK
48 #define AP_SLEEP_MODE BIT(13)
49 #define PUBCP_SLEEP_MODE BIT(14)
50 #define TGLDSP_SLEEP_MODE BIT(15)
51 #define AGDSP_SLEEP_MODE BIT(16)
52 #define SLEEP_MODE_MASK GENMASK(3, 0)
53 #define SLEEP_MODE_SHIFT 13
55 #define SLEEP_INPUT BIT(1)
56 #define SLEEP_INPUT_MASK 0x1
57 #define SLEEP_INPUT_SHIFT 1
59 #define SLEEP_OUTPUT BIT(0)
60 #define SLEEP_OUTPUT_MASK 0x1
61 #define SLEEP_OUTPUT_SHIFT 0
63 #define DRIVE_STRENGTH_MASK GENMASK(3, 0)
64 #define DRIVE_STRENGTH_SHIFT 19
66 #define SLEEP_PULL_DOWN BIT(2)
67 #define SLEEP_PULL_DOWN_MASK 0x1
68 #define SLEEP_PULL_DOWN_SHIFT 2
70 #define PULL_DOWN BIT(6)
71 #define PULL_DOWN_MASK 0x1
72 #define PULL_DOWN_SHIFT 6
74 #define SLEEP_PULL_UP BIT(3)
75 #define SLEEP_PULL_UP_MASK 0x1
76 #define SLEEP_PULL_UP_SHIFT 3
78 #define PULL_UP_20K (BIT(12) | BIT(7))
79 #define PULL_UP_4_7K BIT(12)
80 #define PULL_UP_MASK 0x21
81 #define PULL_UP_SHIFT 7
83 #define INPUT_SCHMITT BIT(11)
84 #define INPUT_SCHMITT_MASK 0x1
85 #define INPUT_SCHMITT_SHIFT 11
90 TGLDSP_SLEEP
= BIT(2),
103 * struct sprd_pin: represent one pin's description
105 * @number: pin number
106 * @type: pin type, can be GLOBAL_CTRL_PIN/COMMON_PIN/MISC_PIN
107 * @reg: pin register address
108 * @bit_offset: bit offset in pin register
109 * @bit_width: bit width in pin register
116 unsigned long bit_offset
;
117 unsigned long bit_width
;
121 * struct sprd_pin_group: represent one group's description
123 * @npins: pin numbers of this group
124 * @pins: pointer to pins array
126 struct sprd_pin_group
{
133 * struct sprd_pinctrl_soc_info: represent the SoC's pins description
134 * @groups: pointer to groups of pins
135 * @ngroups: group numbers of the whole SoC
136 * @pins: pointer to pins description
137 * @npins: pin numbers of the whole SoC
138 * @grp_names: pointer to group names array
140 struct sprd_pinctrl_soc_info
{
141 struct sprd_pin_group
*groups
;
142 unsigned int ngroups
;
143 struct sprd_pin
*pins
;
145 const char **grp_names
;
149 * struct sprd_pinctrl: represent the pin controller device
150 * @dev: pointer to the device structure
151 * @pctl: pointer to the pinctrl handle
152 * @base: base address of the controller
153 * @info: pointer to SoC's pins description information
155 struct sprd_pinctrl
{
157 struct pinctrl_dev
*pctl
;
159 struct sprd_pinctrl_soc_info
*info
;
162 enum sprd_pinconf_params
{
163 SPRD_PIN_CONFIG_CONTROL
= PIN_CONFIG_END
+ 1,
164 SPRD_PIN_CONFIG_SLEEP_MODE
= PIN_CONFIG_END
+ 2,
167 static int sprd_pinctrl_get_id_by_name(struct sprd_pinctrl
*sprd_pctl
,
170 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
173 for (i
= 0; i
< info
->npins
; i
++) {
174 if (!strcmp(info
->pins
[i
].name
, name
))
175 return info
->pins
[i
].number
;
181 static struct sprd_pin
*
182 sprd_pinctrl_get_pin_by_id(struct sprd_pinctrl
*sprd_pctl
, unsigned int id
)
184 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
185 struct sprd_pin
*pin
= NULL
;
188 for (i
= 0; i
< info
->npins
; i
++) {
189 if (info
->pins
[i
].number
== id
) {
190 pin
= &info
->pins
[i
];
198 static const struct sprd_pin_group
*
199 sprd_pinctrl_find_group_by_name(struct sprd_pinctrl
*sprd_pctl
,
202 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
203 const struct sprd_pin_group
*grp
= NULL
;
206 for (i
= 0; i
< info
->ngroups
; i
++) {
207 if (!strcmp(info
->groups
[i
].name
, name
)) {
208 grp
= &info
->groups
[i
];
216 static int sprd_pctrl_group_count(struct pinctrl_dev
*pctldev
)
218 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
219 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
221 return info
->ngroups
;
224 static const char *sprd_pctrl_group_name(struct pinctrl_dev
*pctldev
,
225 unsigned int selector
)
227 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
228 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
230 return info
->groups
[selector
].name
;
233 static int sprd_pctrl_group_pins(struct pinctrl_dev
*pctldev
,
234 unsigned int selector
,
235 const unsigned int **pins
,
238 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
239 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
241 if (selector
>= info
->ngroups
)
244 *pins
= info
->groups
[selector
].pins
;
245 *npins
= info
->groups
[selector
].npins
;
250 static int sprd_dt_node_to_map(struct pinctrl_dev
*pctldev
,
251 struct device_node
*np
,
252 struct pinctrl_map
**map
,
253 unsigned int *num_maps
)
255 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
256 const struct sprd_pin_group
*grp
;
257 unsigned long *configs
= NULL
;
258 unsigned int num_configs
= 0;
259 unsigned int reserved_maps
= 0;
260 unsigned int reserve
= 0;
261 const char *function
;
262 enum pinctrl_map_type type
;
265 grp
= sprd_pinctrl_find_group_by_name(pctl
, np
->name
);
267 dev_err(pctl
->dev
, "unable to find group for node %s\n",
268 of_node_full_name(np
));
272 ret
= of_property_count_strings(np
, "pins");
277 type
= PIN_MAP_TYPE_CONFIGS_PIN
;
279 type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
281 ret
= of_property_read_string(np
, "function", &function
);
285 "%s: could not parse property function\n",
286 of_node_full_name(np
));
290 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
293 dev_err(pctl
->dev
, "%s: could not parse node property\n",
294 of_node_full_name(np
));
301 if (function
!= NULL
)
306 ret
= pinctrl_utils_reserve_map(pctldev
, map
, &reserved_maps
,
312 ret
= pinctrl_utils_add_map_mux(pctldev
, map
,
313 &reserved_maps
, num_maps
,
314 grp
->name
, function
);
320 const char *group_or_pin
;
323 if (type
== PIN_MAP_TYPE_CONFIGS_PIN
) {
324 pin_id
= grp
->pins
[0];
325 group_or_pin
= pin_get_name(pctldev
, pin_id
);
327 group_or_pin
= grp
->name
;
330 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
331 &reserved_maps
, num_maps
,
332 group_or_pin
, configs
,
341 static void sprd_pctrl_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
344 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
347 static const struct pinctrl_ops sprd_pctrl_ops
= {
348 .get_groups_count
= sprd_pctrl_group_count
,
349 .get_group_name
= sprd_pctrl_group_name
,
350 .get_group_pins
= sprd_pctrl_group_pins
,
351 .pin_dbg_show
= sprd_pctrl_dbg_show
,
352 .dt_node_to_map
= sprd_dt_node_to_map
,
353 .dt_free_map
= pinctrl_utils_free_map
,
356 static int sprd_pmx_get_function_count(struct pinctrl_dev
*pctldev
)
361 static const char *sprd_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
362 unsigned int selector
)
378 static int sprd_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
379 unsigned int selector
,
380 const char * const **groups
,
381 unsigned int * const num_groups
)
383 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
384 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
386 *groups
= info
->grp_names
;
387 *num_groups
= info
->ngroups
;
392 static int sprd_pmx_set_mux(struct pinctrl_dev
*pctldev
,
393 unsigned int func_selector
,
394 unsigned int group_selector
)
396 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
397 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
398 struct sprd_pin_group
*grp
= &info
->groups
[group_selector
];
399 unsigned int i
, grp_pins
= grp
->npins
;
401 unsigned int val
= 0;
403 if (group_selector
>= info
->ngroups
)
406 switch (func_selector
) {
408 val
&= PIN_FUNC_SEL_1
;
411 val
|= PIN_FUNC_SEL_2
;
414 val
|= PIN_FUNC_SEL_3
;
417 val
|= PIN_FUNC_SEL_4
;
423 for (i
= 0; i
< grp_pins
; i
++) {
424 unsigned int pin_id
= grp
->pins
[i
];
425 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
427 if (!pin
|| pin
->type
!= COMMON_PIN
)
430 reg
= readl((void __iomem
*)pin
->reg
);
431 reg
&= ~PIN_FUNC_MASK
;
433 writel(reg
, (void __iomem
*)pin
->reg
);
439 static const struct pinmux_ops sprd_pmx_ops
= {
440 .get_functions_count
= sprd_pmx_get_function_count
,
441 .get_function_name
= sprd_pmx_get_function_name
,
442 .get_function_groups
= sprd_pmx_get_function_groups
,
443 .set_mux
= sprd_pmx_set_mux
,
446 static int sprd_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
447 unsigned long *config
)
449 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
450 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
451 unsigned int param
= pinconf_to_config_param(*config
);
452 unsigned int reg
, arg
;
457 if (pin
->type
== GLOBAL_CTRL_PIN
) {
458 reg
= (readl((void __iomem
*)pin
->reg
) >>
459 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
461 reg
= readl((void __iomem
*)pin
->reg
);
464 if (pin
->type
== GLOBAL_CTRL_PIN
&&
465 param
== SPRD_PIN_CONFIG_CONTROL
) {
467 } else if (pin
->type
== COMMON_PIN
) {
469 case SPRD_PIN_CONFIG_SLEEP_MODE
:
470 arg
= (reg
>> SLEEP_MODE_SHIFT
) & SLEEP_MODE_MASK
;
472 case PIN_CONFIG_INPUT_ENABLE
:
473 arg
= (reg
>> SLEEP_INPUT_SHIFT
) & SLEEP_INPUT_MASK
;
475 case PIN_CONFIG_OUTPUT
:
476 arg
= reg
& SLEEP_OUTPUT_MASK
;
478 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
484 } else if (pin
->type
== MISC_PIN
) {
486 case PIN_CONFIG_DRIVE_STRENGTH
:
487 arg
= (reg
>> DRIVE_STRENGTH_SHIFT
) &
490 case PIN_CONFIG_BIAS_PULL_DOWN
:
491 /* combine sleep pull down and pull down config */
492 arg
= ((reg
>> SLEEP_PULL_DOWN_SHIFT
) &
493 SLEEP_PULL_DOWN_MASK
) << 16;
494 arg
|= (reg
>> PULL_DOWN_SHIFT
) & PULL_DOWN_MASK
;
496 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
497 arg
= (reg
>> INPUT_SCHMITT_SHIFT
) & INPUT_SCHMITT_MASK
;
499 case PIN_CONFIG_BIAS_PULL_UP
:
500 /* combine sleep pull up and pull up config */
501 arg
= ((reg
>> SLEEP_PULL_UP_SHIFT
) &
502 SLEEP_PULL_UP_MASK
) << 16;
503 arg
|= (reg
>> PULL_UP_SHIFT
) & PULL_UP_MASK
;
505 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
515 *config
= pinconf_to_config_packed(param
, arg
);
519 static unsigned int sprd_pinconf_drive(unsigned int mA
)
521 unsigned int val
= 0;
533 val
|= BIT(19) | BIT(20);
539 val
|= BIT(21) | BIT(19);
542 val
|= BIT(21) | BIT(20);
545 val
|= BIT(19) | BIT(20) | BIT(21);
551 val
|= BIT(22) | BIT(19);
554 val
|= BIT(22) | BIT(20);
557 val
|= BIT(22) | BIT(20) | BIT(19);
560 val
|= BIT(22) | BIT(21);
563 val
|= BIT(22) | BIT(21) | BIT(19);
566 val
|= BIT(22) | BIT(21) | BIT(20);
569 val
|= BIT(22) | BIT(21) | BIT(20) | BIT(19);
578 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs
,
579 unsigned int num_configs
)
584 for (i
= 0; i
< num_configs
; i
++) {
585 param
= pinconf_to_config_param(configs
[i
]);
586 if (param
== PIN_CONFIG_SLEEP_HARDWARE_STATE
)
593 static int sprd_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin_id
,
594 unsigned long *configs
, unsigned int num_configs
)
596 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
597 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
598 bool is_sleep_config
;
605 is_sleep_config
= sprd_pinctrl_check_sleep_config(configs
, num_configs
);
607 for (i
= 0; i
< num_configs
; i
++) {
608 unsigned int param
, arg
, shift
, mask
, val
;
610 param
= pinconf_to_config_param(configs
[i
]);
611 arg
= pinconf_to_config_argument(configs
[i
]);
616 if (pin
->type
== GLOBAL_CTRL_PIN
&&
617 param
== SPRD_PIN_CONFIG_CONTROL
) {
619 } else if (pin
->type
== COMMON_PIN
) {
621 case SPRD_PIN_CONFIG_SLEEP_MODE
:
623 val
|= AP_SLEEP_MODE
;
624 if (arg
& PUBCP_SLEEP
)
625 val
|= PUBCP_SLEEP_MODE
;
626 if (arg
& TGLDSP_SLEEP
)
627 val
|= TGLDSP_SLEEP_MODE
;
628 if (arg
& AGDSP_SLEEP
)
629 val
|= AGDSP_SLEEP_MODE
;
631 mask
= SLEEP_MODE_MASK
;
632 shift
= SLEEP_MODE_SHIFT
;
634 case PIN_CONFIG_INPUT_ENABLE
:
635 if (is_sleep_config
== true) {
641 mask
= SLEEP_INPUT_MASK
;
642 shift
= SLEEP_INPUT_SHIFT
;
645 case PIN_CONFIG_OUTPUT
:
646 if (is_sleep_config
== true) {
648 mask
= SLEEP_OUTPUT_MASK
;
649 shift
= SLEEP_OUTPUT_SHIFT
;
652 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
657 } else if (pin
->type
== MISC_PIN
) {
659 case PIN_CONFIG_DRIVE_STRENGTH
:
660 if (arg
< 2 || arg
> 60)
663 val
= sprd_pinconf_drive(arg
);
664 mask
= DRIVE_STRENGTH_MASK
;
665 shift
= DRIVE_STRENGTH_SHIFT
;
667 case PIN_CONFIG_BIAS_PULL_DOWN
:
668 if (is_sleep_config
== true) {
669 val
|= SLEEP_PULL_DOWN
;
670 mask
= SLEEP_PULL_DOWN_MASK
;
671 shift
= SLEEP_PULL_DOWN_SHIFT
;
674 mask
= PULL_DOWN_MASK
;
675 shift
= PULL_DOWN_SHIFT
;
678 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
680 val
|= INPUT_SCHMITT
;
682 val
&= ~INPUT_SCHMITT
;
684 mask
= INPUT_SCHMITT_MASK
;
685 shift
= INPUT_SCHMITT_SHIFT
;
687 case PIN_CONFIG_BIAS_PULL_UP
:
688 if (is_sleep_config
== true) {
689 val
|= SLEEP_PULL_UP
;
690 mask
= SLEEP_PULL_UP_MASK
;
691 shift
= SLEEP_PULL_UP_SHIFT
;
695 else if (arg
== 4700)
699 shift
= PULL_UP_SHIFT
;
702 case PIN_CONFIG_SLEEP_HARDWARE_STATE
:
711 if (pin
->type
== GLOBAL_CTRL_PIN
) {
712 reg
= readl((void __iomem
*)pin
->reg
);
713 reg
&= ~(PINCTRL_BIT_MASK(pin
->bit_width
)
715 reg
|= (val
& PINCTRL_BIT_MASK(pin
->bit_width
))
717 writel(reg
, (void __iomem
*)pin
->reg
);
719 reg
= readl((void __iomem
*)pin
->reg
);
720 reg
&= ~(mask
<< shift
);
722 writel(reg
, (void __iomem
*)pin
->reg
);
729 static int sprd_pinconf_group_get(struct pinctrl_dev
*pctldev
,
730 unsigned int selector
, unsigned long *config
)
732 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
733 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
734 struct sprd_pin_group
*grp
;
737 if (selector
>= info
->ngroups
)
740 grp
= &info
->groups
[selector
];
741 pin_id
= grp
->pins
[0];
743 return sprd_pinconf_get(pctldev
, pin_id
, config
);
746 static int sprd_pinconf_group_set(struct pinctrl_dev
*pctldev
,
747 unsigned int selector
,
748 unsigned long *configs
,
749 unsigned int num_configs
)
751 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
752 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
753 struct sprd_pin_group
*grp
;
756 if (selector
>= info
->ngroups
)
759 grp
= &info
->groups
[selector
];
761 for (i
= 0; i
< grp
->npins
; i
++) {
762 unsigned int pin_id
= grp
->pins
[i
];
764 ret
= sprd_pinconf_set(pctldev
, pin_id
, configs
, num_configs
);
772 static int sprd_pinconf_get_config(struct pinctrl_dev
*pctldev
,
774 unsigned long *config
)
776 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
777 struct sprd_pin
*pin
= sprd_pinctrl_get_pin_by_id(pctl
, pin_id
);
782 if (pin
->type
== GLOBAL_CTRL_PIN
) {
783 *config
= (readl((void __iomem
*)pin
->reg
) >>
784 pin
->bit_offset
) & PINCTRL_BIT_MASK(pin
->bit_width
);
786 *config
= readl((void __iomem
*)pin
->reg
);
792 static void sprd_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
793 struct seq_file
*s
, unsigned int pin_id
)
795 unsigned long config
;
798 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
802 seq_printf(s
, "0x%lx", config
);
805 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
807 unsigned int selector
)
809 struct sprd_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
810 struct sprd_pinctrl_soc_info
*info
= pctl
->info
;
811 struct sprd_pin_group
*grp
;
812 unsigned long config
;
816 if (selector
>= info
->ngroups
)
819 grp
= &info
->groups
[selector
];
822 for (i
= 0; i
< grp
->npins
; i
++, config
++) {
823 unsigned int pin_id
= grp
->pins
[i
];
825 name
= pin_get_name(pctldev
, pin_id
);
826 ret
= sprd_pinconf_get_config(pctldev
, pin_id
, &config
);
830 seq_printf(s
, "%s: 0x%lx ", name
, config
);
834 static const struct pinconf_ops sprd_pinconf_ops
= {
836 .pin_config_get
= sprd_pinconf_get
,
837 .pin_config_set
= sprd_pinconf_set
,
838 .pin_config_group_get
= sprd_pinconf_group_get
,
839 .pin_config_group_set
= sprd_pinconf_group_set
,
840 .pin_config_dbg_show
= sprd_pinconf_dbg_show
,
841 .pin_config_group_dbg_show
= sprd_pinconf_group_dbg_show
,
844 static const struct pinconf_generic_params sprd_dt_params
[] = {
845 {"sprd,control", SPRD_PIN_CONFIG_CONTROL
, 0},
846 {"sprd,sleep-mode", SPRD_PIN_CONFIG_SLEEP_MODE
, 0},
849 #ifdef CONFIG_DEBUG_FS
850 static const struct pin_config_item sprd_conf_items
[] = {
851 PCONFDUMP(SPRD_PIN_CONFIG_CONTROL
, "global control", NULL
, true),
852 PCONFDUMP(SPRD_PIN_CONFIG_SLEEP_MODE
, "sleep mode", NULL
, true),
856 static struct pinctrl_desc sprd_pinctrl_desc
= {
857 .pctlops
= &sprd_pctrl_ops
,
858 .pmxops
= &sprd_pmx_ops
,
859 .confops
= &sprd_pinconf_ops
,
860 .num_custom_params
= ARRAY_SIZE(sprd_dt_params
),
861 .custom_params
= sprd_dt_params
,
862 #ifdef CONFIG_DEBUG_FS
863 .custom_conf_items
= sprd_conf_items
,
865 .owner
= THIS_MODULE
,
868 static int sprd_pinctrl_parse_groups(struct device_node
*np
,
869 struct sprd_pinctrl
*sprd_pctl
,
870 struct sprd_pin_group
*grp
)
872 struct property
*prop
;
873 const char *pin_name
;
876 ret
= of_property_count_strings(np
, "pins");
880 grp
->name
= np
->name
;
882 grp
->pins
= devm_kzalloc(sprd_pctl
->dev
, grp
->npins
*
883 sizeof(unsigned int), GFP_KERNEL
);
887 of_property_for_each_string(np
, "pins", prop
, pin_name
) {
888 ret
= sprd_pinctrl_get_id_by_name(sprd_pctl
, pin_name
);
890 grp
->pins
[i
++] = ret
;
893 for (i
= 0; i
< grp
->npins
; i
++) {
894 dev_dbg(sprd_pctl
->dev
,
895 "Group[%s] contains [%d] pins: id = %d\n",
896 grp
->name
, grp
->npins
, grp
->pins
[i
]);
902 static unsigned int sprd_pinctrl_get_groups(struct device_node
*np
)
904 struct device_node
*child
;
905 unsigned int group_cnt
, cnt
;
907 group_cnt
= of_get_child_count(np
);
909 for_each_child_of_node(np
, child
) {
910 cnt
= of_get_child_count(child
);
918 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl
*sprd_pctl
)
920 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
921 struct device_node
*np
= sprd_pctl
->dev
->of_node
;
922 struct device_node
*child
, *sub_child
;
923 struct sprd_pin_group
*grp
;
930 info
->ngroups
= sprd_pinctrl_get_groups(np
);
934 info
->groups
= devm_kzalloc(sprd_pctl
->dev
, info
->ngroups
*
935 sizeof(struct sprd_pin_group
),
940 info
->grp_names
= devm_kzalloc(sprd_pctl
->dev
,
941 info
->ngroups
* sizeof(char *),
943 if (!info
->grp_names
)
946 temp
= info
->grp_names
;
949 for_each_child_of_node(np
, child
) {
950 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
,
973 static int sprd_pinctrl_add_pins(struct sprd_pinctrl
*sprd_pctl
,
974 struct sprd_pins_info
*sprd_soc_pin_info
,
977 struct sprd_pinctrl_soc_info
*info
= sprd_pctl
->info
;
978 unsigned int ctrl_pin
= 0, com_pin
= 0;
979 struct sprd_pin
*pin
;
982 info
->npins
= pins_cnt
;
983 info
->pins
= devm_kzalloc(sprd_pctl
->dev
,
984 info
->npins
* sizeof(struct sprd_pin
),
989 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; i
++, pin
++) {
992 pin
->name
= sprd_soc_pin_info
[i
].name
;
993 pin
->type
= sprd_soc_pin_info
[i
].type
;
994 pin
->number
= sprd_soc_pin_info
[i
].num
;
995 reg
= sprd_soc_pin_info
[i
].reg
;
996 if (pin
->type
== GLOBAL_CTRL_PIN
) {
997 pin
->reg
= (unsigned long)sprd_pctl
->base
+
998 PINCTRL_REG_LEN
* reg
;
999 pin
->bit_offset
= sprd_soc_pin_info
[i
].bit_offset
;
1000 pin
->bit_width
= sprd_soc_pin_info
[i
].bit_width
;
1002 } else if (pin
->type
== COMMON_PIN
) {
1003 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1004 PINCTRL_REG_OFFSET
+ PINCTRL_REG_LEN
*
1007 } else if (pin
->type
== MISC_PIN
) {
1008 pin
->reg
= (unsigned long)sprd_pctl
->base
+
1009 PINCTRL_REG_MISC_OFFSET
+ PINCTRL_REG_LEN
*
1010 (i
- ctrl_pin
- com_pin
);
1014 for (i
= 0, pin
= info
->pins
; i
< info
->npins
; pin
++, i
++) {
1015 dev_dbg(sprd_pctl
->dev
, "pin name[%s-%d], type = %d, "
1016 "bit offset = %ld, bit width = %ld, reg = 0x%lx\n",
1017 pin
->name
, pin
->number
, pin
->type
,
1018 pin
->bit_offset
, pin
->bit_width
, pin
->reg
);
1024 int sprd_pinctrl_core_probe(struct platform_device
*pdev
,
1025 struct sprd_pins_info
*sprd_soc_pin_info
,
1028 struct sprd_pinctrl
*sprd_pctl
;
1029 struct sprd_pinctrl_soc_info
*pinctrl_info
;
1030 struct pinctrl_pin_desc
*pin_desc
;
1031 struct resource
*res
;
1034 sprd_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(struct sprd_pinctrl
),
1039 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1040 sprd_pctl
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1041 if (IS_ERR(sprd_pctl
->base
))
1042 return PTR_ERR(sprd_pctl
->base
);
1044 pinctrl_info
= devm_kzalloc(&pdev
->dev
,
1045 sizeof(struct sprd_pinctrl_soc_info
),
1050 sprd_pctl
->info
= pinctrl_info
;
1051 sprd_pctl
->dev
= &pdev
->dev
;
1052 platform_set_drvdata(pdev
, sprd_pctl
);
1054 ret
= sprd_pinctrl_add_pins(sprd_pctl
, sprd_soc_pin_info
, pins_cnt
);
1056 dev_err(&pdev
->dev
, "fail to add pins information\n");
1060 pin_desc
= devm_kzalloc(&pdev
->dev
, pinctrl_info
->npins
*
1061 sizeof(struct pinctrl_pin_desc
),
1066 for (i
= 0; i
< pinctrl_info
->npins
; i
++) {
1067 pin_desc
[i
].number
= pinctrl_info
->pins
[i
].number
;
1068 pin_desc
[i
].name
= pinctrl_info
->pins
[i
].name
;
1069 pin_desc
[i
].drv_data
= pinctrl_info
;
1072 sprd_pinctrl_desc
.pins
= pin_desc
;
1073 sprd_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1074 sprd_pinctrl_desc
.npins
= pinctrl_info
->npins
;
1076 sprd_pctl
->pctl
= pinctrl_register(&sprd_pinctrl_desc
,
1077 &pdev
->dev
, (void *)sprd_pctl
);
1078 if (IS_ERR(sprd_pctl
->pctl
)) {
1079 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
1080 return PTR_ERR(sprd_pctl
->pctl
);
1083 ret
= sprd_pinctrl_parse_dt(sprd_pctl
);
1085 dev_err(&pdev
->dev
, "fail to parse dt properties\n");
1086 pinctrl_unregister(sprd_pctl
->pctl
);
1093 int sprd_pinctrl_remove(struct platform_device
*pdev
)
1095 struct sprd_pinctrl
*sprd_pctl
= platform_get_drvdata(pdev
);
1097 pinctrl_unregister(sprd_pctl
->pctl
);
1101 void sprd_pinctrl_shutdown(struct platform_device
*pdev
)
1103 struct pinctrl
*pinctl
;
1104 struct pinctrl_state
*state
;
1106 pinctl
= devm_pinctrl_get(&pdev
->dev
);
1109 state
= pinctrl_lookup_state(pinctl
, "shutdown");
1112 pinctrl_select_state(pinctl
, state
);
1115 MODULE_DESCRIPTION("SPREADTRUM Pin Controller Driver");
1116 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1117 MODULE_LICENSE("GPL v2");