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