Merge branch 'parisc-4.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[sfrench/cifs-2.6.git] / drivers / pinctrl / sh-pfc / pinctrl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SuperH Pin Function Controller pinmux support.
4  *
5  * Copyright (C) 2012  Paul Mundt
6  */
7
8 #define DRV_NAME "sh-pfc"
9
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/pinctrl/consumer.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 #include <linux/spinlock.h>
23
24 #include "core.h"
25 #include "../core.h"
26 #include "../pinconf.h"
27
28 struct sh_pfc_pin_config {
29         u32 type;
30 };
31
32 struct sh_pfc_pinctrl {
33         struct pinctrl_dev *pctl;
34         struct pinctrl_desc pctl_desc;
35
36         struct sh_pfc *pfc;
37
38         struct pinctrl_pin_desc *pins;
39         struct sh_pfc_pin_config *configs;
40
41         const char *func_prop_name;
42         const char *groups_prop_name;
43         const char *pins_prop_name;
44 };
45
46 static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
47 {
48         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
49
50         return pmx->pfc->info->nr_groups;
51 }
52
53 static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
54                                          unsigned selector)
55 {
56         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
57
58         return pmx->pfc->info->groups[selector].name;
59 }
60
61 static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
62                                  const unsigned **pins, unsigned *num_pins)
63 {
64         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
65
66         *pins = pmx->pfc->info->groups[selector].pins;
67         *num_pins = pmx->pfc->info->groups[selector].nr_pins;
68
69         return 0;
70 }
71
72 static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
73                                 unsigned offset)
74 {
75         seq_puts(s, DRV_NAME);
76 }
77
78 #ifdef CONFIG_OF
79 static int sh_pfc_map_add_config(struct pinctrl_map *map,
80                                  const char *group_or_pin,
81                                  enum pinctrl_map_type type,
82                                  unsigned long *configs,
83                                  unsigned int num_configs)
84 {
85         unsigned long *cfgs;
86
87         cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
88                        GFP_KERNEL);
89         if (cfgs == NULL)
90                 return -ENOMEM;
91
92         map->type = type;
93         map->data.configs.group_or_pin = group_or_pin;
94         map->data.configs.configs = cfgs;
95         map->data.configs.num_configs = num_configs;
96
97         return 0;
98 }
99
100 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
101                                     struct device_node *np,
102                                     struct pinctrl_map **map,
103                                     unsigned int *num_maps, unsigned int *index)
104 {
105         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
106         struct device *dev = pmx->pfc->dev;
107         struct pinctrl_map *maps = *map;
108         unsigned int nmaps = *num_maps;
109         unsigned int idx = *index;
110         unsigned int num_configs;
111         const char *function = NULL;
112         unsigned long *configs;
113         struct property *prop;
114         unsigned int num_groups;
115         unsigned int num_pins;
116         const char *group;
117         const char *pin;
118         int ret;
119
120         /* Support both the old Renesas-specific properties and the new standard
121          * properties. Mixing old and new properties isn't allowed, neither
122          * inside a subnode nor across subnodes.
123          */
124         if (!pmx->func_prop_name) {
125                 if (of_find_property(np, "groups", NULL) ||
126                     of_find_property(np, "pins", NULL)) {
127                         pmx->func_prop_name = "function";
128                         pmx->groups_prop_name = "groups";
129                         pmx->pins_prop_name = "pins";
130                 } else {
131                         pmx->func_prop_name = "renesas,function";
132                         pmx->groups_prop_name = "renesas,groups";
133                         pmx->pins_prop_name = "renesas,pins";
134                 }
135         }
136
137         /* Parse the function and configuration properties. At least a function
138          * or one configuration must be specified.
139          */
140         ret = of_property_read_string(np, pmx->func_prop_name, &function);
141         if (ret < 0 && ret != -EINVAL) {
142                 dev_err(dev, "Invalid function in DT\n");
143                 return ret;
144         }
145
146         ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
147         if (ret < 0)
148                 return ret;
149
150         if (!function && num_configs == 0) {
151                 dev_err(dev,
152                         "DT node must contain at least a function or config\n");
153                 ret = -ENODEV;
154                 goto done;
155         }
156
157         /* Count the number of pins and groups and reallocate mappings. */
158         ret = of_property_count_strings(np, pmx->pins_prop_name);
159         if (ret == -EINVAL) {
160                 num_pins = 0;
161         } else if (ret < 0) {
162                 dev_err(dev, "Invalid pins list in DT\n");
163                 goto done;
164         } else {
165                 num_pins = ret;
166         }
167
168         ret = of_property_count_strings(np, pmx->groups_prop_name);
169         if (ret == -EINVAL) {
170                 num_groups = 0;
171         } else if (ret < 0) {
172                 dev_err(dev, "Invalid pin groups list in DT\n");
173                 goto done;
174         } else {
175                 num_groups = ret;
176         }
177
178         if (!num_pins && !num_groups) {
179                 dev_err(dev, "No pin or group provided in DT node\n");
180                 ret = -ENODEV;
181                 goto done;
182         }
183
184         if (function)
185                 nmaps += num_groups;
186         if (configs)
187                 nmaps += num_pins + num_groups;
188
189         maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
190         if (maps == NULL) {
191                 ret = -ENOMEM;
192                 goto done;
193         }
194
195         *map = maps;
196         *num_maps = nmaps;
197
198         /* Iterate over pins and groups and create the mappings. */
199         of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
200                 if (function) {
201                         maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
202                         maps[idx].data.mux.group = group;
203                         maps[idx].data.mux.function = function;
204                         idx++;
205                 }
206
207                 if (configs) {
208                         ret = sh_pfc_map_add_config(&maps[idx], group,
209                                                     PIN_MAP_TYPE_CONFIGS_GROUP,
210                                                     configs, num_configs);
211                         if (ret < 0)
212                                 goto done;
213
214                         idx++;
215                 }
216         }
217
218         if (!configs) {
219                 ret = 0;
220                 goto done;
221         }
222
223         of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
224                 ret = sh_pfc_map_add_config(&maps[idx], pin,
225                                             PIN_MAP_TYPE_CONFIGS_PIN,
226                                             configs, num_configs);
227                 if (ret < 0)
228                         goto done;
229
230                 idx++;
231         }
232
233 done:
234         *index = idx;
235         kfree(configs);
236         return ret;
237 }
238
239 static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
240                                struct pinctrl_map *map, unsigned num_maps)
241 {
242         unsigned int i;
243
244         if (map == NULL)
245                 return;
246
247         for (i = 0; i < num_maps; ++i) {
248                 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
249                     map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
250                         kfree(map[i].data.configs.configs);
251         }
252
253         kfree(map);
254 }
255
256 static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
257                                  struct device_node *np,
258                                  struct pinctrl_map **map, unsigned *num_maps)
259 {
260         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
261         struct device *dev = pmx->pfc->dev;
262         struct device_node *child;
263         unsigned int index;
264         int ret;
265
266         *map = NULL;
267         *num_maps = 0;
268         index = 0;
269
270         for_each_child_of_node(np, child) {
271                 ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
272                                                &index);
273                 if (ret < 0) {
274                         of_node_put(child);
275                         goto done;
276                 }
277         }
278
279         /* If no mapping has been found in child nodes try the config node. */
280         if (*num_maps == 0) {
281                 ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
282                                                &index);
283                 if (ret < 0)
284                         goto done;
285         }
286
287         if (*num_maps)
288                 return 0;
289
290         dev_err(dev, "no mapping found in node %pOF\n", np);
291         ret = -EINVAL;
292
293 done:
294         if (ret < 0)
295                 sh_pfc_dt_free_map(pctldev, *map, *num_maps);
296
297         return ret;
298 }
299 #endif /* CONFIG_OF */
300
301 static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
302         .get_groups_count       = sh_pfc_get_groups_count,
303         .get_group_name         = sh_pfc_get_group_name,
304         .get_group_pins         = sh_pfc_get_group_pins,
305         .pin_dbg_show           = sh_pfc_pin_dbg_show,
306 #ifdef CONFIG_OF
307         .dt_node_to_map         = sh_pfc_dt_node_to_map,
308         .dt_free_map            = sh_pfc_dt_free_map,
309 #endif
310 };
311
312 static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
313 {
314         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
315
316         return pmx->pfc->info->nr_functions;
317 }
318
319 static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
320                                             unsigned selector)
321 {
322         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
323
324         return pmx->pfc->info->functions[selector].name;
325 }
326
327 static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
328                                       unsigned selector,
329                                       const char * const **groups,
330                                       unsigned * const num_groups)
331 {
332         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
333
334         *groups = pmx->pfc->info->functions[selector].groups;
335         *num_groups = pmx->pfc->info->functions[selector].nr_groups;
336
337         return 0;
338 }
339
340 static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
341                                unsigned group)
342 {
343         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
344         struct sh_pfc *pfc = pmx->pfc;
345         const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
346         unsigned long flags;
347         unsigned int i;
348         int ret = 0;
349
350         spin_lock_irqsave(&pfc->lock, flags);
351
352         for (i = 0; i < grp->nr_pins; ++i) {
353                 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
354                 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
355
356                 if (cfg->type != PINMUX_TYPE_NONE) {
357                         ret = -EBUSY;
358                         goto done;
359                 }
360         }
361
362         for (i = 0; i < grp->nr_pins; ++i) {
363                 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
364                 if (ret < 0)
365                         break;
366         }
367
368 done:
369         spin_unlock_irqrestore(&pfc->lock, flags);
370         return ret;
371 }
372
373 static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
374                                       struct pinctrl_gpio_range *range,
375                                       unsigned offset)
376 {
377         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
378         struct sh_pfc *pfc = pmx->pfc;
379         int idx = sh_pfc_get_pin_index(pfc, offset);
380         struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
381         unsigned long flags;
382         int ret;
383
384         spin_lock_irqsave(&pfc->lock, flags);
385
386         if (cfg->type != PINMUX_TYPE_NONE) {
387                 dev_err(pfc->dev,
388                         "Pin %u is busy, can't configure it as GPIO.\n",
389                         offset);
390                 ret = -EBUSY;
391                 goto done;
392         }
393
394         if (!pfc->gpio) {
395                 /* If GPIOs are handled externally the pin mux type need to be
396                  * set to GPIO here.
397                  */
398                 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
399
400                 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
401                 if (ret < 0)
402                         goto done;
403         }
404
405         cfg->type = PINMUX_TYPE_GPIO;
406
407         ret = 0;
408
409 done:
410         spin_unlock_irqrestore(&pfc->lock, flags);
411
412         return ret;
413 }
414
415 static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
416                                      struct pinctrl_gpio_range *range,
417                                      unsigned offset)
418 {
419         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
420         struct sh_pfc *pfc = pmx->pfc;
421         int idx = sh_pfc_get_pin_index(pfc, offset);
422         struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
423         unsigned long flags;
424
425         spin_lock_irqsave(&pfc->lock, flags);
426         cfg->type = PINMUX_TYPE_NONE;
427         spin_unlock_irqrestore(&pfc->lock, flags);
428 }
429
430 static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
431                                      struct pinctrl_gpio_range *range,
432                                      unsigned offset, bool input)
433 {
434         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
435         struct sh_pfc *pfc = pmx->pfc;
436         int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
437         int idx = sh_pfc_get_pin_index(pfc, offset);
438         const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
439         struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
440         unsigned long flags;
441         unsigned int dir;
442         int ret;
443
444         /* Check if the requested direction is supported by the pin. Not all SoC
445          * provide pin config data, so perform the check conditionally.
446          */
447         if (pin->configs) {
448                 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
449                 if (!(pin->configs & dir))
450                         return -EINVAL;
451         }
452
453         spin_lock_irqsave(&pfc->lock, flags);
454
455         ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
456         if (ret < 0)
457                 goto done;
458
459         cfg->type = new_type;
460
461 done:
462         spin_unlock_irqrestore(&pfc->lock, flags);
463         return ret;
464 }
465
466 static const struct pinmux_ops sh_pfc_pinmux_ops = {
467         .get_functions_count    = sh_pfc_get_functions_count,
468         .get_function_name      = sh_pfc_get_function_name,
469         .get_function_groups    = sh_pfc_get_function_groups,
470         .set_mux                = sh_pfc_func_set_mux,
471         .gpio_request_enable    = sh_pfc_gpio_request_enable,
472         .gpio_disable_free      = sh_pfc_gpio_disable_free,
473         .gpio_set_direction     = sh_pfc_gpio_set_direction,
474 };
475
476 static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
477                 unsigned int pin, unsigned int *offset, unsigned int *size)
478 {
479         const struct pinmux_drive_reg_field *field;
480         const struct pinmux_drive_reg *reg;
481         unsigned int i;
482
483         for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
484                 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
485                         field = &reg->fields[i];
486
487                         if (field->size && field->pin == pin) {
488                                 *offset = field->offset;
489                                 *size = field->size;
490
491                                 return reg->reg;
492                         }
493                 }
494         }
495
496         return 0;
497 }
498
499 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
500                                              unsigned int pin)
501 {
502         unsigned long flags;
503         unsigned int offset;
504         unsigned int size;
505         u32 reg;
506         u32 val;
507
508         reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
509         if (!reg)
510                 return -EINVAL;
511
512         spin_lock_irqsave(&pfc->lock, flags);
513         val = sh_pfc_read(pfc, reg);
514         spin_unlock_irqrestore(&pfc->lock, flags);
515
516         val = (val >> offset) & GENMASK(size - 1, 0);
517
518         /* Convert the value to mA based on a full drive strength value of 24mA.
519          * We can make the full value configurable later if needed.
520          */
521         return (val + 1) * (size == 2 ? 6 : 3);
522 }
523
524 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
525                                              unsigned int pin, u16 strength)
526 {
527         unsigned long flags;
528         unsigned int offset;
529         unsigned int size;
530         unsigned int step;
531         u32 reg;
532         u32 val;
533
534         reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
535         if (!reg)
536                 return -EINVAL;
537
538         step = size == 2 ? 6 : 3;
539
540         if (strength < step || strength > 24)
541                 return -EINVAL;
542
543         /* Convert the value from mA based on a full drive strength value of
544          * 24mA. We can make the full value configurable later if needed.
545          */
546         strength = strength / step - 1;
547
548         spin_lock_irqsave(&pfc->lock, flags);
549
550         val = sh_pfc_read(pfc, reg);
551         val &= ~GENMASK(offset + size - 1, offset);
552         val |= strength << offset;
553
554         sh_pfc_write(pfc, reg, val);
555
556         spin_unlock_irqrestore(&pfc->lock, flags);
557
558         return 0;
559 }
560
561 /* Check whether the requested parameter is supported for a pin. */
562 static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
563                                     enum pin_config_param param)
564 {
565         int idx = sh_pfc_get_pin_index(pfc, _pin);
566         const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
567
568         switch (param) {
569         case PIN_CONFIG_BIAS_DISABLE:
570                 return pin->configs &
571                         (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
572
573         case PIN_CONFIG_BIAS_PULL_UP:
574                 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
575
576         case PIN_CONFIG_BIAS_PULL_DOWN:
577                 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
578
579         case PIN_CONFIG_DRIVE_STRENGTH:
580                 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
581
582         case PIN_CONFIG_POWER_SOURCE:
583                 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
584
585         default:
586                 return false;
587         }
588 }
589
590 static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
591                               unsigned long *config)
592 {
593         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
594         struct sh_pfc *pfc = pmx->pfc;
595         enum pin_config_param param = pinconf_to_config_param(*config);
596         unsigned long flags;
597         unsigned int arg;
598
599         if (!sh_pfc_pinconf_validate(pfc, _pin, param))
600                 return -ENOTSUPP;
601
602         switch (param) {
603         case PIN_CONFIG_BIAS_DISABLE:
604         case PIN_CONFIG_BIAS_PULL_UP:
605         case PIN_CONFIG_BIAS_PULL_DOWN: {
606                 unsigned int bias;
607
608                 if (!pfc->info->ops || !pfc->info->ops->get_bias)
609                         return -ENOTSUPP;
610
611                 spin_lock_irqsave(&pfc->lock, flags);
612                 bias = pfc->info->ops->get_bias(pfc, _pin);
613                 spin_unlock_irqrestore(&pfc->lock, flags);
614
615                 if (bias != param)
616                         return -EINVAL;
617
618                 arg = 0;
619                 break;
620         }
621
622         case PIN_CONFIG_DRIVE_STRENGTH: {
623                 int ret;
624
625                 ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
626                 if (ret < 0)
627                         return ret;
628
629                 arg = ret;
630                 break;
631         }
632
633         case PIN_CONFIG_POWER_SOURCE: {
634                 u32 pocctrl, val;
635                 int bit;
636
637                 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
638                         return -ENOTSUPP;
639
640                 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
641                 if (WARN(bit < 0, "invalid pin %#x", _pin))
642                         return bit;
643
644                 spin_lock_irqsave(&pfc->lock, flags);
645                 val = sh_pfc_read(pfc, pocctrl);
646                 spin_unlock_irqrestore(&pfc->lock, flags);
647
648                 arg = (val & BIT(bit)) ? 3300 : 1800;
649                 break;
650         }
651
652         default:
653                 return -ENOTSUPP;
654         }
655
656         *config = pinconf_to_config_packed(param, arg);
657         return 0;
658 }
659
660 static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
661                               unsigned long *configs, unsigned num_configs)
662 {
663         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
664         struct sh_pfc *pfc = pmx->pfc;
665         enum pin_config_param param;
666         unsigned long flags;
667         unsigned int i;
668
669         for (i = 0; i < num_configs; i++) {
670                 param = pinconf_to_config_param(configs[i]);
671
672                 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
673                         return -ENOTSUPP;
674
675                 switch (param) {
676                 case PIN_CONFIG_BIAS_PULL_UP:
677                 case PIN_CONFIG_BIAS_PULL_DOWN:
678                 case PIN_CONFIG_BIAS_DISABLE:
679                         if (!pfc->info->ops || !pfc->info->ops->set_bias)
680                                 return -ENOTSUPP;
681
682                         spin_lock_irqsave(&pfc->lock, flags);
683                         pfc->info->ops->set_bias(pfc, _pin, param);
684                         spin_unlock_irqrestore(&pfc->lock, flags);
685
686                         break;
687
688                 case PIN_CONFIG_DRIVE_STRENGTH: {
689                         unsigned int arg =
690                                 pinconf_to_config_argument(configs[i]);
691                         int ret;
692
693                         ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
694                         if (ret < 0)
695                                 return ret;
696
697                         break;
698                 }
699
700                 case PIN_CONFIG_POWER_SOURCE: {
701                         unsigned int mV = pinconf_to_config_argument(configs[i]);
702                         u32 pocctrl, val;
703                         int bit;
704
705                         if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
706                                 return -ENOTSUPP;
707
708                         bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
709                         if (WARN(bit < 0, "invalid pin %#x", _pin))
710                                 return bit;
711
712                         if (mV != 1800 && mV != 3300)
713                                 return -EINVAL;
714
715                         spin_lock_irqsave(&pfc->lock, flags);
716                         val = sh_pfc_read(pfc, pocctrl);
717                         if (mV == 3300)
718                                 val |= BIT(bit);
719                         else
720                                 val &= ~BIT(bit);
721                         sh_pfc_write(pfc, pocctrl, val);
722                         spin_unlock_irqrestore(&pfc->lock, flags);
723
724                         break;
725                 }
726
727                 default:
728                         return -ENOTSUPP;
729                 }
730         } /* for each config */
731
732         return 0;
733 }
734
735 static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
736                                     unsigned long *configs,
737                                     unsigned num_configs)
738 {
739         struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
740         const unsigned int *pins;
741         unsigned int num_pins;
742         unsigned int i, ret;
743
744         pins = pmx->pfc->info->groups[group].pins;
745         num_pins = pmx->pfc->info->groups[group].nr_pins;
746
747         for (i = 0; i < num_pins; ++i) {
748                 ret = sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
749                 if (ret)
750                         return ret;
751         }
752
753         return 0;
754 }
755
756 static const struct pinconf_ops sh_pfc_pinconf_ops = {
757         .is_generic                     = true,
758         .pin_config_get                 = sh_pfc_pinconf_get,
759         .pin_config_set                 = sh_pfc_pinconf_set,
760         .pin_config_group_set           = sh_pfc_pinconf_group_set,
761         .pin_config_config_dbg_show     = pinconf_generic_dump_config,
762 };
763
764 /* PFC ranges -> pinctrl pin descs */
765 static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
766 {
767         unsigned int i;
768
769         /* Allocate and initialize the pins and configs arrays. */
770         pmx->pins = devm_kcalloc(pfc->dev,
771                                  pfc->info->nr_pins, sizeof(*pmx->pins),
772                                  GFP_KERNEL);
773         if (unlikely(!pmx->pins))
774                 return -ENOMEM;
775
776         pmx->configs = devm_kcalloc(pfc->dev,
777                                     pfc->info->nr_pins, sizeof(*pmx->configs),
778                                     GFP_KERNEL);
779         if (unlikely(!pmx->configs))
780                 return -ENOMEM;
781
782         for (i = 0; i < pfc->info->nr_pins; ++i) {
783                 const struct sh_pfc_pin *info = &pfc->info->pins[i];
784                 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
785                 struct pinctrl_pin_desc *pin = &pmx->pins[i];
786
787                 /* If the pin number is equal to -1 all pins are considered */
788                 pin->number = info->pin != (u16)-1 ? info->pin : i;
789                 pin->name = info->name;
790                 cfg->type = PINMUX_TYPE_NONE;
791         }
792
793         return 0;
794 }
795
796 int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
797 {
798         struct sh_pfc_pinctrl *pmx;
799         int ret;
800
801         pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
802         if (unlikely(!pmx))
803                 return -ENOMEM;
804
805         pmx->pfc = pfc;
806
807         ret = sh_pfc_map_pins(pfc, pmx);
808         if (ret < 0)
809                 return ret;
810
811         pmx->pctl_desc.name = DRV_NAME;
812         pmx->pctl_desc.owner = THIS_MODULE;
813         pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
814         pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
815         pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
816         pmx->pctl_desc.pins = pmx->pins;
817         pmx->pctl_desc.npins = pfc->info->nr_pins;
818
819         ret = devm_pinctrl_register_and_init(pfc->dev, &pmx->pctl_desc, pmx,
820                                              &pmx->pctl);
821         if (ret) {
822                 dev_err(pfc->dev, "could not register: %i\n", ret);
823
824                 return ret;
825         }
826
827         return pinctrl_enable(pmx->pctl);
828 }