pinctrl: uniphier: support pin configuration for dedicated pins
[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         unsigned int reg, mask;
437
438         /*
439          * Multiple pins share one input enable, per-pin disabling is
440          * impossible.
441          */
442         if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL) &&
443             !enable)
444                 return -EINVAL;
445
446         /* UNIPHIER_PIN_IECTRL_NONE means the pin is always input-enabled */
447         if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
448                 return enable ? 0 : -EINVAL;
449
450         reg = UNIPHIER_PINCTRL_IECTRL + iectrl / 32 * 4;
451         mask = BIT(iectrl % 32);
452
453         return regmap_update_bits(priv->regmap, reg, mask, enable ? mask : 0);
454 }
455
456 static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
457                                         unsigned pin,
458                                         unsigned long *configs,
459                                         unsigned num_configs)
460 {
461         const struct pin_desc *desc = pin_desc_get(pctldev, pin);
462         int i, ret;
463
464         for (i = 0; i < num_configs; i++) {
465                 enum pin_config_param param =
466                                         pinconf_to_config_param(configs[i]);
467                 u16 arg = pinconf_to_config_argument(configs[i]);
468
469                 switch (param) {
470                 case PIN_CONFIG_BIAS_DISABLE:
471                 case PIN_CONFIG_BIAS_PULL_UP:
472                 case PIN_CONFIG_BIAS_PULL_DOWN:
473                 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
474                         ret = uniphier_conf_pin_bias_set(pctldev, desc,
475                                                          param, arg);
476                         break;
477                 case PIN_CONFIG_DRIVE_STRENGTH:
478                         ret = uniphier_conf_pin_drive_set(pctldev, desc, arg);
479                         break;
480                 case PIN_CONFIG_INPUT_ENABLE:
481                         ret = uniphier_conf_pin_input_enable(pctldev, desc,
482                                                              arg);
483                         break;
484                 default:
485                         dev_err(pctldev->dev,
486                                 "unsupported configuration parameter %u\n",
487                                 param);
488                         return -EINVAL;
489                 }
490
491                 if (ret)
492                         return ret;
493         }
494
495         return 0;
496 }
497
498 static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
499                                               unsigned selector,
500                                               unsigned long *configs,
501                                               unsigned num_configs)
502 {
503         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
504         const unsigned *pins = priv->socdata->groups[selector].pins;
505         unsigned num_pins = priv->socdata->groups[selector].num_pins;
506         int i, ret;
507
508         for (i = 0; i < num_pins; i++) {
509                 ret = uniphier_conf_pin_config_set(pctldev, pins[i],
510                                                    configs, num_configs);
511                 if (ret)
512                         return ret;
513         }
514
515         return 0;
516 }
517
518 static const struct pinconf_ops uniphier_confops = {
519         .is_generic = true,
520         .pin_config_get = uniphier_conf_pin_config_get,
521         .pin_config_set = uniphier_conf_pin_config_set,
522         .pin_config_group_set = uniphier_conf_pin_config_group_set,
523 };
524
525 static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
526 {
527         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
528
529         return priv->socdata->functions_count;
530 }
531
532 static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
533                                                   unsigned selector)
534 {
535         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
536
537         return priv->socdata->functions[selector].name;
538 }
539
540 static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
541                                             unsigned selector,
542                                             const char * const **groups,
543                                             unsigned *num_groups)
544 {
545         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
546
547         *groups = priv->socdata->functions[selector].groups;
548         *num_groups = priv->socdata->functions[selector].num_groups;
549
550         return 0;
551 }
552
553 static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
554                                     int muxval)
555 {
556         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
557         unsigned int mux_bits, reg_stride, reg, reg_end, shift, mask;
558         bool load_pinctrl;
559         int ret;
560
561         /* some pins need input-enabling */
562         ret = uniphier_conf_pin_input_enable(pctldev,
563                                              pin_desc_get(pctldev, pin), 1);
564         if (ret)
565                 return ret;
566
567         if (muxval < 0)
568                 return 0;       /* dedicated pin; nothing to do for pin-mux */
569
570         if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
571                 /*
572                  *  Mode     reg_offset     bit_position
573                  *  Normal    4 * n        shift+3:shift
574                  *  Debug     4 * n        shift+7:shift+4
575                  */
576                 mux_bits = 4;
577                 reg_stride = 8;
578                 load_pinctrl = true;
579         } else {
580                 /*
581                  *  Mode     reg_offset     bit_position
582                  *  Normal    8 * n        shift+3:shift
583                  *  Debug     8 * n + 4    shift+3:shift
584                  */
585                 mux_bits = 8;
586                 reg_stride = 4;
587                 load_pinctrl = false;
588         }
589
590         reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
591         reg_end = reg + reg_stride;
592         shift = pin * mux_bits % 32;
593         mask = (1U << mux_bits) - 1;
594
595         /*
596          * If reg_stride is greater than 4, the MSB of each pinsel shall be
597          * stored in the offset+4.
598          */
599         for (; reg < reg_end; reg += 4) {
600                 ret = regmap_update_bits(priv->regmap, reg,
601                                          mask << shift, muxval << shift);
602                 if (ret)
603                         return ret;
604                 muxval >>= mux_bits;
605         }
606
607         if (load_pinctrl) {
608                 ret = regmap_write(priv->regmap,
609                                    UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
610                 if (ret)
611                         return ret;
612         }
613
614         return 0;
615 }
616
617 static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
618                                 unsigned func_selector,
619                                 unsigned group_selector)
620 {
621         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
622         const struct uniphier_pinctrl_group *grp =
623                                         &priv->socdata->groups[group_selector];
624         int i;
625         int ret;
626
627         for (i = 0; i < grp->num_pins; i++) {
628                 ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
629                                                grp->muxvals[i]);
630                 if (ret)
631                         return ret;
632         }
633
634         return 0;
635 }
636
637 static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
638                                             struct pinctrl_gpio_range *range,
639                                             unsigned offset)
640 {
641         struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
642         const struct uniphier_pinctrl_group *groups = priv->socdata->groups;
643         int groups_count = priv->socdata->groups_count;
644         enum uniphier_pinmux_gpio_range_type range_type;
645         int i, j;
646
647         if (strstr(range->name, "irq"))
648                 range_type = UNIPHIER_PINMUX_GPIO_RANGE_IRQ;
649         else
650                 range_type = UNIPHIER_PINMUX_GPIO_RANGE_PORT;
651
652         for (i = 0; i < groups_count; i++) {
653                 if (groups[i].range_type != range_type)
654                         continue;
655
656                 for (j = 0; j < groups[i].num_pins; j++)
657                         if (groups[i].pins[j] == offset)
658                                 goto found;
659         }
660
661         dev_err(pctldev->dev, "pin %u does not support GPIO\n", offset);
662         return -EINVAL;
663
664 found:
665         return uniphier_pmx_set_one_mux(pctldev, offset, groups[i].muxvals[j]);
666 }
667
668 static const struct pinmux_ops uniphier_pmxops = {
669         .get_functions_count = uniphier_pmx_get_functions_count,
670         .get_function_name = uniphier_pmx_get_function_name,
671         .get_function_groups = uniphier_pmx_get_function_groups,
672         .set_mux = uniphier_pmx_set_mux,
673         .gpio_request_enable = uniphier_pmx_gpio_request_enable,
674         .strict = true,
675 };
676
677 int uniphier_pinctrl_probe(struct platform_device *pdev,
678                            struct uniphier_pinctrl_socdata *socdata)
679 {
680         struct device *dev = &pdev->dev;
681         struct uniphier_pinctrl_priv *priv;
682
683         if (!socdata ||
684             !socdata->pins || !socdata->npins ||
685             !socdata->groups || !socdata->groups_count ||
686             !socdata->functions || !socdata->functions_count) {
687                 dev_err(dev, "pinctrl socdata lacks necessary members\n");
688                 return -EINVAL;
689         }
690
691         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
692         if (!priv)
693                 return -ENOMEM;
694
695         priv->regmap = syscon_node_to_regmap(dev->of_node);
696         if (IS_ERR(priv->regmap)) {
697                 dev_err(dev, "failed to get regmap\n");
698                 return PTR_ERR(priv->regmap);
699         }
700
701         priv->socdata = socdata;
702         priv->pctldesc.name = dev->driver->name;
703         priv->pctldesc.pins = socdata->pins;
704         priv->pctldesc.npins = socdata->npins;
705         priv->pctldesc.pctlops = &uniphier_pctlops;
706         priv->pctldesc.pmxops = &uniphier_pmxops;
707         priv->pctldesc.confops = &uniphier_confops;
708         priv->pctldesc.owner = dev->driver->owner;
709
710         priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
711         if (IS_ERR(priv->pctldev)) {
712                 dev_err(dev, "failed to register UniPhier pinctrl driver\n");
713                 return PTR_ERR(priv->pctldev);
714         }
715
716         platform_set_drvdata(pdev, priv);
717
718         return 0;
719 }
720 EXPORT_SYMBOL_GPL(uniphier_pinctrl_probe);