pinctrl: uniphier: introduce capability flag
[cascardo/linux.git] / drivers / pinctrl / uniphier / pinctrl-uniphier-core.c
1 /*
2  * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/export.h>
16 #include <linux/mfd/syscon.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/platform_device.h>
22 #include <linux/regmap.h>
23
24 #include "../core.h"
25 #include "../pinctrl-utils.h"
26 #include "pinctrl-uniphier.h"
27
28 struct uniphier_pinctrl_priv {
29         struct pinctrl_desc pctldesc;
30         struct pinctrl_dev *pctldev;
31         struct regmap *regmap;
32         struct uniphier_pinctrl_socdata *socdata;
33 };
34
35 static int uniphier_pctl_get_groups_count(struct pinctrl_dev *pctldev)
36 {
37         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
38
39         return priv->socdata->groups_count;
40 }
41
42 static const char *uniphier_pctl_get_group_name(struct pinctrl_dev *pctldev,
43                                                 unsigned selector)
44 {
45         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
46
47         return priv->socdata->groups[selector].name;
48 }
49
50 static int uniphier_pctl_get_group_pins(struct pinctrl_dev *pctldev,
51                                         unsigned selector,
52                                         const unsigned **pins,
53                                         unsigned *num_pins)
54 {
55         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
56
57         *pins = priv->socdata->groups[selector].pins;
58         *num_pins = priv->socdata->groups[selector].num_pins;
59
60         return 0;
61 }
62
63 #ifdef CONFIG_DEBUG_FS
64 static void uniphier_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
65                                        struct seq_file *s, unsigned offset)
66 {
67         const struct pin_desc *desc = pin_desc_get(pctldev, offset);
68         const char *pull_dir, *drv_type;
69
70         switch (uniphier_pin_get_pull_dir(desc->drv_data)) {
71         case UNIPHIER_PIN_PULL_UP:
72                 pull_dir = "UP";
73                 break;
74         case UNIPHIER_PIN_PULL_DOWN:
75                 pull_dir = "DOWN";
76                 break;
77         case UNIPHIER_PIN_PULL_UP_FIXED:
78                 pull_dir = "UP(FIXED)";
79                 break;
80         case UNIPHIER_PIN_PULL_DOWN_FIXED:
81                 pull_dir = "DOWN(FIXED)";
82                 break;
83         case UNIPHIER_PIN_PULL_NONE:
84                 pull_dir = "NONE";
85                 break;
86         default:
87                 BUG();
88         }
89
90         switch (uniphier_pin_get_drv_type(desc->drv_data)) {
91         case UNIPHIER_PIN_DRV_1BIT:
92                 drv_type = "4/8(mA)";
93                 break;
94         case UNIPHIER_PIN_DRV_2BIT:
95                 drv_type = "8/12/16/20(mA)";
96                 break;
97         case UNIPHIER_PIN_DRV_3BIT:
98                 drv_type = "4/5/7/9/11/12/14/16(mA)";
99                 break;
100         case UNIPHIER_PIN_DRV_FIXED4:
101                 drv_type = "4(mA)";
102                 break;
103         case UNIPHIER_PIN_DRV_FIXED5:
104                 drv_type = "5(mA)";
105                 break;
106         case UNIPHIER_PIN_DRV_FIXED8:
107                 drv_type = "8(mA)";
108                 break;
109         case UNIPHIER_PIN_DRV_NONE:
110                 drv_type = "NONE";
111                 break;
112         default:
113                 BUG();
114         }
115
116         seq_printf(s, " PULL_DIR=%s  DRV_TYPE=%s", pull_dir, drv_type);
117 }
118 #endif
119
120 static const struct pinctrl_ops uniphier_pctlops = {
121         .get_groups_count = uniphier_pctl_get_groups_count,
122         .get_group_name = uniphier_pctl_get_group_name,
123         .get_group_pins = uniphier_pctl_get_group_pins,
124 #ifdef CONFIG_DEBUG_FS
125         .pin_dbg_show = uniphier_pctl_pin_dbg_show,
126 #endif
127         .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
128         .dt_free_map = pinctrl_utils_free_map,
129 };
130
131 static int uniphier_conf_pin_bias_get(struct pinctrl_dev *pctldev,
132                                       const struct pin_desc *desc,
133                                       enum pin_config_param param)
134 {
135         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
136         enum uniphier_pin_pull_dir pull_dir =
137                                 uniphier_pin_get_pull_dir(desc->drv_data);
138         unsigned int pupdctrl, reg, shift, val;
139         unsigned int expected = 1;
140         int ret;
141
142         switch (param) {
143         case PIN_CONFIG_BIAS_DISABLE:
144                 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
145                         return 0;
146                 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
147                     pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
148                         return -EINVAL;
149                 expected = 0;
150                 break;
151         case PIN_CONFIG_BIAS_PULL_UP:
152                 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED)
153                         return 0;
154                 if (pull_dir != UNIPHIER_PIN_PULL_UP)
155                         return -EINVAL;
156                 break;
157         case PIN_CONFIG_BIAS_PULL_DOWN:
158                 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
159                         return 0;
160                 if (pull_dir != UNIPHIER_PIN_PULL_DOWN)
161                         return -EINVAL;
162                 break;
163         default:
164                 BUG();
165         }
166
167         pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
168
169         reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
170         shift = pupdctrl % 32;
171
172         ret = regmap_read(priv->regmap, reg, &val);
173         if (ret)
174                 return ret;
175
176         val = (val >> shift) & 1;
177
178         return (val == expected) ? 0 : -EINVAL;
179 }
180
181 static int uniphier_conf_pin_drive_get(struct pinctrl_dev *pctldev,
182                                        const struct pin_desc *desc,
183                                        u16 *strength)
184 {
185         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
186         enum uniphier_pin_drv_type type =
187                                 uniphier_pin_get_drv_type(desc->drv_data);
188         const unsigned int strength_1bit[] = {4, 8};
189         const unsigned int strength_2bit[] = {8, 12, 16, 20};
190         const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16};
191         const unsigned int *supported_strength;
192         unsigned int drvctrl, reg, shift, mask, width, val;
193         int ret;
194
195         switch (type) {
196         case UNIPHIER_PIN_DRV_1BIT:
197                 supported_strength = strength_1bit;
198                 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
199                 width = 1;
200                 break;
201         case UNIPHIER_PIN_DRV_2BIT:
202                 supported_strength = strength_2bit;
203                 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
204                 width = 2;
205                 break;
206         case UNIPHIER_PIN_DRV_3BIT:
207                 supported_strength = strength_3bit;
208                 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
209                 width = 4;
210                 break;
211         case UNIPHIER_PIN_DRV_FIXED4:
212                 *strength = 4;
213                 return 0;
214         case UNIPHIER_PIN_DRV_FIXED5:
215                 *strength = 5;
216                 return 0;
217         case UNIPHIER_PIN_DRV_FIXED8:
218                 *strength = 8;
219                 return 0;
220         default:
221                 /* drive strength control is not supported for this pin */
222                 return -EINVAL;
223         }
224
225         drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
226         drvctrl *= width;
227
228         reg += drvctrl / 32 * 4;
229         shift = drvctrl % 32;
230         mask = (1U << width) - 1;
231
232         ret = regmap_read(priv->regmap, reg, &val);
233         if (ret)
234                 return ret;
235
236         *strength = supported_strength[(val >> shift) & mask];
237
238         return 0;
239 }
240
241 static int uniphier_conf_pin_input_enable_get(struct pinctrl_dev *pctldev,
242                                               const struct pin_desc *desc)
243 {
244         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
245         unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
246         unsigned int val;
247         int ret;
248
249         if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
250                 /* This pin is always input-enabled. */
251                 return 0;
252
253         ret = regmap_read(priv->regmap, UNIPHIER_PINCTRL_IECTRL, &val);
254         if (ret)
255                 return ret;
256
257         return val & BIT(iectrl) ? 0 : -EINVAL;
258 }
259
260 static int uniphier_conf_pin_config_get(struct pinctrl_dev *pctldev,
261                                         unsigned pin,
262                                         unsigned long *configs)
263 {
264         const struct pin_desc *desc = pin_desc_get(pctldev, pin);
265         enum pin_config_param param = pinconf_to_config_param(*configs);
266         bool has_arg = false;
267         u16 arg;
268         int ret;
269
270         switch (param) {
271         case PIN_CONFIG_BIAS_DISABLE:
272         case PIN_CONFIG_BIAS_PULL_UP:
273         case PIN_CONFIG_BIAS_PULL_DOWN:
274                 ret = uniphier_conf_pin_bias_get(pctldev, desc, param);
275                 break;
276         case PIN_CONFIG_DRIVE_STRENGTH:
277                 ret = uniphier_conf_pin_drive_get(pctldev, desc, &arg);
278                 has_arg = true;
279                 break;
280         case PIN_CONFIG_INPUT_ENABLE:
281                 ret = uniphier_conf_pin_input_enable_get(pctldev, desc);
282                 break;
283         default:
284                 /* unsupported parameter */
285                 ret = -EINVAL;
286                 break;
287         }
288
289         if (ret == 0 && has_arg)
290                 *configs = pinconf_to_config_packed(param, arg);
291
292         return ret;
293 }
294
295 static int uniphier_conf_pin_bias_set(struct pinctrl_dev *pctldev,
296                                       const struct pin_desc *desc,
297                                       enum pin_config_param param, u16 arg)
298 {
299         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
300         enum uniphier_pin_pull_dir pull_dir =
301                                 uniphier_pin_get_pull_dir(desc->drv_data);
302         unsigned int pupdctrl, reg, shift;
303         unsigned int val = 1;
304
305         switch (param) {
306         case PIN_CONFIG_BIAS_DISABLE:
307                 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
308                         return 0;
309                 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
310                     pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED) {
311                         dev_err(pctldev->dev,
312                                 "can not disable pull register for pin %s\n",
313                                 desc->name);
314                         return -EINVAL;
315                 }
316                 val = 0;
317                 break;
318         case PIN_CONFIG_BIAS_PULL_UP:
319                 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED && arg != 0)
320                         return 0;
321                 if (pull_dir != UNIPHIER_PIN_PULL_UP) {
322                         dev_err(pctldev->dev,
323                                 "pull-up is unsupported for pin %s\n",
324                                 desc->name);
325                         return -EINVAL;
326                 }
327                 if (arg == 0) {
328                         dev_err(pctldev->dev, "pull-up can not be total\n");
329                         return -EINVAL;
330                 }
331                 break;
332         case PIN_CONFIG_BIAS_PULL_DOWN:
333                 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED && arg != 0)
334                         return 0;
335                 if (pull_dir != UNIPHIER_PIN_PULL_DOWN) {
336                         dev_err(pctldev->dev,
337                                 "pull-down is unsupported for pin %s\n",
338                                 desc->name);
339                         return -EINVAL;
340                 }
341                 if (arg == 0) {
342                         dev_err(pctldev->dev, "pull-down can not be total\n");
343                         return -EINVAL;
344                 }
345                 break;
346         case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
347                 if (pull_dir == UNIPHIER_PIN_PULL_NONE) {
348                         dev_err(pctldev->dev,
349                                 "pull-up/down is unsupported for pin %s\n",
350                                 desc->name);
351                         return -EINVAL;
352                 }
353
354                 if (arg == 0)
355                         return 0; /* configuration ingored */
356                 break;
357         default:
358                 BUG();
359         }
360
361         pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
362
363         reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
364         shift = pupdctrl % 32;
365
366         return regmap_update_bits(priv->regmap, reg, 1 << shift, val << shift);
367 }
368
369 static int uniphier_conf_pin_drive_set(struct pinctrl_dev *pctldev,
370                                        const struct pin_desc *desc,
371                                        u16 strength)
372 {
373         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
374         enum uniphier_pin_drv_type type =
375                                 uniphier_pin_get_drv_type(desc->drv_data);
376         const unsigned int strength_1bit[] = {4, 8, -1};
377         const unsigned int strength_2bit[] = {8, 12, 16, 20, -1};
378         const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16, -1};
379         const unsigned int *supported_strength;
380         unsigned int drvctrl, reg, shift, mask, width, val;
381
382         switch (type) {
383         case UNIPHIER_PIN_DRV_1BIT:
384                 supported_strength = strength_1bit;
385                 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
386                 width = 1;
387                 break;
388         case UNIPHIER_PIN_DRV_2BIT:
389                 supported_strength = strength_2bit;
390                 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
391                 width = 2;
392                 break;
393         case UNIPHIER_PIN_DRV_3BIT:
394                 supported_strength = strength_3bit;
395                 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
396                 width = 4;
397                 break;
398         default:
399                 dev_err(pctldev->dev,
400                         "cannot change drive strength for pin %s\n",
401                         desc->name);
402                 return -EINVAL;
403         }
404
405         for (val = 0; supported_strength[val] > 0; val++) {
406                 if (supported_strength[val] > strength)
407                         break;
408         }
409
410         if (val == 0) {
411                 dev_err(pctldev->dev,
412                         "unsupported drive strength %u mA for pin %s\n",
413                         strength, desc->name);
414                 return -EINVAL;
415         }
416
417         val--;
418
419         drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
420         drvctrl *= width;
421
422         reg += drvctrl / 32 * 4;
423         shift = drvctrl % 32;
424         mask = (1U << width) - 1;
425
426         return regmap_update_bits(priv->regmap, reg,
427                                   mask << shift, val << shift);
428 }
429
430 static int uniphier_conf_pin_input_enable(struct pinctrl_dev *pctldev,
431                                           const struct pin_desc *desc,
432                                           u16 enable)
433 {
434         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
435         unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
436
437         if (enable == 0) {
438                 /*
439                  * Multiple pins share one input enable, so per-pin disabling
440                  * is impossible.
441                  */
442                 dev_err(pctldev->dev, "unable to disable input\n");
443                 return -EINVAL;
444         }
445
446         if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
447                 /* This pin is always input-enabled. nothing to do. */
448                 return 0;
449
450         return regmap_update_bits(priv->regmap, UNIPHIER_PINCTRL_IECTRL,
451                                   BIT(iectrl), BIT(iectrl));
452 }
453
454 static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
455                                         unsigned pin,
456                                         unsigned long *configs,
457                                         unsigned num_configs)
458 {
459         const struct pin_desc *desc = pin_desc_get(pctldev, pin);
460         int i, ret;
461
462         for (i = 0; i < num_configs; i++) {
463                 enum pin_config_param param =
464                                         pinconf_to_config_param(configs[i]);
465                 u16 arg = pinconf_to_config_argument(configs[i]);
466
467                 switch (param) {
468                 case PIN_CONFIG_BIAS_DISABLE:
469                 case PIN_CONFIG_BIAS_PULL_UP:
470                 case PIN_CONFIG_BIAS_PULL_DOWN:
471                 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
472                         ret = uniphier_conf_pin_bias_set(pctldev, desc,
473                                                          param, arg);
474                         break;
475                 case PIN_CONFIG_DRIVE_STRENGTH:
476                         ret = uniphier_conf_pin_drive_set(pctldev, desc, arg);
477                         break;
478                 case PIN_CONFIG_INPUT_ENABLE:
479                         ret = uniphier_conf_pin_input_enable(pctldev, desc,
480                                                              arg);
481                         break;
482                 default:
483                         dev_err(pctldev->dev,
484                                 "unsupported configuration parameter %u\n",
485                                 param);
486                         return -EINVAL;
487                 }
488
489                 if (ret)
490                         return ret;
491         }
492
493         return 0;
494 }
495
496 static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
497                                               unsigned selector,
498                                               unsigned long *configs,
499                                               unsigned num_configs)
500 {
501         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
502         const unsigned *pins = priv->socdata->groups[selector].pins;
503         unsigned num_pins = priv->socdata->groups[selector].num_pins;
504         int i, ret;
505
506         for (i = 0; i < num_pins; i++) {
507                 ret = uniphier_conf_pin_config_set(pctldev, pins[i],
508                                                    configs, num_configs);
509                 if (ret)
510                         return ret;
511         }
512
513         return 0;
514 }
515
516 static const struct pinconf_ops uniphier_confops = {
517         .is_generic = true,
518         .pin_config_get = uniphier_conf_pin_config_get,
519         .pin_config_set = uniphier_conf_pin_config_set,
520         .pin_config_group_set = uniphier_conf_pin_config_group_set,
521 };
522
523 static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
524 {
525         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
526
527         return priv->socdata->functions_count;
528 }
529
530 static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
531                                                   unsigned selector)
532 {
533         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
534
535         return priv->socdata->functions[selector].name;
536 }
537
538 static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
539                                             unsigned selector,
540                                             const char * const **groups,
541                                             unsigned *num_groups)
542 {
543         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
544
545         *groups = priv->socdata->functions[selector].groups;
546         *num_groups = priv->socdata->functions[selector].num_groups;
547
548         return 0;
549 }
550
551 static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
552                                     unsigned muxval)
553 {
554         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
555         unsigned int mux_bits, reg_stride, reg, reg_end, shift, mask;
556         bool load_pinctrl;
557         int ret;
558
559         /* some pins need input-enabling */
560         ret = uniphier_conf_pin_input_enable(pctldev,
561                                              pin_desc_get(pctldev, pin), 1);
562         if (ret)
563                 return ret;
564
565         if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
566                 /*
567                  *  Mode     reg_offset     bit_position
568                  *  Normal    4 * n        shift+3:shift
569                  *  Debug     4 * n        shift+7:shift+4
570                  */
571                 mux_bits = 4;
572                 reg_stride = 8;
573                 load_pinctrl = true;
574         } else {
575                 /*
576                  *  Mode     reg_offset     bit_position
577                  *  Normal    8 * n        shift+3:shift
578                  *  Debug     8 * n + 4    shift+3:shift
579                  */
580                 mux_bits = 8;
581                 reg_stride = 4;
582                 load_pinctrl = false;
583         }
584
585         reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
586         reg_end = reg + reg_stride;
587         shift = pin * mux_bits % 32;
588         mask = (1U << mux_bits) - 1;
589
590         /*
591          * If reg_stride is greater than 4, the MSB of each pinsel shall be
592          * stored in the offset+4.
593          */
594         for (; reg < reg_end; reg += 4) {
595                 ret = regmap_update_bits(priv->regmap, reg,
596                                          mask << shift, muxval << shift);
597                 if (ret)
598                         return ret;
599                 muxval >>= mux_bits;
600         }
601
602         if (load_pinctrl) {
603                 ret = regmap_write(priv->regmap,
604                                    UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
605                 if (ret)
606                         return ret;
607         }
608
609         return 0;
610 }
611
612 static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
613                                 unsigned func_selector,
614                                 unsigned group_selector)
615 {
616         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
617         const struct uniphier_pinctrl_group *grp =
618                                         &priv->socdata->groups[group_selector];
619         int i;
620         int ret;
621
622         for (i = 0; i < grp->num_pins; i++) {
623                 ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
624                                                grp->muxvals[i]);
625                 if (ret)
626                         return ret;
627         }
628
629         return 0;
630 }
631
632 static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
633                                             struct pinctrl_gpio_range *range,
634                                             unsigned offset)
635 {
636         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
637         const struct uniphier_pinctrl_group *groups = priv->socdata->groups;
638         int groups_count = priv->socdata->groups_count;
639         enum uniphier_pinmux_gpio_range_type range_type;
640         int i, j;
641
642         if (strstr(range->name, "irq"))
643                 range_type = UNIPHIER_PINMUX_GPIO_RANGE_IRQ;
644         else
645                 range_type = UNIPHIER_PINMUX_GPIO_RANGE_PORT;
646
647         for (i = 0; i < groups_count; i++) {
648                 if (groups[i].range_type != range_type)
649                         continue;
650
651                 for (j = 0; j < groups[i].num_pins; j++)
652                         if (groups[i].pins[j] == offset)
653                                 goto found;
654         }
655
656         dev_err(pctldev->dev, "pin %u does not support GPIO\n", offset);
657         return -EINVAL;
658
659 found:
660         return uniphier_pmx_set_one_mux(pctldev, offset, groups[i].muxvals[j]);
661 }
662
663 static const struct pinmux_ops uniphier_pmxops = {
664         .get_functions_count = uniphier_pmx_get_functions_count,
665         .get_function_name = uniphier_pmx_get_function_name,
666         .get_function_groups = uniphier_pmx_get_function_groups,
667         .set_mux = uniphier_pmx_set_mux,
668         .gpio_request_enable = uniphier_pmx_gpio_request_enable,
669         .strict = true,
670 };
671
672 int uniphier_pinctrl_probe(struct platform_device *pdev,
673                            struct uniphier_pinctrl_socdata *socdata)
674 {
675         struct device *dev = &pdev->dev;
676         struct uniphier_pinctrl_priv *priv;
677
678         if (!socdata ||
679             !socdata->pins || !socdata->npins ||
680             !socdata->groups || !socdata->groups_count ||
681             !socdata->functions || !socdata->functions_count) {
682                 dev_err(dev, "pinctrl socdata lacks necessary members\n");
683                 return -EINVAL;
684         }
685
686         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
687         if (!priv)
688                 return -ENOMEM;
689
690         priv->regmap = syscon_node_to_regmap(dev->of_node);
691         if (IS_ERR(priv->regmap)) {
692                 dev_err(dev, "failed to get regmap\n");
693                 return PTR_ERR(priv->regmap);
694         }
695
696         priv->socdata = socdata;
697         priv->pctldesc.name = dev->driver->name;
698         priv->pctldesc.pins = socdata->pins;
699         priv->pctldesc.npins = socdata->npins;
700         priv->pctldesc.pctlops = &uniphier_pctlops;
701         priv->pctldesc.pmxops = &uniphier_pmxops;
702         priv->pctldesc.confops = &uniphier_confops;
703         priv->pctldesc.owner = dev->driver->owner;
704
705         priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
706         if (IS_ERR(priv->pctldev)) {
707                 dev_err(dev, "failed to register UniPhier pinctrl driver\n");
708                 return PTR_ERR(priv->pctldev);
709         }
710
711         platform_set_drvdata(pdev, priv);
712
713         return 0;
714 }
715 EXPORT_SYMBOL_GPL(uniphier_pinctrl_probe);