pinctrl: sprd: Allow the SPRD pinctrl driver building into a module
[platform/kernel/linux-rpi.git] / drivers / pinctrl / sprd / pinctrl-sprd.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Spreadtrum pin controller driver
4  * Copyright (C) 2017 Spreadtrum  - http://www.spreadtrum.com
5  */
6
7 #include <linux/debugfs.h>
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.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>
22
23 #include "../core.h"
24 #include "../pinmux.h"
25 #include "../pinconf.h"
26 #include "../pinctrl-utils.h"
27 #include "pinctrl-sprd.h"
28
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
33
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
39
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
47
48 #define SLEEP_INPUT                     BIT(1)
49 #define SLEEP_INPUT_MASK                0x1
50 #define SLEEP_INPUT_SHIFT               1
51
52 #define SLEEP_OUTPUT                    BIT(0)
53 #define SLEEP_OUTPUT_MASK               0x1
54 #define SLEEP_OUTPUT_SHIFT              0
55
56 #define DRIVE_STRENGTH_MASK             GENMASK(3, 0)
57 #define DRIVE_STRENGTH_SHIFT            19
58
59 #define SLEEP_PULL_DOWN                 BIT(2)
60 #define SLEEP_PULL_DOWN_MASK            0x1
61 #define SLEEP_PULL_DOWN_SHIFT           2
62
63 #define PULL_DOWN                       BIT(6)
64 #define PULL_DOWN_MASK                  0x1
65 #define PULL_DOWN_SHIFT                 6
66
67 #define SLEEP_PULL_UP                   BIT(3)
68 #define SLEEP_PULL_UP_MASK              0x1
69 #define SLEEP_PULL_UP_SHIFT             3
70
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
75
76 #define INPUT_SCHMITT                   BIT(11)
77 #define INPUT_SCHMITT_MASK              0x1
78 #define INPUT_SCHMITT_SHIFT             11
79
80 enum pin_sleep_mode {
81         AP_SLEEP = BIT(0),
82         PUBCP_SLEEP = BIT(1),
83         TGLDSP_SLEEP = BIT(2),
84         AGDSP_SLEEP = BIT(3),
85         CM4_SLEEP = BIT(4),
86 };
87
88 enum pin_func_sel {
89         PIN_FUNC_1,
90         PIN_FUNC_2,
91         PIN_FUNC_3,
92         PIN_FUNC_4,
93         PIN_FUNC_MAX,
94 };
95
96 /**
97  * struct sprd_pin: represent one pin's description
98  * @name: pin name
99  * @number: pin number
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
104  */
105 struct sprd_pin {
106         const char *name;
107         unsigned int number;
108         enum pin_type type;
109         unsigned long reg;
110         unsigned long bit_offset;
111         unsigned long bit_width;
112 };
113
114 /**
115  * struct sprd_pin_group: represent one group's description
116  * @name: group name
117  * @npins: pin numbers of this group
118  * @pins: pointer to pins array
119  */
120 struct sprd_pin_group {
121         const char *name;
122         unsigned int npins;
123         unsigned int *pins;
124 };
125
126 /**
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
133  */
134 struct sprd_pinctrl_soc_info {
135         struct sprd_pin_group *groups;
136         unsigned int ngroups;
137         struct sprd_pin *pins;
138         unsigned int npins;
139         const char **grp_names;
140 };
141
142 /**
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
148  */
149 struct sprd_pinctrl {
150         struct device *dev;
151         struct pinctrl_dev *pctl;
152         void __iomem *base;
153         struct sprd_pinctrl_soc_info *info;
154 };
155
156 #define SPRD_PIN_CONFIG_CONTROL         (PIN_CONFIG_END + 1)
157 #define SPRD_PIN_CONFIG_SLEEP_MODE      (PIN_CONFIG_END + 2)
158
159 static int sprd_pinctrl_get_id_by_name(struct sprd_pinctrl *sprd_pctl,
160                                        const char *name)
161 {
162         struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
163         int i;
164
165         for (i = 0; i < info->npins; i++) {
166                 if (!strcmp(info->pins[i].name, name))
167                         return info->pins[i].number;
168         }
169
170         return -ENODEV;
171 }
172
173 static struct sprd_pin *
174 sprd_pinctrl_get_pin_by_id(struct sprd_pinctrl *sprd_pctl, unsigned int id)
175 {
176         struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
177         struct sprd_pin *pin = NULL;
178         int i;
179
180         for (i = 0; i < info->npins; i++) {
181                 if (info->pins[i].number == id) {
182                         pin = &info->pins[i];
183                         break;
184                 }
185         }
186
187         return pin;
188 }
189
190 static const struct sprd_pin_group *
191 sprd_pinctrl_find_group_by_name(struct sprd_pinctrl *sprd_pctl,
192                                 const char *name)
193 {
194         struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
195         const struct sprd_pin_group *grp = NULL;
196         int i;
197
198         for (i = 0; i < info->ngroups; i++) {
199                 if (!strcmp(info->groups[i].name, name)) {
200                         grp = &info->groups[i];
201                         break;
202                 }
203         }
204
205         return grp;
206 }
207
208 static int sprd_pctrl_group_count(struct pinctrl_dev *pctldev)
209 {
210         struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
211         struct sprd_pinctrl_soc_info *info = pctl->info;
212
213         return info->ngroups;
214 }
215
216 static const char *sprd_pctrl_group_name(struct pinctrl_dev *pctldev,
217                                          unsigned int selector)
218 {
219         struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
220         struct sprd_pinctrl_soc_info *info = pctl->info;
221
222         return info->groups[selector].name;
223 }
224
225 static int sprd_pctrl_group_pins(struct pinctrl_dev *pctldev,
226                                  unsigned int selector,
227                                  const unsigned int **pins,
228                                  unsigned int *npins)
229 {
230         struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
231         struct sprd_pinctrl_soc_info *info = pctl->info;
232
233         if (selector >= info->ngroups)
234                 return -EINVAL;
235
236         *pins = info->groups[selector].pins;
237         *npins = info->groups[selector].npins;
238
239         return 0;
240 }
241
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)
246 {
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;
255         int ret;
256
257         grp = sprd_pinctrl_find_group_by_name(pctl, np->name);
258         if (!grp) {
259                 dev_err(pctl->dev, "unable to find group for node %s\n",
260                         of_node_full_name(np));
261                 return -EINVAL;
262         }
263
264         ret = of_property_count_strings(np, "pins");
265         if (ret < 0)
266                 return ret;
267
268         if (ret == 1)
269                 type = PIN_MAP_TYPE_CONFIGS_PIN;
270         else
271                 type = PIN_MAP_TYPE_CONFIGS_GROUP;
272
273         ret = of_property_read_string(np, "function", &function);
274         if (ret < 0) {
275                 if (ret != -EINVAL)
276                         dev_err(pctl->dev,
277                                 "%s: could not parse property function\n",
278                                 of_node_full_name(np));
279                 function = NULL;
280         }
281
282         ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
283                                               &num_configs);
284         if (ret < 0) {
285                 dev_err(pctl->dev, "%s: could not parse node property\n",
286                         of_node_full_name(np));
287                 return ret;
288         }
289
290         *map = NULL;
291         *num_maps = 0;
292
293         if (function != NULL)
294                 reserve++;
295         if (num_configs)
296                 reserve++;
297
298         ret = pinctrl_utils_reserve_map(pctldev, map, &reserved_maps,
299                                         num_maps, reserve);
300         if (ret < 0)
301                 goto out;
302
303         if (function) {
304                 ret = pinctrl_utils_add_map_mux(pctldev, map,
305                                                 &reserved_maps, num_maps,
306                                                 grp->name, function);
307                 if (ret < 0)
308                         goto out;
309         }
310
311         if (num_configs) {
312                 const char *group_or_pin;
313                 unsigned int pin_id;
314
315                 if (type == PIN_MAP_TYPE_CONFIGS_PIN) {
316                         pin_id = grp->pins[0];
317                         group_or_pin = pin_get_name(pctldev, pin_id);
318                 } else {
319                         group_or_pin = grp->name;
320                 }
321
322                 ret = pinctrl_utils_add_map_configs(pctldev, map,
323                                                     &reserved_maps, num_maps,
324                                                     group_or_pin, configs,
325                                                     num_configs, type);
326         }
327
328 out:
329         kfree(configs);
330         return ret;
331 }
332
333 static void sprd_pctrl_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
334                                 unsigned int offset)
335 {
336         seq_printf(s, "%s", dev_name(pctldev->dev));
337 }
338
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,
346 };
347
348 static int sprd_pmx_get_function_count(struct pinctrl_dev *pctldev)
349 {
350         return PIN_FUNC_MAX;
351 }
352
353 static const char *sprd_pmx_get_function_name(struct pinctrl_dev *pctldev,
354                                               unsigned int selector)
355 {
356         switch (selector) {
357         case PIN_FUNC_1:
358                 return "func1";
359         case PIN_FUNC_2:
360                 return "func2";
361         case PIN_FUNC_3:
362                 return "func3";
363         case PIN_FUNC_4:
364                 return "func4";
365         default:
366                 return "null";
367         }
368 }
369
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)
374 {
375         struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
376         struct sprd_pinctrl_soc_info *info = pctl->info;
377
378         *groups = info->grp_names;
379         *num_groups = info->ngroups;
380
381         return 0;
382 }
383
384 static int sprd_pmx_set_mux(struct pinctrl_dev *pctldev,
385                             unsigned int func_selector,
386                             unsigned int group_selector)
387 {
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;
392         unsigned long reg;
393         unsigned int val = 0;
394
395         if (group_selector >= info->ngroups)
396                 return -EINVAL;
397
398         switch (func_selector) {
399         case PIN_FUNC_1:
400                 val &= PIN_FUNC_SEL_1;
401                 break;
402         case PIN_FUNC_2:
403                 val |= PIN_FUNC_SEL_2;
404                 break;
405         case PIN_FUNC_3:
406                 val |= PIN_FUNC_SEL_3;
407                 break;
408         case PIN_FUNC_4:
409                 val |= PIN_FUNC_SEL_4;
410                 break;
411         default:
412                 break;
413         }
414
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);
418
419                 if (!pin || pin->type != COMMON_PIN)
420                         continue;
421
422                 reg = readl((void __iomem *)pin->reg);
423                 reg &= ~PIN_FUNC_MASK;
424                 reg |= val;
425                 writel(reg, (void __iomem *)pin->reg);
426         }
427
428         return 0;
429 }
430
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,
436 };
437
438 static int sprd_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin_id,
439                             unsigned long *config)
440 {
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;
445
446         if (!pin)
447                 return -EINVAL;
448
449         if (pin->type == GLOBAL_CTRL_PIN) {
450                 reg = (readl((void __iomem *)pin->reg) >>
451                            pin->bit_offset) & PINCTRL_BIT_MASK(pin->bit_width);
452         } else {
453                 reg = readl((void __iomem *)pin->reg);
454         }
455
456         if (pin->type == GLOBAL_CTRL_PIN &&
457             param == SPRD_PIN_CONFIG_CONTROL) {
458                 arg = reg;
459         } else if (pin->type == COMMON_PIN || pin->type == MISC_PIN) {
460                 switch (param) {
461                 case SPRD_PIN_CONFIG_SLEEP_MODE:
462                         arg = (reg >> SLEEP_MODE_SHIFT) & SLEEP_MODE_MASK;
463                         break;
464                 case PIN_CONFIG_INPUT_ENABLE:
465                         arg = (reg >> SLEEP_INPUT_SHIFT) & SLEEP_INPUT_MASK;
466                         break;
467                 case PIN_CONFIG_OUTPUT:
468                         arg = reg & SLEEP_OUTPUT_MASK;
469                         break;
470                 case PIN_CONFIG_DRIVE_STRENGTH:
471                         arg = (reg >> DRIVE_STRENGTH_SHIFT) &
472                                 DRIVE_STRENGTH_MASK;
473                         break;
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;
479                         break;
480                 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
481                         arg = (reg >> INPUT_SCHMITT_SHIFT) & INPUT_SCHMITT_MASK;
482                         break;
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;
488                         break;
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)))
492                                 return -EINVAL;
493
494                         arg = 1;
495                         break;
496                 case PIN_CONFIG_SLEEP_HARDWARE_STATE:
497                         arg = 0;
498                         break;
499                 default:
500                         return -ENOTSUPP;
501                 }
502         } else {
503                 return -ENOTSUPP;
504         }
505
506         *config = pinconf_to_config_packed(param, arg);
507         return 0;
508 }
509
510 static unsigned int sprd_pinconf_drive(unsigned int mA)
511 {
512         unsigned int val = 0;
513
514         switch (mA) {
515         case 2:
516                 break;
517         case 4:
518                 val |= BIT(19);
519                 break;
520         case 6:
521                 val |= BIT(20);
522                 break;
523         case 8:
524                 val |= BIT(19) | BIT(20);
525                 break;
526         case 10:
527                 val |= BIT(21);
528                 break;
529         case 12:
530                 val |= BIT(21) | BIT(19);
531                 break;
532         case 14:
533                 val |= BIT(21) | BIT(20);
534                 break;
535         case 16:
536                 val |= BIT(19) | BIT(20) | BIT(21);
537                 break;
538         case 20:
539                 val |= BIT(22);
540                 break;
541         case 21:
542                 val |= BIT(22) | BIT(19);
543                 break;
544         case 24:
545                 val |= BIT(22) | BIT(20);
546                 break;
547         case 25:
548                 val |= BIT(22) | BIT(20) | BIT(19);
549                 break;
550         case 27:
551                 val |= BIT(22) | BIT(21);
552                 break;
553         case 29:
554                 val |= BIT(22) | BIT(21) | BIT(19);
555                 break;
556         case 31:
557                 val |= BIT(22) | BIT(21) | BIT(20);
558                 break;
559         case 33:
560                 val |= BIT(22) | BIT(21) | BIT(20) | BIT(19);
561                 break;
562         default:
563                 break;
564         }
565
566         return val;
567 }
568
569 static bool sprd_pinctrl_check_sleep_config(unsigned long *configs,
570                                             unsigned int num_configs)
571 {
572         unsigned int param;
573         int i;
574
575         for (i = 0; i < num_configs; i++) {
576                 param = pinconf_to_config_param(configs[i]);
577                 if (param == PIN_CONFIG_SLEEP_HARDWARE_STATE)
578                         return true;
579         }
580
581         return false;
582 }
583
584 static int sprd_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin_id,
585                             unsigned long *configs, unsigned int num_configs)
586 {
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;
590         unsigned long reg;
591         int i;
592
593         if (!pin)
594                 return -EINVAL;
595
596         is_sleep_config = sprd_pinctrl_check_sleep_config(configs, num_configs);
597
598         for (i = 0; i < num_configs; i++) {
599                 unsigned int param, arg, shift, mask, val;
600
601                 param = pinconf_to_config_param(configs[i]);
602                 arg = pinconf_to_config_argument(configs[i]);
603
604                 val = 0;
605                 shift = 0;
606                 mask = 0;
607                 if (pin->type == GLOBAL_CTRL_PIN &&
608                     param == SPRD_PIN_CONFIG_CONTROL) {
609                         val = arg;
610                 } else if (pin->type == COMMON_PIN || pin->type == MISC_PIN) {
611                         switch (param) {
612                         case SPRD_PIN_CONFIG_SLEEP_MODE:
613                                 if (arg & AP_SLEEP)
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;
621                                 if (arg & CM4_SLEEP)
622                                         val |= CM4_SLEEP_MODE;
623
624                                 mask = SLEEP_MODE_MASK;
625                                 shift = SLEEP_MODE_SHIFT;
626                                 break;
627                         case PIN_CONFIG_INPUT_ENABLE:
628                                 if (is_sleep_config == true) {
629                                         if (arg > 0)
630                                                 val |= SLEEP_INPUT;
631                                         else
632                                                 val &= ~SLEEP_INPUT;
633
634                                         mask = SLEEP_INPUT_MASK;
635                                         shift = SLEEP_INPUT_SHIFT;
636                                 }
637                                 break;
638                         case PIN_CONFIG_OUTPUT:
639                                 if (is_sleep_config == true) {
640                                         val |= SLEEP_OUTPUT;
641                                         mask = SLEEP_OUTPUT_MASK;
642                                         shift = SLEEP_OUTPUT_SHIFT;
643                                 }
644                                 break;
645                         case PIN_CONFIG_DRIVE_STRENGTH:
646                                 if (arg < 2 || arg > 60)
647                                         return -EINVAL;
648
649                                 val = sprd_pinconf_drive(arg);
650                                 mask = DRIVE_STRENGTH_MASK;
651                                 shift = DRIVE_STRENGTH_SHIFT;
652                                 break;
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;
658                                 } else {
659                                         val |= PULL_DOWN;
660                                         mask = PULL_DOWN_MASK;
661                                         shift = PULL_DOWN_SHIFT;
662                                 }
663                                 break;
664                         case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
665                                 if (arg > 0)
666                                         val |= INPUT_SCHMITT;
667                                 else
668                                         val &= ~INPUT_SCHMITT;
669
670                                 mask = INPUT_SCHMITT_MASK;
671                                 shift = INPUT_SCHMITT_SHIFT;
672                                 break;
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;
678                                 } else {
679                                         if (arg == 20000)
680                                                 val |= PULL_UP_20K;
681                                         else if (arg == 4700)
682                                                 val |= PULL_UP_4_7K;
683
684                                         mask = PULL_UP_MASK;
685                                         shift = PULL_UP_SHIFT;
686                                 }
687                                 break;
688                         case PIN_CONFIG_BIAS_DISABLE:
689                                 if (is_sleep_config == true) {
690                                         val = shift = 0;
691                                         mask = SLEEP_PULL_DOWN | SLEEP_PULL_UP;
692                                 } else {
693                                         val = shift = 0;
694                                         mask = PULL_DOWN | PULL_UP_20K |
695                                                 PULL_UP_4_7K;
696                                 }
697                                 break;
698                         case PIN_CONFIG_SLEEP_HARDWARE_STATE:
699                                 continue;
700                         default:
701                                 return -ENOTSUPP;
702                         }
703                 } else {
704                         return -ENOTSUPP;
705                 }
706
707                 if (pin->type == GLOBAL_CTRL_PIN) {
708                         reg = readl((void __iomem *)pin->reg);
709                         reg &= ~(PINCTRL_BIT_MASK(pin->bit_width)
710                                 << pin->bit_offset);
711                         reg |= (val & PINCTRL_BIT_MASK(pin->bit_width))
712                                 << pin->bit_offset;
713                         writel(reg, (void __iomem *)pin->reg);
714                 } else {
715                         reg = readl((void __iomem *)pin->reg);
716                         reg &= ~(mask << shift);
717                         reg |= val;
718                         writel(reg, (void __iomem *)pin->reg);
719                 }
720         }
721
722         return 0;
723 }
724
725 static int sprd_pinconf_group_get(struct pinctrl_dev *pctldev,
726                                   unsigned int selector, unsigned long *config)
727 {
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;
731         unsigned int pin_id;
732
733         if (selector >= info->ngroups)
734                 return -EINVAL;
735
736         grp = &info->groups[selector];
737         pin_id = grp->pins[0];
738
739         return sprd_pinconf_get(pctldev, pin_id, config);
740 }
741
742 static int sprd_pinconf_group_set(struct pinctrl_dev *pctldev,
743                                   unsigned int selector,
744                                   unsigned long *configs,
745                                   unsigned int num_configs)
746 {
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;
750         int ret, i;
751
752         if (selector >= info->ngroups)
753                 return -EINVAL;
754
755         grp = &info->groups[selector];
756
757         for (i = 0; i < grp->npins; i++) {
758                 unsigned int pin_id = grp->pins[i];
759
760                 ret = sprd_pinconf_set(pctldev, pin_id, configs, num_configs);
761                 if (ret)
762                         return ret;
763         }
764
765         return 0;
766 }
767
768 static int sprd_pinconf_get_config(struct pinctrl_dev *pctldev,
769                                    unsigned int pin_id,
770                                    unsigned long *config)
771 {
772         struct sprd_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
773         struct sprd_pin *pin = sprd_pinctrl_get_pin_by_id(pctl, pin_id);
774
775         if (!pin)
776                 return -EINVAL;
777
778         if (pin->type == GLOBAL_CTRL_PIN) {
779                 *config = (readl((void __iomem *)pin->reg) >>
780                            pin->bit_offset) & PINCTRL_BIT_MASK(pin->bit_width);
781         } else {
782                 *config = readl((void __iomem *)pin->reg);
783         }
784
785         return 0;
786 }
787
788 static void sprd_pinconf_dbg_show(struct pinctrl_dev *pctldev,
789                                   struct seq_file *s, unsigned int pin_id)
790 {
791         unsigned long config;
792         int ret;
793
794         ret = sprd_pinconf_get_config(pctldev, pin_id, &config);
795         if (ret)
796                 return;
797
798         seq_printf(s, "0x%lx", config);
799 }
800
801 static void sprd_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
802                                         struct seq_file *s,
803                                         unsigned int selector)
804 {
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;
809         const char *name;
810         int i, ret;
811
812         if (selector >= info->ngroups)
813                 return;
814
815         grp = &info->groups[selector];
816
817         seq_putc(s, '\n');
818         for (i = 0; i < grp->npins; i++, config++) {
819                 unsigned int pin_id = grp->pins[i];
820
821                 name = pin_get_name(pctldev, pin_id);
822                 ret = sprd_pinconf_get_config(pctldev, pin_id, &config);
823                 if (ret)
824                         return;
825
826                 seq_printf(s, "%s: 0x%lx ", name, config);
827         }
828 }
829
830 static const struct pinconf_ops sprd_pinconf_ops = {
831         .is_generic = true,
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,
838 };
839
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},
843 };
844
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),
849 };
850 #endif
851
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,
860 #endif
861         .owner = THIS_MODULE,
862 };
863
864 static int sprd_pinctrl_parse_groups(struct device_node *np,
865                                      struct sprd_pinctrl *sprd_pctl,
866                                      struct sprd_pin_group *grp)
867 {
868         struct property *prop;
869         const char *pin_name;
870         int ret, i = 0;
871
872         ret = of_property_count_strings(np, "pins");
873         if (ret < 0)
874                 return ret;
875
876         grp->name = np->name;
877         grp->npins = ret;
878         grp->pins = devm_kcalloc(sprd_pctl->dev,
879                                  grp->npins, sizeof(unsigned int),
880                                  GFP_KERNEL);
881         if (!grp->pins)
882                 return -ENOMEM;
883
884         of_property_for_each_string(np, "pins", prop, pin_name) {
885                 ret = sprd_pinctrl_get_id_by_name(sprd_pctl, pin_name);
886                 if (ret >= 0)
887                         grp->pins[i++] = ret;
888         }
889
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]);
894         }
895
896         return 0;
897 }
898
899 static unsigned int sprd_pinctrl_get_groups(struct device_node *np)
900 {
901         struct device_node *child;
902         unsigned int group_cnt, cnt;
903
904         group_cnt = of_get_child_count(np);
905
906         for_each_child_of_node(np, child) {
907                 cnt = of_get_child_count(child);
908                 if (cnt > 0)
909                         group_cnt += cnt;
910         }
911
912         return group_cnt;
913 }
914
915 static int sprd_pinctrl_parse_dt(struct sprd_pinctrl *sprd_pctl)
916 {
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;
921         const char **temp;
922         int ret;
923
924         if (!np)
925                 return -ENODEV;
926
927         info->ngroups = sprd_pinctrl_get_groups(np);
928         if (!info->ngroups)
929                 return 0;
930
931         info->groups = devm_kcalloc(sprd_pctl->dev,
932                                     info->ngroups,
933                                     sizeof(struct sprd_pin_group),
934                                     GFP_KERNEL);
935         if (!info->groups)
936                 return -ENOMEM;
937
938         info->grp_names = devm_kcalloc(sprd_pctl->dev,
939                                        info->ngroups, sizeof(char *),
940                                        GFP_KERNEL);
941         if (!info->grp_names)
942                 return -ENOMEM;
943
944         temp = info->grp_names;
945         grp = info->groups;
946
947         for_each_child_of_node(np, child) {
948                 ret = sprd_pinctrl_parse_groups(child, sprd_pctl, grp);
949                 if (ret) {
950                         of_node_put(child);
951                         return ret;
952                 }
953
954                 *temp++ = grp->name;
955                 grp++;
956
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,
960                                                                 sprd_pctl, grp);
961                                 if (ret) {
962                                         of_node_put(sub_child);
963                                         of_node_put(child);
964                                         return ret;
965                                 }
966
967                                 *temp++ = grp->name;
968                                 grp++;
969                         }
970                 }
971         }
972
973         return 0;
974 }
975
976 static int sprd_pinctrl_add_pins(struct sprd_pinctrl *sprd_pctl,
977                                  struct sprd_pins_info *sprd_soc_pin_info,
978                                  int pins_cnt)
979 {
980         struct sprd_pinctrl_soc_info *info = sprd_pctl->info;
981         unsigned int ctrl_pin = 0, com_pin = 0;
982         struct sprd_pin *pin;
983         int i;
984
985         info->npins = pins_cnt;
986         info->pins = devm_kcalloc(sprd_pctl->dev,
987                                   info->npins, sizeof(struct sprd_pin),
988                                   GFP_KERNEL);
989         if (!info->pins)
990                 return -ENOMEM;
991
992         for (i = 0, pin = info->pins; i < info->npins; i++, pin++) {
993                 unsigned int reg;
994
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;
1004                         ctrl_pin++;
1005                 } else if (pin->type == COMMON_PIN) {
1006                         pin->reg = (unsigned long)sprd_pctl->base +
1007                                 PINCTRL_REG_OFFSET + PINCTRL_REG_LEN *
1008                                 (i - ctrl_pin);
1009                         com_pin++;
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);
1014                 }
1015         }
1016
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);
1022         }
1023
1024         return 0;
1025 }
1026
1027 int sprd_pinctrl_core_probe(struct platform_device *pdev,
1028                             struct sprd_pins_info *sprd_soc_pin_info,
1029                             int pins_cnt)
1030 {
1031         struct sprd_pinctrl *sprd_pctl;
1032         struct sprd_pinctrl_soc_info *pinctrl_info;
1033         struct pinctrl_pin_desc *pin_desc;
1034         int ret, i;
1035
1036         sprd_pctl = devm_kzalloc(&pdev->dev, sizeof(struct sprd_pinctrl),
1037                                  GFP_KERNEL);
1038         if (!sprd_pctl)
1039                 return -ENOMEM;
1040
1041         sprd_pctl->base = devm_platform_ioremap_resource(pdev, 0);
1042         if (IS_ERR(sprd_pctl->base))
1043                 return PTR_ERR(sprd_pctl->base);
1044
1045         pinctrl_info = devm_kzalloc(&pdev->dev,
1046                                     sizeof(struct sprd_pinctrl_soc_info),
1047                                     GFP_KERNEL);
1048         if (!pinctrl_info)
1049                 return -ENOMEM;
1050
1051         sprd_pctl->info = pinctrl_info;
1052         sprd_pctl->dev = &pdev->dev;
1053         platform_set_drvdata(pdev, sprd_pctl);
1054
1055         ret = sprd_pinctrl_add_pins(sprd_pctl, sprd_soc_pin_info, pins_cnt);
1056         if (ret) {
1057                 dev_err(&pdev->dev, "fail to add pins information\n");
1058                 return ret;
1059         }
1060
1061         ret = sprd_pinctrl_parse_dt(sprd_pctl);
1062         if (ret) {
1063                 dev_err(&pdev->dev, "fail to parse dt properties\n");
1064                 return ret;
1065         }
1066
1067         pin_desc = devm_kcalloc(&pdev->dev,
1068                                 pinctrl_info->npins,
1069                                 sizeof(struct pinctrl_pin_desc),
1070                                 GFP_KERNEL);
1071         if (!pin_desc)
1072                 return -ENOMEM;
1073
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;
1078         }
1079
1080         sprd_pinctrl_desc.pins = pin_desc;
1081         sprd_pinctrl_desc.name = dev_name(&pdev->dev);
1082         sprd_pinctrl_desc.npins = pinctrl_info->npins;
1083
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);
1089         }
1090
1091         return 0;
1092 }
1093 EXPORT_SYMBOL_GPL(sprd_pinctrl_core_probe);
1094
1095 int sprd_pinctrl_remove(struct platform_device *pdev)
1096 {
1097         struct sprd_pinctrl *sprd_pctl = platform_get_drvdata(pdev);
1098
1099         pinctrl_unregister(sprd_pctl->pctl);
1100         return 0;
1101 }
1102 EXPORT_SYMBOL_GPL(sprd_pinctrl_remove);
1103
1104 void sprd_pinctrl_shutdown(struct platform_device *pdev)
1105 {
1106         struct pinctrl *pinctl;
1107         struct pinctrl_state *state;
1108
1109         pinctl = devm_pinctrl_get(&pdev->dev);
1110         if (IS_ERR(pinctl))
1111                 return;
1112         state = pinctrl_lookup_state(pinctl, "shutdown");
1113         if (IS_ERR(state))
1114                 return;
1115         pinctrl_select_state(pinctl, state);
1116 }
1117 EXPORT_SYMBOL_GPL(sprd_pinctrl_shutdown);
1118
1119 MODULE_DESCRIPTION("SPREADTRUM Pin Controller Driver");
1120 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1121 MODULE_LICENSE("GPL v2");