GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / pinctrl / samsung / pinctrl-samsung.c
1 /*
2  * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *              http://www.samsung.com
6  * Copyright (c) 2012 Linaro Ltd
7  *              http://www.linaro.org
8  *
9  * Author: Thomas Abraham <thomas.ab@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This driver implements the Samsung pinctrl driver. It supports setting up of
17  * pinmux and pinconf configurations. The gpiolib interface is also included.
18  * External interrupt (gpio and wakeup) support are not included in this driver
19  * but provides extensions to which platform specific implementation of the gpio
20  * and wakeup interrupts can be hooked to.
21  */
22
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/io.h>
26 #include <linux/slab.h>
27 #include <linux/err.h>
28 #include <linux/gpio.h>
29 #include <linux/irqdomain.h>
30 #include <linux/of_device.h>
31 #include <linux/spinlock.h>
32
33 #include <dt-bindings/pinctrl/samsung.h>
34
35 #include "../core.h"
36 #include "pinctrl-samsung.h"
37
38 /* maximum number of the memory resources */
39 #define SAMSUNG_PINCTRL_NUM_RESOURCES   2
40
41 /* list of all possible config options supported */
42 static struct pin_config {
43         const char *property;
44         enum pincfg_type param;
45 } cfg_params[] = {
46         { "samsung,pin-pud", PINCFG_TYPE_PUD },
47         { "samsung,pin-drv", PINCFG_TYPE_DRV },
48         { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
49         { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
50         { "samsung,pin-val", PINCFG_TYPE_DAT },
51 };
52
53 static unsigned int pin_base;
54
55 static int samsung_get_group_count(struct pinctrl_dev *pctldev)
56 {
57         struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
58
59         return pmx->nr_groups;
60 }
61
62 static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
63                                                 unsigned group)
64 {
65         struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
66
67         return pmx->pin_groups[group].name;
68 }
69
70 static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
71                                         unsigned group,
72                                         const unsigned **pins,
73                                         unsigned *num_pins)
74 {
75         struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
76
77         *pins = pmx->pin_groups[group].pins;
78         *num_pins = pmx->pin_groups[group].num_pins;
79
80         return 0;
81 }
82
83 static int reserve_map(struct device *dev, struct pinctrl_map **map,
84                        unsigned *reserved_maps, unsigned *num_maps,
85                        unsigned reserve)
86 {
87         unsigned old_num = *reserved_maps;
88         unsigned new_num = *num_maps + reserve;
89         struct pinctrl_map *new_map;
90
91         if (old_num >= new_num)
92                 return 0;
93
94         new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
95         if (!new_map)
96                 return -ENOMEM;
97
98         memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
99
100         *map = new_map;
101         *reserved_maps = new_num;
102
103         return 0;
104 }
105
106 static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
107                        unsigned *num_maps, const char *group,
108                        const char *function)
109 {
110         if (WARN_ON(*num_maps == *reserved_maps))
111                 return -ENOSPC;
112
113         (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
114         (*map)[*num_maps].data.mux.group = group;
115         (*map)[*num_maps].data.mux.function = function;
116         (*num_maps)++;
117
118         return 0;
119 }
120
121 static int add_map_configs(struct device *dev, struct pinctrl_map **map,
122                            unsigned *reserved_maps, unsigned *num_maps,
123                            const char *group, unsigned long *configs,
124                            unsigned num_configs)
125 {
126         unsigned long *dup_configs;
127
128         if (WARN_ON(*num_maps == *reserved_maps))
129                 return -ENOSPC;
130
131         dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
132                               GFP_KERNEL);
133         if (!dup_configs)
134                 return -ENOMEM;
135
136         (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
137         (*map)[*num_maps].data.configs.group_or_pin = group;
138         (*map)[*num_maps].data.configs.configs = dup_configs;
139         (*map)[*num_maps].data.configs.num_configs = num_configs;
140         (*num_maps)++;
141
142         return 0;
143 }
144
145 static int add_config(struct device *dev, unsigned long **configs,
146                       unsigned *num_configs, unsigned long config)
147 {
148         unsigned old_num = *num_configs;
149         unsigned new_num = old_num + 1;
150         unsigned long *new_configs;
151
152         new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
153                                GFP_KERNEL);
154         if (!new_configs)
155                 return -ENOMEM;
156
157         new_configs[old_num] = config;
158
159         *configs = new_configs;
160         *num_configs = new_num;
161
162         return 0;
163 }
164
165 static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
166                                       struct pinctrl_map *map,
167                                       unsigned num_maps)
168 {
169         int i;
170
171         for (i = 0; i < num_maps; i++)
172                 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
173                         kfree(map[i].data.configs.configs);
174
175         kfree(map);
176 }
177
178 static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data *drvdata,
179                                      struct device *dev,
180                                      struct device_node *np,
181                                      struct pinctrl_map **map,
182                                      unsigned *reserved_maps,
183                                      unsigned *num_maps)
184 {
185         int ret, i;
186         u32 val;
187         unsigned long config;
188         unsigned long *configs = NULL;
189         unsigned num_configs = 0;
190         unsigned reserve;
191         struct property *prop;
192         const char *group;
193         bool has_func = false;
194
195         ret = of_property_read_u32(np, "samsung,pin-function", &val);
196         if (!ret)
197                 has_func = true;
198
199         for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
200                 ret = of_property_read_u32(np, cfg_params[i].property, &val);
201                 if (!ret) {
202                         config = PINCFG_PACK(cfg_params[i].param, val);
203                         ret = add_config(dev, &configs, &num_configs, config);
204                         if (ret < 0)
205                                 goto exit;
206                 /* EINVAL=missing, which is fine since it's optional */
207                 } else if (ret != -EINVAL) {
208                         dev_err(dev, "could not parse property %s\n",
209                                 cfg_params[i].property);
210                 }
211         }
212
213         reserve = 0;
214         if (has_func)
215                 reserve++;
216         if (num_configs)
217                 reserve++;
218         ret = of_property_count_strings(np, "samsung,pins");
219         if (ret < 0) {
220                 dev_err(dev, "could not parse property samsung,pins\n");
221                 goto exit;
222         }
223         reserve *= ret;
224
225         ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
226         if (ret < 0)
227                 goto exit;
228
229         of_property_for_each_string(np, "samsung,pins", prop, group) {
230                 if (has_func) {
231                         ret = add_map_mux(map, reserved_maps,
232                                                 num_maps, group, np->full_name);
233                         if (ret < 0)
234                                 goto exit;
235                 }
236
237                 if (num_configs) {
238                         ret = add_map_configs(dev, map, reserved_maps,
239                                               num_maps, group, configs,
240                                               num_configs);
241                         if (ret < 0)
242                                 goto exit;
243                 }
244         }
245
246         ret = 0;
247
248 exit:
249         kfree(configs);
250         return ret;
251 }
252
253 static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
254                                         struct device_node *np_config,
255                                         struct pinctrl_map **map,
256                                         unsigned *num_maps)
257 {
258         struct samsung_pinctrl_drv_data *drvdata;
259         unsigned reserved_maps;
260         struct device_node *np;
261         int ret;
262
263         drvdata = pinctrl_dev_get_drvdata(pctldev);
264
265         reserved_maps = 0;
266         *map = NULL;
267         *num_maps = 0;
268
269         if (!of_get_child_count(np_config))
270                 return samsung_dt_subnode_to_map(drvdata, pctldev->dev,
271                                                         np_config, map,
272                                                         &reserved_maps,
273                                                         num_maps);
274
275         for_each_child_of_node(np_config, np) {
276                 ret = samsung_dt_subnode_to_map(drvdata, pctldev->dev, np, map,
277                                                 &reserved_maps, num_maps);
278                 if (ret < 0) {
279                         samsung_dt_free_map(pctldev, *map, *num_maps);
280                         of_node_put(np);
281                         return ret;
282                 }
283         }
284
285         return 0;
286 }
287
288 /* list of pinctrl callbacks for the pinctrl core */
289 static const struct pinctrl_ops samsung_pctrl_ops = {
290         .get_groups_count       = samsung_get_group_count,
291         .get_group_name         = samsung_get_group_name,
292         .get_group_pins         = samsung_get_group_pins,
293         .dt_node_to_map         = samsung_dt_node_to_map,
294         .dt_free_map            = samsung_dt_free_map,
295 };
296
297 /* check if the selector is a valid pin function selector */
298 static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
299 {
300         struct samsung_pinctrl_drv_data *drvdata;
301
302         drvdata = pinctrl_dev_get_drvdata(pctldev);
303         return drvdata->nr_functions;
304 }
305
306 /* return the name of the pin function specified */
307 static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
308                                                 unsigned selector)
309 {
310         struct samsung_pinctrl_drv_data *drvdata;
311
312         drvdata = pinctrl_dev_get_drvdata(pctldev);
313         return drvdata->pmx_functions[selector].name;
314 }
315
316 /* return the groups associated for the specified function selector */
317 static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
318                 unsigned selector, const char * const **groups,
319                 unsigned * const num_groups)
320 {
321         struct samsung_pinctrl_drv_data *drvdata;
322
323         drvdata = pinctrl_dev_get_drvdata(pctldev);
324         *groups = drvdata->pmx_functions[selector].groups;
325         *num_groups = drvdata->pmx_functions[selector].num_groups;
326         return 0;
327 }
328
329 /*
330  * given a pin number that is local to a pin controller, find out the pin bank
331  * and the register base of the pin bank.
332  */
333 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
334                         unsigned pin, void __iomem **reg, u32 *offset,
335                         struct samsung_pin_bank **bank)
336 {
337         struct samsung_pin_bank *b;
338
339         b = drvdata->pin_banks;
340
341         while ((pin >= b->pin_base) &&
342                         ((b->pin_base + b->nr_pins - 1) < pin))
343                 b++;
344
345         *reg = b->pctl_base + b->pctl_offset;
346         *offset = pin - b->pin_base;
347         if (bank)
348                 *bank = b;
349 }
350
351 /* enable or disable a pinmux function */
352 static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
353                                         unsigned group)
354 {
355         struct samsung_pinctrl_drv_data *drvdata;
356         const struct samsung_pin_bank_type *type;
357         struct samsung_pin_bank *bank;
358         void __iomem *reg;
359         u32 mask, shift, data, pin_offset;
360         unsigned long flags;
361         const struct samsung_pmx_func *func;
362         const struct samsung_pin_group *grp;
363
364         drvdata = pinctrl_dev_get_drvdata(pctldev);
365         func = &drvdata->pmx_functions[selector];
366         grp = &drvdata->pin_groups[group];
367
368         pin_to_reg_bank(drvdata, grp->pins[0] - drvdata->pin_base,
369                         &reg, &pin_offset, &bank);
370         type = bank->type;
371         mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
372         shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC];
373         if (shift >= 32) {
374                 /* Some banks have two config registers */
375                 shift -= 32;
376                 reg += 4;
377         }
378
379         spin_lock_irqsave(&bank->slock, flags);
380
381         data = readl(reg + type->reg_offset[PINCFG_TYPE_FUNC]);
382         data &= ~(mask << shift);
383         data |= func->val << shift;
384         writel(data, reg + type->reg_offset[PINCFG_TYPE_FUNC]);
385
386         spin_unlock_irqrestore(&bank->slock, flags);
387 }
388
389 /* enable a specified pinmux by writing to registers */
390 static int samsung_pinmux_set_mux(struct pinctrl_dev *pctldev,
391                                   unsigned selector,
392                                   unsigned group)
393 {
394         samsung_pinmux_setup(pctldev, selector, group);
395         return 0;
396 }
397
398 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
399 static const struct pinmux_ops samsung_pinmux_ops = {
400         .get_functions_count    = samsung_get_functions_count,
401         .get_function_name      = samsung_pinmux_get_fname,
402         .get_function_groups    = samsung_pinmux_get_groups,
403         .set_mux                = samsung_pinmux_set_mux,
404 };
405
406 /* set or get the pin config settings for a specified pin */
407 static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
408                                 unsigned long *config, bool set)
409 {
410         struct samsung_pinctrl_drv_data *drvdata;
411         const struct samsung_pin_bank_type *type;
412         struct samsung_pin_bank *bank;
413         void __iomem *reg_base;
414         enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
415         u32 data, width, pin_offset, mask, shift;
416         u32 cfg_value, cfg_reg;
417         unsigned long flags;
418
419         drvdata = pinctrl_dev_get_drvdata(pctldev);
420         pin_to_reg_bank(drvdata, pin - drvdata->pin_base, &reg_base,
421                                         &pin_offset, &bank);
422         type = bank->type;
423
424         if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type])
425                 return -EINVAL;
426
427         width = type->fld_width[cfg_type];
428         cfg_reg = type->reg_offset[cfg_type];
429
430         spin_lock_irqsave(&bank->slock, flags);
431
432         mask = (1 << width) - 1;
433         shift = pin_offset * width;
434         data = readl(reg_base + cfg_reg);
435
436         if (set) {
437                 cfg_value = PINCFG_UNPACK_VALUE(*config);
438                 data &= ~(mask << shift);
439                 data |= (cfg_value << shift);
440                 writel(data, reg_base + cfg_reg);
441         } else {
442                 data >>= shift;
443                 data &= mask;
444                 *config = PINCFG_PACK(cfg_type, data);
445         }
446
447         spin_unlock_irqrestore(&bank->slock, flags);
448
449         return 0;
450 }
451
452 /* set the pin config settings for a specified pin */
453 static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
454                                 unsigned long *configs, unsigned num_configs)
455 {
456         int i, ret;
457
458         for (i = 0; i < num_configs; i++) {
459                 ret = samsung_pinconf_rw(pctldev, pin, &configs[i], true);
460                 if (ret < 0)
461                         return ret;
462         } /* for each config */
463
464         return 0;
465 }
466
467 /* get the pin config settings for a specified pin */
468 static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
469                                         unsigned long *config)
470 {
471         return samsung_pinconf_rw(pctldev, pin, config, false);
472 }
473
474 /* set the pin config settings for a specified pin group */
475 static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
476                         unsigned group, unsigned long *configs,
477                         unsigned num_configs)
478 {
479         struct samsung_pinctrl_drv_data *drvdata;
480         const unsigned int *pins;
481         unsigned int cnt;
482
483         drvdata = pinctrl_dev_get_drvdata(pctldev);
484         pins = drvdata->pin_groups[group].pins;
485
486         for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
487                 samsung_pinconf_set(pctldev, pins[cnt], configs, num_configs);
488
489         return 0;
490 }
491
492 /* get the pin config settings for a specified pin group */
493 static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
494                                 unsigned int group, unsigned long *config)
495 {
496         struct samsung_pinctrl_drv_data *drvdata;
497         const unsigned int *pins;
498
499         drvdata = pinctrl_dev_get_drvdata(pctldev);
500         pins = drvdata->pin_groups[group].pins;
501         samsung_pinconf_get(pctldev, pins[0], config);
502         return 0;
503 }
504
505 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
506 static const struct pinconf_ops samsung_pinconf_ops = {
507         .pin_config_get         = samsung_pinconf_get,
508         .pin_config_set         = samsung_pinconf_set,
509         .pin_config_group_get   = samsung_pinconf_group_get,
510         .pin_config_group_set   = samsung_pinconf_group_set,
511 };
512
513 /*
514  * The samsung_gpio_set_vlaue() should be called with "bank->slock" held
515  * to avoid race condition.
516  */
517 static void samsung_gpio_set_value(struct gpio_chip *gc,
518                                           unsigned offset, int value)
519 {
520         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
521         const struct samsung_pin_bank_type *type = bank->type;
522         void __iomem *reg;
523         u32 data;
524
525         reg = bank->pctl_base + bank->pctl_offset;
526
527         data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
528         data &= ~(1 << offset);
529         if (value)
530                 data |= 1 << offset;
531         writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]);
532 }
533
534 /* gpiolib gpio_set callback function */
535 static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
536 {
537         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
538         unsigned long flags;
539
540         spin_lock_irqsave(&bank->slock, flags);
541         samsung_gpio_set_value(gc, offset, value);
542         spin_unlock_irqrestore(&bank->slock, flags);
543 }
544
545 /* gpiolib gpio_get callback function */
546 static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
547 {
548         void __iomem *reg;
549         u32 data;
550         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
551         const struct samsung_pin_bank_type *type = bank->type;
552
553         reg = bank->pctl_base + bank->pctl_offset;
554
555         data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
556         data >>= offset;
557         data &= 1;
558         return data;
559 }
560
561 /*
562  * The samsung_gpio_set_direction() should be called with "bank->slock" held
563  * to avoid race condition.
564  * The calls to gpio_direction_output() and gpio_direction_input()
565  * leads to this function call.
566  */
567 static int samsung_gpio_set_direction(struct gpio_chip *gc,
568                                              unsigned offset, bool input)
569 {
570         const struct samsung_pin_bank_type *type;
571         struct samsung_pin_bank *bank;
572         void __iomem *reg;
573         u32 data, mask, shift;
574
575         bank = gpiochip_get_data(gc);
576         type = bank->type;
577
578         reg = bank->pctl_base + bank->pctl_offset
579                         + type->reg_offset[PINCFG_TYPE_FUNC];
580
581         mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
582         shift = offset * type->fld_width[PINCFG_TYPE_FUNC];
583         if (shift >= 32) {
584                 /* Some banks have two config registers */
585                 shift -= 32;
586                 reg += 4;
587         }
588
589         data = readl(reg);
590         data &= ~(mask << shift);
591         if (!input)
592                 data |= EXYNOS_PIN_FUNC_OUTPUT << shift;
593         writel(data, reg);
594
595         return 0;
596 }
597
598 /* gpiolib gpio_direction_input callback function. */
599 static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
600 {
601         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
602         unsigned long flags;
603         int ret;
604
605         spin_lock_irqsave(&bank->slock, flags);
606         ret = samsung_gpio_set_direction(gc, offset, true);
607         spin_unlock_irqrestore(&bank->slock, flags);
608         return ret;
609 }
610
611 /* gpiolib gpio_direction_output callback function. */
612 static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
613                                                         int value)
614 {
615         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
616         unsigned long flags;
617         int ret;
618
619         spin_lock_irqsave(&bank->slock, flags);
620         samsung_gpio_set_value(gc, offset, value);
621         ret = samsung_gpio_set_direction(gc, offset, false);
622         spin_unlock_irqrestore(&bank->slock, flags);
623
624         return ret;
625 }
626
627 /*
628  * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
629  * and a virtual IRQ, if not already present.
630  */
631 static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
632 {
633         struct samsung_pin_bank *bank = gpiochip_get_data(gc);
634         unsigned int virq;
635
636         if (!bank->irq_domain)
637                 return -ENXIO;
638
639         virq = irq_create_mapping(bank->irq_domain, offset);
640
641         return (virq) ? : -ENXIO;
642 }
643
644 static struct samsung_pin_group *samsung_pinctrl_create_groups(
645                                 struct device *dev,
646                                 struct samsung_pinctrl_drv_data *drvdata,
647                                 unsigned int *cnt)
648 {
649         struct pinctrl_desc *ctrldesc = &drvdata->pctl;
650         struct samsung_pin_group *groups, *grp;
651         const struct pinctrl_pin_desc *pdesc;
652         int i;
653
654         groups = devm_kzalloc(dev, ctrldesc->npins * sizeof(*groups),
655                                 GFP_KERNEL);
656         if (!groups)
657                 return ERR_PTR(-EINVAL);
658         grp = groups;
659
660         pdesc = ctrldesc->pins;
661         for (i = 0; i < ctrldesc->npins; ++i, ++pdesc, ++grp) {
662                 grp->name = pdesc->name;
663                 grp->pins = &pdesc->number;
664                 grp->num_pins = 1;
665         }
666
667         *cnt = ctrldesc->npins;
668         return groups;
669 }
670
671 static int samsung_pinctrl_create_function(struct device *dev,
672                                 struct samsung_pinctrl_drv_data *drvdata,
673                                 struct device_node *func_np,
674                                 struct samsung_pmx_func *func)
675 {
676         int npins;
677         int ret;
678         int i;
679
680         if (of_property_read_u32(func_np, "samsung,pin-function", &func->val))
681                 return 0;
682
683         npins = of_property_count_strings(func_np, "samsung,pins");
684         if (npins < 1) {
685                 dev_err(dev, "invalid pin list in %pOFn node", func_np);
686                 return -EINVAL;
687         }
688
689         func->name = func_np->full_name;
690
691         func->groups = devm_kzalloc(dev, npins * sizeof(char *), GFP_KERNEL);
692         if (!func->groups)
693                 return -ENOMEM;
694
695         for (i = 0; i < npins; ++i) {
696                 const char *gname;
697
698                 ret = of_property_read_string_index(func_np, "samsung,pins",
699                                                         i, &gname);
700                 if (ret) {
701                         dev_err(dev,
702                                 "failed to read pin name %d from %pOFn node\n",
703                                 i, func_np);
704                         return ret;
705                 }
706
707                 func->groups[i] = gname;
708         }
709
710         func->num_groups = npins;
711         return 1;
712 }
713
714 static struct samsung_pmx_func *samsung_pinctrl_create_functions(
715                                 struct device *dev,
716                                 struct samsung_pinctrl_drv_data *drvdata,
717                                 unsigned int *cnt)
718 {
719         struct samsung_pmx_func *functions, *func;
720         struct device_node *dev_np = dev->of_node;
721         struct device_node *cfg_np;
722         unsigned int func_cnt = 0;
723         int ret;
724
725         /*
726          * Iterate over all the child nodes of the pin controller node
727          * and create pin groups and pin function lists.
728          */
729         for_each_child_of_node(dev_np, cfg_np) {
730                 struct device_node *func_np;
731
732                 if (!of_get_child_count(cfg_np)) {
733                         if (!of_find_property(cfg_np,
734                             "samsung,pin-function", NULL))
735                                 continue;
736                         ++func_cnt;
737                         continue;
738                 }
739
740                 for_each_child_of_node(cfg_np, func_np) {
741                         if (!of_find_property(func_np,
742                             "samsung,pin-function", NULL))
743                                 continue;
744                         ++func_cnt;
745                 }
746         }
747
748         functions = devm_kzalloc(dev, func_cnt * sizeof(*functions),
749                                         GFP_KERNEL);
750         if (!functions)
751                 return ERR_PTR(-ENOMEM);
752         func = functions;
753
754         /*
755          * Iterate over all the child nodes of the pin controller node
756          * and create pin groups and pin function lists.
757          */
758         func_cnt = 0;
759         for_each_child_of_node(dev_np, cfg_np) {
760                 struct device_node *func_np;
761
762                 if (!of_get_child_count(cfg_np)) {
763                         ret = samsung_pinctrl_create_function(dev, drvdata,
764                                                         cfg_np, func);
765                         if (ret < 0) {
766                                 of_node_put(cfg_np);
767                                 return ERR_PTR(ret);
768                         }
769                         if (ret > 0) {
770                                 ++func;
771                                 ++func_cnt;
772                         }
773                         continue;
774                 }
775
776                 for_each_child_of_node(cfg_np, func_np) {
777                         ret = samsung_pinctrl_create_function(dev, drvdata,
778                                                 func_np, func);
779                         if (ret < 0) {
780                                 of_node_put(func_np);
781                                 of_node_put(cfg_np);
782                                 return ERR_PTR(ret);
783                         }
784                         if (ret > 0) {
785                                 ++func;
786                                 ++func_cnt;
787                         }
788                 }
789         }
790
791         *cnt = func_cnt;
792         return functions;
793 }
794
795 /*
796  * Parse the information about all the available pin groups and pin functions
797  * from device node of the pin-controller. A pin group is formed with all
798  * the pins listed in the "samsung,pins" property.
799  */
800
801 static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
802                                     struct samsung_pinctrl_drv_data *drvdata)
803 {
804         struct device *dev = &pdev->dev;
805         struct samsung_pin_group *groups;
806         struct samsung_pmx_func *functions;
807         unsigned int grp_cnt = 0, func_cnt = 0;
808
809         groups = samsung_pinctrl_create_groups(dev, drvdata, &grp_cnt);
810         if (IS_ERR(groups)) {
811                 dev_err(dev, "failed to parse pin groups\n");
812                 return PTR_ERR(groups);
813         }
814
815         functions = samsung_pinctrl_create_functions(dev, drvdata, &func_cnt);
816         if (IS_ERR(functions)) {
817                 dev_err(dev, "failed to parse pin functions\n");
818                 return PTR_ERR(functions);
819         }
820
821         drvdata->pin_groups = groups;
822         drvdata->nr_groups = grp_cnt;
823         drvdata->pmx_functions = functions;
824         drvdata->nr_functions = func_cnt;
825
826         return 0;
827 }
828
829 /* register the pinctrl interface with the pinctrl subsystem */
830 static int samsung_pinctrl_register(struct platform_device *pdev,
831                                     struct samsung_pinctrl_drv_data *drvdata)
832 {
833         struct pinctrl_desc *ctrldesc = &drvdata->pctl;
834         struct pinctrl_pin_desc *pindesc, *pdesc;
835         struct samsung_pin_bank *pin_bank;
836         char *pin_names;
837         int pin, bank, ret;
838
839         ctrldesc->name = "samsung-pinctrl";
840         ctrldesc->owner = THIS_MODULE;
841         ctrldesc->pctlops = &samsung_pctrl_ops;
842         ctrldesc->pmxops = &samsung_pinmux_ops;
843         ctrldesc->confops = &samsung_pinconf_ops;
844
845         pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
846                         drvdata->nr_pins, GFP_KERNEL);
847         if (!pindesc)
848                 return -ENOMEM;
849         ctrldesc->pins = pindesc;
850         ctrldesc->npins = drvdata->nr_pins;
851
852         /* dynamically populate the pin number and pin name for pindesc */
853         for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
854                 pdesc->number = pin + drvdata->pin_base;
855
856         /*
857          * allocate space for storing the dynamically generated names for all
858          * the pins which belong to this pin-controller.
859          */
860         pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
861                                         drvdata->nr_pins, GFP_KERNEL);
862         if (!pin_names)
863                 return -ENOMEM;
864
865         /* for each pin, the name of the pin is pin-bank name + pin number */
866         for (bank = 0; bank < drvdata->nr_banks; bank++) {
867                 pin_bank = &drvdata->pin_banks[bank];
868                 for (pin = 0; pin < pin_bank->nr_pins; pin++) {
869                         sprintf(pin_names, "%s-%d", pin_bank->name, pin);
870                         pdesc = pindesc + pin_bank->pin_base + pin;
871                         pdesc->name = pin_names;
872                         pin_names += PIN_NAME_LENGTH;
873                 }
874         }
875
876         ret = samsung_pinctrl_parse_dt(pdev, drvdata);
877         if (ret)
878                 return ret;
879
880         drvdata->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc,
881                                                   drvdata);
882         if (IS_ERR(drvdata->pctl_dev)) {
883                 dev_err(&pdev->dev, "could not register pinctrl driver\n");
884                 return PTR_ERR(drvdata->pctl_dev);
885         }
886
887         for (bank = 0; bank < drvdata->nr_banks; ++bank) {
888                 pin_bank = &drvdata->pin_banks[bank];
889                 pin_bank->grange.name = pin_bank->name;
890                 pin_bank->grange.id = bank;
891                 pin_bank->grange.pin_base = drvdata->pin_base
892                                                 + pin_bank->pin_base;
893                 pin_bank->grange.base = pin_bank->grange.pin_base;
894                 pin_bank->grange.npins = pin_bank->nr_pins;
895                 pin_bank->grange.gc = &pin_bank->gpio_chip;
896                 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
897         }
898
899         return 0;
900 }
901
902 /* unregister the pinctrl interface with the pinctrl subsystem */
903 static int samsung_pinctrl_unregister(struct platform_device *pdev,
904                                       struct samsung_pinctrl_drv_data *drvdata)
905 {
906         struct samsung_pin_bank *bank = drvdata->pin_banks;
907         int i;
908
909         for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
910                 pinctrl_remove_gpio_range(drvdata->pctl_dev, &bank->grange);
911
912         return 0;
913 }
914
915 static const struct gpio_chip samsung_gpiolib_chip = {
916         .request = gpiochip_generic_request,
917         .free = gpiochip_generic_free,
918         .set = samsung_gpio_set,
919         .get = samsung_gpio_get,
920         .direction_input = samsung_gpio_direction_input,
921         .direction_output = samsung_gpio_direction_output,
922         .to_irq = samsung_gpio_to_irq,
923         .owner = THIS_MODULE,
924 };
925
926 /* register the gpiolib interface with the gpiolib subsystem */
927 static int samsung_gpiolib_register(struct platform_device *pdev,
928                                     struct samsung_pinctrl_drv_data *drvdata)
929 {
930         struct samsung_pin_bank *bank = drvdata->pin_banks;
931         struct gpio_chip *gc;
932         int ret;
933         int i;
934
935         for (i = 0; i < drvdata->nr_banks; ++i, ++bank) {
936                 bank->gpio_chip = samsung_gpiolib_chip;
937
938                 gc = &bank->gpio_chip;
939                 gc->base = bank->grange.base;
940                 gc->ngpio = bank->nr_pins;
941                 gc->parent = &pdev->dev;
942                 gc->of_node = bank->of_node;
943                 gc->label = bank->name;
944
945                 ret = devm_gpiochip_add_data(&pdev->dev, gc, bank);
946                 if (ret) {
947                         dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
948                                                         gc->label, ret);
949                         return ret;
950                 }
951         }
952
953         return 0;
954 }
955
956 static const struct samsung_pin_ctrl *
957 samsung_pinctrl_get_soc_data_for_of_alias(struct platform_device *pdev)
958 {
959         struct device_node *node = pdev->dev.of_node;
960         const struct samsung_pinctrl_of_match_data *of_data;
961         int id;
962
963         id = of_alias_get_id(node, "pinctrl");
964         if (id < 0) {
965                 dev_err(&pdev->dev, "failed to get alias id\n");
966                 return NULL;
967         }
968
969         of_data = of_device_get_match_data(&pdev->dev);
970         if (id >= of_data->num_ctrl) {
971                 dev_err(&pdev->dev, "invalid alias id %d\n", id);
972                 return NULL;
973         }
974
975         return &(of_data->ctrl[id]);
976 }
977
978 static void samsung_banks_of_node_put(struct samsung_pinctrl_drv_data *d)
979 {
980         struct samsung_pin_bank *bank;
981         unsigned int i;
982
983         bank = d->pin_banks;
984         for (i = 0; i < d->nr_banks; ++i, ++bank)
985                 of_node_put(bank->of_node);
986 }
987
988 /* retrieve the soc specific data */
989 static const struct samsung_pin_ctrl *
990 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data *d,
991                              struct platform_device *pdev)
992 {
993         struct device_node *node = pdev->dev.of_node;
994         struct device_node *np;
995         const struct samsung_pin_bank_data *bdata;
996         const struct samsung_pin_ctrl *ctrl;
997         struct samsung_pin_bank *bank;
998         struct resource *res;
999         void __iomem *virt_base[SAMSUNG_PINCTRL_NUM_RESOURCES];
1000         unsigned int i;
1001
1002         ctrl = samsung_pinctrl_get_soc_data_for_of_alias(pdev);
1003         if (!ctrl)
1004                 return ERR_PTR(-ENOENT);
1005
1006         d->suspend = ctrl->suspend;
1007         d->resume = ctrl->resume;
1008         d->nr_banks = ctrl->nr_banks;
1009         d->pin_banks = devm_kcalloc(&pdev->dev, d->nr_banks,
1010                                         sizeof(*d->pin_banks), GFP_KERNEL);
1011         if (!d->pin_banks)
1012                 return ERR_PTR(-ENOMEM);
1013
1014         if (ctrl->nr_ext_resources + 1 > SAMSUNG_PINCTRL_NUM_RESOURCES)
1015                 return ERR_PTR(-EINVAL);
1016
1017         for (i = 0; i < ctrl->nr_ext_resources + 1; i++) {
1018                 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1019                 if (!res) {
1020                         dev_err(&pdev->dev, "failed to get mem%d resource\n", i);
1021                         return ERR_PTR(-EINVAL);
1022                 }
1023                 virt_base[i] = devm_ioremap(&pdev->dev, res->start,
1024                                                 resource_size(res));
1025                 if (!virt_base[i]) {
1026                         dev_err(&pdev->dev, "failed to ioremap %pR\n", res);
1027                         return ERR_PTR(-EIO);
1028                 }
1029         }
1030
1031         bank = d->pin_banks;
1032         bdata = ctrl->pin_banks;
1033         for (i = 0; i < ctrl->nr_banks; ++i, ++bdata, ++bank) {
1034                 bank->type = bdata->type;
1035                 bank->pctl_offset = bdata->pctl_offset;
1036                 bank->nr_pins = bdata->nr_pins;
1037                 bank->eint_func = bdata->eint_func;
1038                 bank->eint_type = bdata->eint_type;
1039                 bank->eint_mask = bdata->eint_mask;
1040                 bank->eint_offset = bdata->eint_offset;
1041                 bank->name = bdata->name;
1042
1043                 spin_lock_init(&bank->slock);
1044                 bank->drvdata = d;
1045                 bank->pin_base = d->nr_pins;
1046                 d->nr_pins += bank->nr_pins;
1047
1048                 bank->eint_base = virt_base[0];
1049                 bank->pctl_base = virt_base[bdata->pctl_res_idx];
1050         }
1051         /*
1052          * Legacy platforms should provide only one resource with IO memory.
1053          * Store it as virt_base because legacy driver needs to access it
1054          * through samsung_pinctrl_drv_data.
1055          */
1056         d->virt_base = virt_base[0];
1057
1058         for_each_child_of_node(node, np) {
1059                 if (!of_find_property(np, "gpio-controller", NULL))
1060                         continue;
1061                 bank = d->pin_banks;
1062                 for (i = 0; i < d->nr_banks; ++i, ++bank) {
1063                         if (!strcmp(bank->name, np->name)) {
1064                                 bank->of_node = np;
1065                                 break;
1066                         }
1067                 }
1068         }
1069
1070         d->pin_base = pin_base;
1071         pin_base += d->nr_pins;
1072
1073         return ctrl;
1074 }
1075
1076 static int samsung_pinctrl_probe(struct platform_device *pdev)
1077 {
1078         struct samsung_pinctrl_drv_data *drvdata;
1079         const struct samsung_pin_ctrl *ctrl;
1080         struct device *dev = &pdev->dev;
1081         struct resource *res;
1082         int ret;
1083
1084         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1085         if (!drvdata)
1086                 return -ENOMEM;
1087
1088         ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
1089         if (IS_ERR(ctrl)) {
1090                 dev_err(&pdev->dev, "driver data not available\n");
1091                 return PTR_ERR(ctrl);
1092         }
1093         drvdata->dev = dev;
1094
1095         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1096         if (res)
1097                 drvdata->irq = res->start;
1098
1099         if (ctrl->retention_data) {
1100                 drvdata->retention_ctrl = ctrl->retention_data->init(drvdata,
1101                                                           ctrl->retention_data);
1102                 if (IS_ERR(drvdata->retention_ctrl)) {
1103                         ret = PTR_ERR(drvdata->retention_ctrl);
1104                         goto err_put_banks;
1105                 }
1106         }
1107
1108         ret = samsung_pinctrl_register(pdev, drvdata);
1109         if (ret)
1110                 goto err_put_banks;
1111
1112         ret = samsung_gpiolib_register(pdev, drvdata);
1113         if (ret)
1114                 goto err_unregister;
1115
1116         if (ctrl->eint_gpio_init)
1117                 ctrl->eint_gpio_init(drvdata);
1118         if (ctrl->eint_wkup_init)
1119                 ctrl->eint_wkup_init(drvdata);
1120
1121         platform_set_drvdata(pdev, drvdata);
1122
1123         return 0;
1124
1125 err_unregister:
1126         samsung_pinctrl_unregister(pdev, drvdata);
1127 err_put_banks:
1128         samsung_banks_of_node_put(drvdata);
1129         return ret;
1130 }
1131
1132 /**
1133  * samsung_pinctrl_suspend - save pinctrl state for suspend
1134  *
1135  * Save data for all banks handled by this device.
1136  */
1137 static int __maybe_unused samsung_pinctrl_suspend(struct device *dev)
1138 {
1139         struct samsung_pinctrl_drv_data *drvdata = dev_get_drvdata(dev);
1140         int i;
1141
1142         for (i = 0; i < drvdata->nr_banks; i++) {
1143                 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
1144                 void __iomem *reg = bank->pctl_base + bank->pctl_offset;
1145                 const u8 *offs = bank->type->reg_offset;
1146                 const u8 *widths = bank->type->fld_width;
1147                 enum pincfg_type type;
1148
1149                 /* Registers without a powerdown config aren't lost */
1150                 if (!widths[PINCFG_TYPE_CON_PDN])
1151                         continue;
1152
1153                 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1154                         if (widths[type])
1155                                 bank->pm_save[type] = readl(reg + offs[type]);
1156
1157                 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1158                         /* Some banks have two config registers */
1159                         bank->pm_save[PINCFG_TYPE_NUM] =
1160                                 readl(reg + offs[PINCFG_TYPE_FUNC] + 4);
1161                         pr_debug("Save %s @ %p (con %#010x %08x)\n",
1162                                  bank->name, reg,
1163                                  bank->pm_save[PINCFG_TYPE_FUNC],
1164                                  bank->pm_save[PINCFG_TYPE_NUM]);
1165                 } else {
1166                         pr_debug("Save %s @ %p (con %#010x)\n", bank->name,
1167                                  reg, bank->pm_save[PINCFG_TYPE_FUNC]);
1168                 }
1169         }
1170
1171         if (drvdata->suspend)
1172                 drvdata->suspend(drvdata);
1173         if (drvdata->retention_ctrl && drvdata->retention_ctrl->enable)
1174                 drvdata->retention_ctrl->enable(drvdata);
1175
1176         return 0;
1177 }
1178
1179 /**
1180  * samsung_pinctrl_resume - restore pinctrl state from suspend
1181  *
1182  * Restore one of the banks that was saved during suspend.
1183  *
1184  * We don't bother doing anything complicated to avoid glitching lines since
1185  * we're called before pad retention is turned off.
1186  */
1187 static int __maybe_unused samsung_pinctrl_resume(struct device *dev)
1188 {
1189         struct samsung_pinctrl_drv_data *drvdata = dev_get_drvdata(dev);
1190         int i;
1191
1192         if (drvdata->resume)
1193                 drvdata->resume(drvdata);
1194
1195         for (i = 0; i < drvdata->nr_banks; i++) {
1196                 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
1197                 void __iomem *reg = bank->pctl_base + bank->pctl_offset;
1198                 const u8 *offs = bank->type->reg_offset;
1199                 const u8 *widths = bank->type->fld_width;
1200                 enum pincfg_type type;
1201
1202                 /* Registers without a powerdown config aren't lost */
1203                 if (!widths[PINCFG_TYPE_CON_PDN])
1204                         continue;
1205
1206                 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1207                         /* Some banks have two config registers */
1208                         pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1209                                  bank->name, reg,
1210                                  readl(reg + offs[PINCFG_TYPE_FUNC]),
1211                                  readl(reg + offs[PINCFG_TYPE_FUNC] + 4),
1212                                  bank->pm_save[PINCFG_TYPE_FUNC],
1213                                  bank->pm_save[PINCFG_TYPE_NUM]);
1214                         writel(bank->pm_save[PINCFG_TYPE_NUM],
1215                                reg + offs[PINCFG_TYPE_FUNC] + 4);
1216                 } else {
1217                         pr_debug("%s @ %p (con %#010x => %#010x)\n", bank->name,
1218                                  reg, readl(reg + offs[PINCFG_TYPE_FUNC]),
1219                                  bank->pm_save[PINCFG_TYPE_FUNC]);
1220                 }
1221                 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1222                         if (widths[type])
1223                                 writel(bank->pm_save[type], reg + offs[type]);
1224         }
1225
1226         if (drvdata->retention_ctrl && drvdata->retention_ctrl->disable)
1227                 drvdata->retention_ctrl->disable(drvdata);
1228
1229         return 0;
1230 }
1231
1232 static const struct of_device_id samsung_pinctrl_dt_match[] = {
1233 #ifdef CONFIG_PINCTRL_EXYNOS_ARM
1234         { .compatible = "samsung,exynos3250-pinctrl",
1235                 .data = &exynos3250_of_data },
1236         { .compatible = "samsung,exynos4210-pinctrl",
1237                 .data = &exynos4210_of_data },
1238         { .compatible = "samsung,exynos4x12-pinctrl",
1239                 .data = &exynos4x12_of_data },
1240         { .compatible = "samsung,exynos5250-pinctrl",
1241                 .data = &exynos5250_of_data },
1242         { .compatible = "samsung,exynos5260-pinctrl",
1243                 .data = &exynos5260_of_data },
1244         { .compatible = "samsung,exynos5410-pinctrl",
1245                 .data = &exynos5410_of_data },
1246         { .compatible = "samsung,exynos5420-pinctrl",
1247                 .data = &exynos5420_of_data },
1248         { .compatible = "samsung,s5pv210-pinctrl",
1249                 .data = &s5pv210_of_data },
1250 #endif
1251 #ifdef CONFIG_PINCTRL_EXYNOS_ARM64
1252         { .compatible = "samsung,exynos5433-pinctrl",
1253                 .data = &exynos5433_of_data },
1254         { .compatible = "samsung,exynos7-pinctrl",
1255                 .data = &exynos7_of_data },
1256 #endif
1257 #ifdef CONFIG_PINCTRL_S3C64XX
1258         { .compatible = "samsung,s3c64xx-pinctrl",
1259                 .data = &s3c64xx_of_data },
1260 #endif
1261 #ifdef CONFIG_PINCTRL_S3C24XX
1262         { .compatible = "samsung,s3c2412-pinctrl",
1263                 .data = &s3c2412_of_data },
1264         { .compatible = "samsung,s3c2416-pinctrl",
1265                 .data = &s3c2416_of_data },
1266         { .compatible = "samsung,s3c2440-pinctrl",
1267                 .data = &s3c2440_of_data },
1268         { .compatible = "samsung,s3c2450-pinctrl",
1269                 .data = &s3c2450_of_data },
1270 #endif
1271         {},
1272 };
1273
1274 static const struct dev_pm_ops samsung_pinctrl_pm_ops = {
1275         SET_LATE_SYSTEM_SLEEP_PM_OPS(samsung_pinctrl_suspend,
1276                                      samsung_pinctrl_resume)
1277 };
1278
1279 static struct platform_driver samsung_pinctrl_driver = {
1280         .probe          = samsung_pinctrl_probe,
1281         .driver = {
1282                 .name   = "samsung-pinctrl",
1283                 .of_match_table = samsung_pinctrl_dt_match,
1284                 .suppress_bind_attrs = true,
1285                 .pm = &samsung_pinctrl_pm_ops,
1286         },
1287 };
1288
1289 static int __init samsung_pinctrl_drv_register(void)
1290 {
1291         return platform_driver_register(&samsung_pinctrl_driver);
1292 }
1293 postcore_initcall(samsung_pinctrl_drv_register);