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