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