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 #define SPRD_PIN_CONFIG_CONTROL (PIN_CONFIG_END + 1)
163 #define SPRD_PIN_CONFIG_SLEEP_MODE (PIN_CONFIG_END + 2)
165 static int sprd_pinctrl_get_id_by_name(struct sprd_pinctrl *sprd_pctl,
168 struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
171 for (i = 0; i < info->npins; i++) {
172 if (!strcmp(info->pins[i].name, name))
173 return info->pins[i].number;
179 static struct sprd_pin *
180 sprd_pinctrl_get_pin_by_id(struct sprd_pinctrl *sprd_pctl, unsigned int id)
182 struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
183 struct sprd_pin *pin = NULL;
186 for (i = 0; i < info->npins; i++) {
187 if (info->pins[i].number == id) {
188 pin = &info->pins[i];
196 static const struct sprd_pin_group *
197 sprd_pinctrl_find_group_by_name(struct sprd_pinctrl *sprd_pctl,
200 struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
201 const struct sprd_pin_group *grp = NULL;
204 for (i = 0; i < info->ngroups; i++) {
205 if (!strcmp(info->groups[i].name, name)) {
206 grp = &info->groups[i];
214 static int sprd_pctrl_group_count(struct pinctrl_dev *pctldev)
216 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
217 struct sprd_pinctrl_soc_info *info = pctl->info;
219 return info->ngroups;
222 static const char *sprd_pctrl_group_name(struct pinctrl_dev *pctldev,
223 unsigned int selector)
225 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
226 struct sprd_pinctrl_soc_info *info = pctl->info;
228 return info->groups[selector].name;
231 static int sprd_pctrl_group_pins(struct pinctrl_dev *pctldev,
232 unsigned int selector,
233 const unsigned int **pins,
236 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
237 struct sprd_pinctrl_soc_info *info = pctl->info;
239 if (selector >= info->ngroups)
242 *pins = info->groups[selector].pins;
243 *npins = info->groups[selector].npins;
248 static int sprd_dt_node_to_map(struct pinctrl_dev *pctldev,
249 struct device_node *np,
250 struct pinctrl_map **map,
251 unsigned int *num_maps)
253 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
254 const struct sprd_pin_group *grp;
255 unsigned long *configs = NULL;
256 unsigned int num_configs = 0;
257 unsigned int reserved_maps = 0;
258 unsigned int reserve = 0;
259 const char *function;
260 enum pinctrl_map_type type;
263 grp = sprd_pinctrl_find_group_by_name(pctl, np->name);
265 dev_err(pctl->dev, "unable to find group for node %s\n",
266 of_node_full_name(np));
270 ret = of_property_count_strings(np, "pins");
275 type = PIN_MAP_TYPE_CONFIGS_PIN;
277 type = PIN_MAP_TYPE_CONFIGS_GROUP;
279 ret = of_property_read_string(np, "function", &function);
283 "%s: could not parse property function\n",
284 of_node_full_name(np));
288 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
291 dev_err(pctl->dev, "%s: could not parse node property\n",
292 of_node_full_name(np));
299 if (function != NULL)
304 ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps,
310 ret = pinctrl_utils_add_map_mux(pctldev, map,
311 &reserved_maps, num_maps,
312 grp->name, function);
318 const char *group_or_pin;
321 if (type == PIN_MAP_TYPE_CONFIGS_PIN) {
322 pin_id = grp->pins[0];
323 group_or_pin = pin_get_name(pctldev, pin_id);
325 group_or_pin = grp->name;
328 ret = pinctrl_utils_add_map_configs(pctldev, map,
329 &reserved_maps, num_maps,
330 group_or_pin, configs,
339 static void sprd_pctrl_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
342 seq_printf(s, "%s", dev_name(pctldev->dev));
345 static const struct pinctrl_ops sprd_pctrl_ops = {
346 .get_groups_count = sprd_pctrl_group_count,
347 .get_group_name = sprd_pctrl_group_name,
348 .get_group_pins = sprd_pctrl_group_pins,
349 .pin_dbg_show = sprd_pctrl_dbg_show,
350 .dt_node_to_map = sprd_dt_node_to_map,
351 .dt_free_map = pinctrl_utils_free_map,
354 static int sprd_pmx_get_function_count(struct pinctrl_dev *pctldev)
359 static const char *sprd_pmx_get_function_name(struct pinctrl_dev *pctldev,
360 unsigned int selector)
376 static int sprd_pmx_get_function_groups(struct pinctrl_dev *pctldev,
377 unsigned int selector,
378 const char * const **groups,
379 unsigned int * const num_groups)
381 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
382 struct sprd_pinctrl_soc_info *info = pctl->info;
384 *groups = info->grp_names;
385 *num_groups = info->ngroups;
390 static int sprd_pmx_set_mux(struct pinctrl_dev *pctldev,
391 unsigned int func_selector,
392 unsigned int group_selector)
394 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
395 struct sprd_pinctrl_soc_info *info = pctl->info;
396 struct sprd_pin_group *grp = &info->groups[group_selector];
397 unsigned int i, grp_pins = grp->npins;
399 unsigned int val = 0;
401 if (group_selector >= info->ngroups)
404 switch (func_selector) {
406 val &= PIN_FUNC_SEL_1;
409 val |= PIN_FUNC_SEL_2;
412 val |= PIN_FUNC_SEL_3;
415 val |= PIN_FUNC_SEL_4;
421 for (i = 0; i < grp_pins; i++) {
422 unsigned int pin_id = grp->pins[i];
423 struct sprd_pin *pin = sprd_pinctrl_get_pin_by_id(pctl, pin_id);
425 if (!pin || pin->type != COMMON_PIN)
428 reg = readl((void __iomem *)pin->reg);
429 reg &= ~PIN_FUNC_MASK;
431 writel(reg, (void __iomem *)pin->reg);
437 static const struct pinmux_ops sprd_pmx_ops = {
438 .get_functions_count = sprd_pmx_get_function_count,
439 .get_function_name = sprd_pmx_get_function_name,
440 .get_function_groups = sprd_pmx_get_function_groups,
441 .set_mux = sprd_pmx_set_mux,
444 static int sprd_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin_id,
445 unsigned long *config)
447 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
448 struct sprd_pin *pin = sprd_pinctrl_get_pin_by_id(pctl, pin_id);
449 unsigned int param = pinconf_to_config_param(*config);
450 unsigned int reg, arg;
455 if (pin->type == GLOBAL_CTRL_PIN) {
456 reg = (readl((void __iomem *)pin->reg) >>
457 pin->bit_offset) & PINCTRL_BIT_MASK(pin->bit_width);
459 reg = readl((void __iomem *)pin->reg);
462 if (pin->type == GLOBAL_CTRL_PIN &&
463 param == SPRD_PIN_CONFIG_CONTROL) {
465 } else if (pin->type == COMMON_PIN) {
467 case SPRD_PIN_CONFIG_SLEEP_MODE:
468 arg = (reg >> SLEEP_MODE_SHIFT) & SLEEP_MODE_MASK;
470 case PIN_CONFIG_INPUT_ENABLE:
471 arg = (reg >> SLEEP_INPUT_SHIFT) & SLEEP_INPUT_MASK;
473 case PIN_CONFIG_OUTPUT:
474 arg = reg & SLEEP_OUTPUT_MASK;
476 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
482 } else if (pin->type == MISC_PIN) {
484 case PIN_CONFIG_DRIVE_STRENGTH:
485 arg = (reg >> DRIVE_STRENGTH_SHIFT) &
488 case PIN_CONFIG_BIAS_PULL_DOWN:
489 /* combine sleep pull down and pull down config */
490 arg = ((reg >> SLEEP_PULL_DOWN_SHIFT) &
491 SLEEP_PULL_DOWN_MASK) << 16;
492 arg |= (reg >> PULL_DOWN_SHIFT) & PULL_DOWN_MASK;
494 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
495 arg = (reg >> INPUT_SCHMITT_SHIFT) & INPUT_SCHMITT_MASK;
497 case PIN_CONFIG_BIAS_PULL_UP:
498 /* combine sleep pull up and pull up config */
499 arg = ((reg >> SLEEP_PULL_UP_SHIFT) &
500 SLEEP_PULL_UP_MASK) << 16;
501 arg |= (reg >> PULL_UP_SHIFT) & PULL_UP_MASK;
503 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
513 *config = pinconf_to_config_packed(param, arg);
517 static unsigned int sprd_pinconf_drive(unsigned int mA)
519 unsigned int val = 0;
531 val |= BIT(19) | BIT(20);
537 val |= BIT(21) | BIT(19);
540 val |= BIT(21) | BIT(20);
543 val |= BIT(19) | BIT(20) | BIT(21);
549 val |= BIT(22) | BIT(19);
552 val |= BIT(22) | BIT(20);
555 val |= BIT(22) | BIT(20) | BIT(19);
558 val |= BIT(22) | BIT(21);
561 val |= BIT(22) | BIT(21) | BIT(19);
564 val |= BIT(22) | BIT(21) | BIT(20);
567 val |= BIT(22) | BIT(21) | BIT(20) | BIT(19);
576 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs,
577 unsigned int num_configs)
582 for (i = 0; i < num_configs; i++) {
583 param = pinconf_to_config_param(configs[i]);
584 if (param == PIN_CONFIG_SLEEP_HARDWARE_STATE)
591 static int sprd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin_id,
592 unsigned long *configs, unsigned int num_configs)
594 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
595 struct sprd_pin *pin = sprd_pinctrl_get_pin_by_id(pctl, pin_id);
596 bool is_sleep_config;
603 is_sleep_config = sprd_pinctrl_check_sleep_config(configs, num_configs);
605 for (i = 0; i < num_configs; i++) {
606 unsigned int param, arg, shift, mask, val;
608 param = pinconf_to_config_param(configs[i]);
609 arg = pinconf_to_config_argument(configs[i]);
614 if (pin->type == GLOBAL_CTRL_PIN &&
615 param == SPRD_PIN_CONFIG_CONTROL) {
617 } else if (pin->type == COMMON_PIN) {
619 case SPRD_PIN_CONFIG_SLEEP_MODE:
621 val |= AP_SLEEP_MODE;
622 if (arg & PUBCP_SLEEP)
623 val |= PUBCP_SLEEP_MODE;
624 if (arg & TGLDSP_SLEEP)
625 val |= TGLDSP_SLEEP_MODE;
626 if (arg & AGDSP_SLEEP)
627 val |= AGDSP_SLEEP_MODE;
629 mask = SLEEP_MODE_MASK;
630 shift = SLEEP_MODE_SHIFT;
632 case PIN_CONFIG_INPUT_ENABLE:
633 if (is_sleep_config == true) {
639 mask = SLEEP_INPUT_MASK;
640 shift = SLEEP_INPUT_SHIFT;
643 case PIN_CONFIG_OUTPUT:
644 if (is_sleep_config == true) {
646 mask = SLEEP_OUTPUT_MASK;
647 shift = SLEEP_OUTPUT_SHIFT;
650 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
655 } else if (pin->type == MISC_PIN) {
657 case PIN_CONFIG_DRIVE_STRENGTH:
658 if (arg < 2 || arg > 60)
661 val = sprd_pinconf_drive(arg);
662 mask = DRIVE_STRENGTH_MASK;
663 shift = DRIVE_STRENGTH_SHIFT;
665 case PIN_CONFIG_BIAS_PULL_DOWN:
666 if (is_sleep_config == true) {
667 val |= SLEEP_PULL_DOWN;
668 mask = SLEEP_PULL_DOWN_MASK;
669 shift = SLEEP_PULL_DOWN_SHIFT;
672 mask = PULL_DOWN_MASK;
673 shift = PULL_DOWN_SHIFT;
676 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
678 val |= INPUT_SCHMITT;
680 val &= ~INPUT_SCHMITT;
682 mask = INPUT_SCHMITT_MASK;
683 shift = INPUT_SCHMITT_SHIFT;
685 case PIN_CONFIG_BIAS_PULL_UP:
686 if (is_sleep_config == true) {
687 val |= SLEEP_PULL_UP;
688 mask = SLEEP_PULL_UP_MASK;
689 shift = SLEEP_PULL_UP_SHIFT;
693 else if (arg == 4700)
697 shift = PULL_UP_SHIFT;
700 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
709 if (pin->type == GLOBAL_CTRL_PIN) {
710 reg = readl((void __iomem *)pin->reg);
711 reg &= ~(PINCTRL_BIT_MASK(pin->bit_width)
713 reg |= (val & PINCTRL_BIT_MASK(pin->bit_width))
715 writel(reg, (void __iomem *)pin->reg);
717 reg = readl((void __iomem *)pin->reg);
718 reg &= ~(mask << shift);
720 writel(reg, (void __iomem *)pin->reg);
727 static int sprd_pinconf_group_get(struct pinctrl_dev *pctldev,
728 unsigned int selector, unsigned long *config)
730 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
731 struct sprd_pinctrl_soc_info *info = pctl->info;
732 struct sprd_pin_group *grp;
735 if (selector >= info->ngroups)
738 grp = &info->groups[selector];
739 pin_id = grp->pins[0];
741 return sprd_pinconf_get(pctldev, pin_id, config);
744 static int sprd_pinconf_group_set(struct pinctrl_dev *pctldev,
745 unsigned int selector,
746 unsigned long *configs,
747 unsigned int num_configs)
749 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
750 struct sprd_pinctrl_soc_info *info = pctl->info;
751 struct sprd_pin_group *grp;
754 if (selector >= info->ngroups)
757 grp = &info->groups[selector];
759 for (i = 0; i < grp->npins; i++) {
760 unsigned int pin_id = grp->pins[i];
762 ret = sprd_pinconf_set(pctldev, pin_id, configs, num_configs);
770 static int sprd_pinconf_get_config(struct pinctrl_dev *pctldev,
772 unsigned long *config)
774 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
775 struct sprd_pin *pin = sprd_pinctrl_get_pin_by_id(pctl, pin_id);
780 if (pin->type == GLOBAL_CTRL_PIN) {
781 *config = (readl((void __iomem *)pin->reg) >>
782 pin->bit_offset) & PINCTRL_BIT_MASK(pin->bit_width);
784 *config = readl((void __iomem *)pin->reg);
790 static void sprd_pinconf_dbg_show(struct pinctrl_dev *pctldev,
791 struct seq_file *s, unsigned int pin_id)
793 unsigned long config;
796 ret = sprd_pinconf_get_config(pctldev, pin_id, &config);
800 seq_printf(s, "0x%lx", config);
803 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
805 unsigned int selector)
807 struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
808 struct sprd_pinctrl_soc_info *info = pctl->info;
809 struct sprd_pin_group *grp;
810 unsigned long config;
814 if (selector >= info->ngroups)
817 grp = &info->groups[selector];
820 for (i = 0; i < grp->npins; i++, config++) {
821 unsigned int pin_id = grp->pins[i];
823 name = pin_get_name(pctldev, pin_id);
824 ret = sprd_pinconf_get_config(pctldev, pin_id, &config);
828 seq_printf(s, "%s: 0x%lx ", name, config);
832 static const struct pinconf_ops sprd_pinconf_ops = {
834 .pin_config_get = sprd_pinconf_get,
835 .pin_config_set = sprd_pinconf_set,
836 .pin_config_group_get = sprd_pinconf_group_get,
837 .pin_config_group_set = sprd_pinconf_group_set,
838 .pin_config_dbg_show = sprd_pinconf_dbg_show,
839 .pin_config_group_dbg_show = sprd_pinconf_group_dbg_show,
842 static const struct pinconf_generic_params sprd_dt_params[] = {
843 {"sprd,control", SPRD_PIN_CONFIG_CONTROL, 0},
844 {"sprd,sleep-mode", SPRD_PIN_CONFIG_SLEEP_MODE, 0},
847 #ifdef CONFIG_DEBUG_FS
848 static const struct pin_config_item sprd_conf_items[] = {
849 PCONFDUMP(SPRD_PIN_CONFIG_CONTROL, "global control", NULL, true),
850 PCONFDUMP(SPRD_PIN_CONFIG_SLEEP_MODE, "sleep mode", NULL, true),
854 static struct pinctrl_desc sprd_pinctrl_desc = {
855 .pctlops = &sprd_pctrl_ops,
856 .pmxops = &sprd_pmx_ops,
857 .confops = &sprd_pinconf_ops,
858 .num_custom_params = ARRAY_SIZE(sprd_dt_params),
859 .custom_params = sprd_dt_params,
860 #ifdef CONFIG_DEBUG_FS
861 .custom_conf_items = sprd_conf_items,
863 .owner = THIS_MODULE,
866 static int sprd_pinctrl_parse_groups(struct device_node *np,
867 struct sprd_pinctrl *sprd_pctl,
868 struct sprd_pin_group *grp)
870 struct property *prop;
871 const char *pin_name;
874 ret = of_property_count_strings(np, "pins");
878 grp->name = np->name;
880 grp->pins = devm_kcalloc(sprd_pctl->dev,
881 grp->npins, sizeof(unsigned int),
886 of_property_for_each_string(np, "pins", prop, pin_name) {
887 ret = sprd_pinctrl_get_id_by_name(sprd_pctl, pin_name);
889 grp->pins[i++] = ret;
892 for (i = 0; i < grp->npins; i++) {
893 dev_dbg(sprd_pctl->dev,
894 "Group[%s] contains [%d] pins: id = %d\n",
895 grp->name, grp->npins, grp->pins[i]);
901 static unsigned int sprd_pinctrl_get_groups(struct device_node *np)
903 struct device_node *child;
904 unsigned int group_cnt, cnt;
906 group_cnt = of_get_child_count(np);
908 for_each_child_of_node(np, child) {
909 cnt = of_get_child_count(child);
917 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl *sprd_pctl)
919 struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
920 struct device_node *np = sprd_pctl->dev->of_node;
921 struct device_node *child, *sub_child;
922 struct sprd_pin_group *grp;
929 info->ngroups = sprd_pinctrl_get_groups(np);
933 info->groups = devm_kcalloc(sprd_pctl->dev,
935 sizeof(struct sprd_pin_group),
940 info->grp_names = devm_kcalloc(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_kcalloc(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_kcalloc(&pdev->dev,
1061 pinctrl_info->npins,
1062 sizeof(struct pinctrl_pin_desc),
1067 for (i = 0; i < pinctrl_info->npins; i++) {
1068 pin_desc[i].number = pinctrl_info->pins[i].number;
1069 pin_desc[i].name = pinctrl_info->pins[i].name;
1070 pin_desc[i].drv_data = pinctrl_info;
1073 sprd_pinctrl_desc.pins = pin_desc;
1074 sprd_pinctrl_desc.name = dev_name(&pdev->dev);
1075 sprd_pinctrl_desc.npins = pinctrl_info->npins;
1077 sprd_pctl->pctl = pinctrl_register(&sprd_pinctrl_desc,
1078 &pdev->dev, (void *)sprd_pctl);
1079 if (IS_ERR(sprd_pctl->pctl)) {
1080 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1081 return PTR_ERR(sprd_pctl->pctl);
1084 ret = sprd_pinctrl_parse_dt(sprd_pctl);
1086 dev_err(&pdev->dev, "fail to parse dt properties\n");
1087 pinctrl_unregister(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");