1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/platform_device.h>
12#include <linux/io.h>
13#include <linux/slab.h>
14#include <linux/err.h>
15#include <linux/gpio/driver.h>
16#include <linux/device.h>
17#include <linux/pinctrl/pinctrl.h>
18#include <linux/pinctrl/pinmux.h>
19#include <linux/pinctrl/pinconf.h>
20#include <linux/interrupt.h>
21#include <linux/irqdomain.h>
22#include <linux/of_irq.h>
23#include "../core.h"
24
25
26#define GPIO_MUX 0x00
27#define GPIO_IE 0x04
28#define GPIO_INT 0x08
29#define GPIO_TYPE 0x0C
30#define GPIO_VAL 0x10
31#define GPIO_OE 0x14
32#define GPIO_IN 0x18
33#define GPIO_PE 0x1C
34#define GPIO_PS 0x20
35#define GPIO_SR 0x24
36#define GPIO_DS0 0x28
37#define GPIO_DS1 0x2C
38
39#define EXYNOS5440_MAX_PINS 23
40#define EXYNOS5440_MAX_GPIO_INT 8
41#define PIN_NAME_LENGTH 10
42
43#define GROUP_SUFFIX "-grp"
44#define FUNCTION_SUFFIX "-mux"
45
46
47
48
49
50
51#define PINCFG_TYPE_MASK 0xFF
52#define PINCFG_VALUE_SHIFT 8
53#define PINCFG_VALUE_MASK (0xFF << PINCFG_VALUE_SHIFT)
54#define PINCFG_PACK(type, value) (((value) << PINCFG_VALUE_SHIFT) | type)
55#define PINCFG_UNPACK_TYPE(cfg) ((cfg) & PINCFG_TYPE_MASK)
56#define PINCFG_UNPACK_VALUE(cfg) (((cfg) & PINCFG_VALUE_MASK) >> \
57 PINCFG_VALUE_SHIFT)
58
59
60
61
62
63
64
65
66enum pincfg_type {
67 PINCFG_TYPE_PUD,
68 PINCFG_TYPE_DRV,
69 PINCFG_TYPE_SKEW_RATE,
70 PINCFG_TYPE_INPUT_TYPE
71};
72
73
74
75
76
77
78
79struct exynos5440_pin_group {
80 const char *name;
81 const unsigned int *pins;
82 u8 num_pins;
83};
84
85
86
87
88
89
90
91
92struct exynos5440_pmx_func {
93 const char *name;
94 const char **groups;
95 u8 num_groups;
96 unsigned long function;
97};
98
99
100
101
102
103
104
105
106
107
108
109struct exynos5440_pinctrl_priv_data {
110 void __iomem *reg_base;
111 struct gpio_chip *gc;
112 struct irq_domain *irq_domain;
113
114 const struct exynos5440_pin_group *pin_groups;
115 unsigned int nr_groups;
116 const struct exynos5440_pmx_func *pmx_functions;
117 unsigned int nr_functions;
118 struct pinctrl_gpio_range range;
119};
120
121
122
123
124
125
126struct exynos5440_gpio_intr_data {
127 struct exynos5440_pinctrl_priv_data *priv;
128 unsigned int gpio_int;
129};
130
131
132static struct pin_config {
133 char *prop_cfg;
134 unsigned int cfg_type;
135} pcfgs[] = {
136 { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD },
137 { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV },
138 { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE },
139 { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE },
140};
141
142
143static int exynos5440_get_group_count(struct pinctrl_dev *pctldev)
144{
145 struct exynos5440_pinctrl_priv_data *priv;
146
147 priv = pinctrl_dev_get_drvdata(pctldev);
148 return priv->nr_groups;
149}
150
151
152static const char *exynos5440_get_group_name(struct pinctrl_dev *pctldev,
153 unsigned selector)
154{
155 struct exynos5440_pinctrl_priv_data *priv;
156
157 priv = pinctrl_dev_get_drvdata(pctldev);
158 return priv->pin_groups[selector].name;
159}
160
161
162static int exynos5440_get_group_pins(struct pinctrl_dev *pctldev,
163 unsigned selector, const unsigned **pins, unsigned *num_pins)
164{
165 struct exynos5440_pinctrl_priv_data *priv;
166
167 priv = pinctrl_dev_get_drvdata(pctldev);
168 *pins = priv->pin_groups[selector].pins;
169 *num_pins = priv->pin_groups[selector].num_pins;
170 return 0;
171}
172
173
174static int exynos5440_dt_node_to_map(struct pinctrl_dev *pctldev,
175 struct device_node *np, struct pinctrl_map **maps,
176 unsigned *nmaps)
177{
178 struct device *dev = pctldev->dev;
179 struct pinctrl_map *map;
180 unsigned long *cfg = NULL;
181 char *gname, *fname;
182 int cfg_cnt = 0, map_cnt = 0, idx = 0;
183
184
185 for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++)
186 if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
187 cfg_cnt++;
188
189
190
191
192
193 if (cfg_cnt)
194 map_cnt = 1;
195 if (of_find_property(np, "samsung,exynos5440-pin-function", NULL))
196 map_cnt++;
197 if (!map_cnt) {
198 dev_err(dev, "node %s does not have either config or function "
199 "configurations\n", np->name);
200 return -EINVAL;
201 }
202
203
204 map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
205 if (!map)
206 return -ENOMEM;
207 *nmaps = 0;
208
209
210
211
212
213 gname = kasprintf(GFP_KERNEL, "%s%s", np->name, GROUP_SUFFIX);
214 if (!gname)
215 goto free_map;
216
217
218
219
220
221 if (!cfg_cnt)
222 goto skip_cfgs;
223
224
225 cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
226 if (!cfg)
227 goto free_gname;
228
229
230 for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
231 u32 value;
232 if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
233 cfg[cfg_cnt++] =
234 PINCFG_PACK(pcfgs[idx].cfg_type, value);
235 }
236
237
238 map[*nmaps].data.configs.group_or_pin = gname;
239 map[*nmaps].data.configs.configs = cfg;
240 map[*nmaps].data.configs.num_configs = cfg_cnt;
241 map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
242 *nmaps += 1;
243
244skip_cfgs:
245
246 if (of_find_property(np, "samsung,exynos5440-pin-function", NULL)) {
247 fname = kasprintf(GFP_KERNEL,
248 "%s%s", np->name, FUNCTION_SUFFIX);
249 if (!fname)
250 goto free_cfg;
251
252 map[*nmaps].data.mux.group = gname;
253 map[*nmaps].data.mux.function = fname;
254 map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
255 *nmaps += 1;
256 }
257
258 *maps = map;
259 return 0;
260
261free_cfg:
262 kfree(cfg);
263free_gname:
264 kfree(gname);
265free_map:
266 kfree(map);
267 return -ENOMEM;
268}
269
270
271static void exynos5440_dt_free_map(struct pinctrl_dev *pctldev,
272 struct pinctrl_map *map, unsigned num_maps)
273{
274 int idx;
275
276 for (idx = 0; idx < num_maps; idx++) {
277 if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
278 kfree(map[idx].data.mux.function);
279 if (!idx)
280 kfree(map[idx].data.mux.group);
281 } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
282 kfree(map[idx].data.configs.configs);
283 if (!idx)
284 kfree(map[idx].data.configs.group_or_pin);
285 }
286 }
287
288 kfree(map);
289}
290
291
292static const struct pinctrl_ops exynos5440_pctrl_ops = {
293 .get_groups_count = exynos5440_get_group_count,
294 .get_group_name = exynos5440_get_group_name,
295 .get_group_pins = exynos5440_get_group_pins,
296 .dt_node_to_map = exynos5440_dt_node_to_map,
297 .dt_free_map = exynos5440_dt_free_map,
298};
299
300
301static int exynos5440_get_functions_count(struct pinctrl_dev *pctldev)
302{
303 struct exynos5440_pinctrl_priv_data *priv;
304
305 priv = pinctrl_dev_get_drvdata(pctldev);
306 return priv->nr_functions;
307}
308
309
310static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev *pctldev,
311 unsigned selector)
312{
313 struct exynos5440_pinctrl_priv_data *priv;
314
315 priv = pinctrl_dev_get_drvdata(pctldev);
316 return priv->pmx_functions[selector].name;
317}
318
319
320static int exynos5440_pinmux_get_groups(struct pinctrl_dev *pctldev,
321 unsigned selector, const char * const **groups,
322 unsigned * const num_groups)
323{
324 struct exynos5440_pinctrl_priv_data *priv;
325
326 priv = pinctrl_dev_get_drvdata(pctldev);
327 *groups = priv->pmx_functions[selector].groups;
328 *num_groups = priv->pmx_functions[selector].num_groups;
329 return 0;
330}
331
332
333static void exynos5440_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
334 unsigned group, bool enable)
335{
336 struct exynos5440_pinctrl_priv_data *priv;
337 void __iomem *base;
338 u32 function;
339 u32 data;
340
341 priv = pinctrl_dev_get_drvdata(pctldev);
342 base = priv->reg_base;
343 function = priv->pmx_functions[selector].function;
344
345 data = readl(base + GPIO_MUX);
346 if (enable)
347 data |= (1 << function);
348 else
349 data &= ~(1 << function);
350 writel(data, base + GPIO_MUX);
351}
352
353
354static int exynos5440_pinmux_set_mux(struct pinctrl_dev *pctldev,
355 unsigned selector,
356 unsigned group)
357{
358 exynos5440_pinmux_setup(pctldev, selector, group, true);
359 return 0;
360}
361
362
363
364
365
366
367static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
368 struct pinctrl_gpio_range *range, unsigned offset, bool input)
369{
370 return 0;
371}
372
373
374static const struct pinmux_ops exynos5440_pinmux_ops = {
375 .get_functions_count = exynos5440_get_functions_count,
376 .get_function_name = exynos5440_pinmux_get_fname,
377 .get_function_groups = exynos5440_pinmux_get_groups,
378 .set_mux = exynos5440_pinmux_set_mux,
379 .gpio_set_direction = exynos5440_pinmux_gpio_set_direction,
380};
381
382
383static int exynos5440_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
384 unsigned long *configs,
385 unsigned num_configs)
386{
387 struct exynos5440_pinctrl_priv_data *priv;
388 void __iomem *base;
389 enum pincfg_type cfg_type;
390 u32 cfg_value;
391 u32 data;
392 int i;
393
394 priv = pinctrl_dev_get_drvdata(pctldev);
395 base = priv->reg_base;
396
397 for (i = 0; i < num_configs; i++) {
398 cfg_type = PINCFG_UNPACK_TYPE(configs[i]);
399 cfg_value = PINCFG_UNPACK_VALUE(configs[i]);
400
401 switch (cfg_type) {
402 case PINCFG_TYPE_PUD:
403
404 data = readl(base + GPIO_PE);
405 data &= ~(1 << pin);
406 if (cfg_value)
407 data |= (1 << pin);
408 writel(data, base + GPIO_PE);
409
410
411 data = readl(base + GPIO_PS);
412 data &= ~(1 << pin);
413 if (cfg_value == 2)
414 data |= (1 << pin);
415 writel(data, base + GPIO_PS);
416 break;
417
418 case PINCFG_TYPE_DRV:
419
420 data = readl(base + GPIO_DS0);
421 data &= ~(1 << pin);
422 data |= ((cfg_value & 1) << pin);
423 writel(data, base + GPIO_DS0);
424 cfg_value >>= 1;
425
426
427 data = readl(base + GPIO_DS1);
428 data &= ~(1 << pin);
429 data |= ((cfg_value & 1) << pin);
430 writel(data, base + GPIO_DS1);
431 break;
432 case PINCFG_TYPE_SKEW_RATE:
433 data = readl(base + GPIO_SR);
434 data &= ~(1 << pin);
435 data |= ((cfg_value & 1) << pin);
436 writel(data, base + GPIO_SR);
437 break;
438 case PINCFG_TYPE_INPUT_TYPE:
439 data = readl(base + GPIO_TYPE);
440 data &= ~(1 << pin);
441 data |= ((cfg_value & 1) << pin);
442 writel(data, base + GPIO_TYPE);
443 break;
444 default:
445 WARN_ON(1);
446 return -EINVAL;
447 }
448 }
449
450 return 0;
451}
452
453
454static int exynos5440_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
455 unsigned long *config)
456{
457 struct exynos5440_pinctrl_priv_data *priv;
458 void __iomem *base;
459 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
460 u32 data;
461
462 priv = pinctrl_dev_get_drvdata(pctldev);
463 base = priv->reg_base;
464
465 switch (cfg_type) {
466 case PINCFG_TYPE_PUD:
467 data = readl(base + GPIO_PE);
468 data = (data >> pin) & 1;
469 if (!data)
470 *config = 0;
471 else
472 *config = ((readl(base + GPIO_PS) >> pin) & 1) + 1;
473 break;
474 case PINCFG_TYPE_DRV:
475 data = readl(base + GPIO_DS0);
476 data = (data >> pin) & 1;
477 *config = data;
478 data = readl(base + GPIO_DS1);
479 data = (data >> pin) & 1;
480 *config |= (data << 1);
481 break;
482 case PINCFG_TYPE_SKEW_RATE:
483 data = readl(base + GPIO_SR);
484 *config = (data >> pin) & 1;
485 break;
486 case PINCFG_TYPE_INPUT_TYPE:
487 data = readl(base + GPIO_TYPE);
488 *config = (data >> pin) & 1;
489 break;
490 default:
491 WARN_ON(1);
492 return -EINVAL;
493 }
494
495 return 0;
496}
497
498
499static int exynos5440_pinconf_group_set(struct pinctrl_dev *pctldev,
500 unsigned group, unsigned long *configs,
501 unsigned num_configs)
502{
503 struct exynos5440_pinctrl_priv_data *priv;
504 const unsigned int *pins;
505 unsigned int cnt;
506
507 priv = pinctrl_dev_get_drvdata(pctldev);
508 pins = priv->pin_groups[group].pins;
509
510 for (cnt = 0; cnt < priv->pin_groups[group].num_pins; cnt++)
511 exynos5440_pinconf_set(pctldev, pins[cnt], configs,
512 num_configs);
513
514 return 0;
515}
516
517
518static int exynos5440_pinconf_group_get(struct pinctrl_dev *pctldev,
519 unsigned int group, unsigned long *config)
520{
521 struct exynos5440_pinctrl_priv_data *priv;
522 const unsigned int *pins;
523
524 priv = pinctrl_dev_get_drvdata(pctldev);
525 pins = priv->pin_groups[group].pins;
526 exynos5440_pinconf_get(pctldev, pins[0], config);
527 return 0;
528}
529
530
531static const struct pinconf_ops exynos5440_pinconf_ops = {
532 .pin_config_get = exynos5440_pinconf_get,
533 .pin_config_set = exynos5440_pinconf_set,
534 .pin_config_group_get = exynos5440_pinconf_group_get,
535 .pin_config_group_set = exynos5440_pinconf_group_set,
536};
537
538
539static void exynos5440_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
540{
541 struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
542 void __iomem *base = priv->reg_base;
543 u32 data;
544
545 data = readl(base + GPIO_VAL);
546 data &= ~(1 << offset);
547 if (value)
548 data |= 1 << offset;
549 writel(data, base + GPIO_VAL);
550}
551
552
553static int exynos5440_gpio_get(struct gpio_chip *gc, unsigned offset)
554{
555 struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
556 void __iomem *base = priv->reg_base;
557 u32 data;
558
559 data = readl(base + GPIO_IN);
560 data >>= offset;
561 data &= 1;
562 return data;
563}
564
565
566static int exynos5440_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
567{
568 struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
569 void __iomem *base = priv->reg_base;
570 u32 data;
571
572
573 data = readl(base + GPIO_OE);
574 data &= ~(1 << offset);
575 writel(data, base + GPIO_OE);
576
577
578 data = readl(base + GPIO_IE);
579 data |= 1 << offset;
580 writel(data, base + GPIO_IE);
581 return 0;
582}
583
584
585static int exynos5440_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
586 int value)
587{
588 struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
589 void __iomem *base = priv->reg_base;
590 u32 data;
591
592 exynos5440_gpio_set(gc, offset, value);
593
594
595 data = readl(base + GPIO_IE);
596 data &= ~(1 << offset);
597 writel(data, base + GPIO_IE);
598
599
600 data = readl(base + GPIO_OE);
601 data |= 1 << offset;
602 writel(data, base + GPIO_OE);
603 return 0;
604}
605
606
607static int exynos5440_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
608{
609 struct exynos5440_pinctrl_priv_data *priv = gpiochip_get_data(gc);
610 unsigned int virq;
611
612 if (offset < 16 || offset > 23)
613 return -ENXIO;
614
615 if (!priv->irq_domain)
616 return -ENXIO;
617
618 virq = irq_create_mapping(priv->irq_domain, offset - 16);
619 return virq ? : -ENXIO;
620}
621
622
623static int exynos5440_pinctrl_parse_dt_pins(struct platform_device *pdev,
624 struct device_node *cfg_np, unsigned int **pin_list,
625 unsigned int *npins)
626{
627 struct device *dev = &pdev->dev;
628 struct property *prop;
629
630 prop = of_find_property(cfg_np, "samsung,exynos5440-pins", NULL);
631 if (!prop)
632 return -ENOENT;
633
634 *npins = prop->length / sizeof(unsigned long);
635 if (!*npins) {
636 dev_err(dev, "invalid pin list in %s node", cfg_np->name);
637 return -EINVAL;
638 }
639
640 *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
641 if (!*pin_list)
642 return -ENOMEM;
643
644 return of_property_read_u32_array(cfg_np, "samsung,exynos5440-pins",
645 *pin_list, *npins);
646}
647
648
649
650
651
652static int exynos5440_pinctrl_parse_dt(struct platform_device *pdev,
653 struct exynos5440_pinctrl_priv_data *priv)
654{
655 struct device *dev = &pdev->dev;
656 struct device_node *dev_np = dev->of_node;
657 struct device_node *cfg_np;
658 struct exynos5440_pin_group *groups, *grp;
659 struct exynos5440_pmx_func *functions, *func;
660 unsigned *pin_list;
661 unsigned int npins, grp_cnt, func_idx = 0;
662 char *gname, *fname;
663 int ret;
664
665 grp_cnt = of_get_child_count(dev_np);
666 if (!grp_cnt)
667 return -EINVAL;
668
669 groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
670 if (!groups)
671 return -EINVAL;
672
673 grp = groups;
674
675 functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
676 if (!functions)
677 return -EINVAL;
678
679 func = functions;
680
681
682
683
684
685 for_each_child_of_node(dev_np, cfg_np) {
686 u32 function;
687
688 ret = exynos5440_pinctrl_parse_dt_pins(pdev, cfg_np,
689 &pin_list, &npins);
690 if (ret) {
691 gname = NULL;
692 goto skip_to_pin_function;
693 }
694
695
696 gname = devm_kasprintf(dev, GFP_KERNEL,
697 "%s%s", cfg_np->name, GROUP_SUFFIX);
698 if (!gname)
699 return -ENOMEM;
700
701 grp->name = gname;
702 grp->pins = pin_list;
703 grp->num_pins = npins;
704 grp++;
705
706skip_to_pin_function:
707 ret = of_property_read_u32(cfg_np, "samsung,exynos5440-pin-function",
708 &function);
709 if (ret)
710 continue;
711
712
713 fname = devm_kasprintf(dev, GFP_KERNEL,
714 "%s%s", cfg_np->name, FUNCTION_SUFFIX);
715 if (!fname)
716 return -ENOMEM;
717
718 func->name = fname;
719 func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
720 if (!func->groups)
721 return -ENOMEM;
722 func->groups[0] = gname;
723 func->num_groups = gname ? 1 : 0;
724 func->function = function;
725 func++;
726 func_idx++;
727 }
728
729 priv->pin_groups = groups;
730 priv->nr_groups = grp_cnt;
731 priv->pmx_functions = functions;
732 priv->nr_functions = func_idx;
733 return 0;
734}
735
736
737static int exynos5440_pinctrl_register(struct platform_device *pdev,
738 struct exynos5440_pinctrl_priv_data *priv)
739{
740 struct device *dev = &pdev->dev;
741 struct pinctrl_desc *ctrldesc;
742 struct pinctrl_dev *pctl_dev;
743 struct pinctrl_pin_desc *pindesc, *pdesc;
744 char *pin_names;
745 int pin, ret;
746
747 ctrldesc = devm_kzalloc(dev, sizeof(*ctrldesc), GFP_KERNEL);
748 if (!ctrldesc)
749 return -ENOMEM;
750
751 ctrldesc->name = "exynos5440-pinctrl";
752 ctrldesc->owner = THIS_MODULE;
753 ctrldesc->pctlops = &exynos5440_pctrl_ops;
754 ctrldesc->pmxops = &exynos5440_pinmux_ops;
755 ctrldesc->confops = &exynos5440_pinconf_ops;
756
757 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
758 EXYNOS5440_MAX_PINS, GFP_KERNEL);
759 if (!pindesc)
760 return -ENOMEM;
761 ctrldesc->pins = pindesc;
762 ctrldesc->npins = EXYNOS5440_MAX_PINS;
763
764
765 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
766 pdesc->number = pin;
767
768
769
770
771
772 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
773 ctrldesc->npins, GFP_KERNEL);
774 if (!pin_names)
775 return -ENOMEM;
776
777
778 for (pin = 0; pin < ctrldesc->npins; pin++) {
779 snprintf(pin_names, 6, "gpio%02d", pin);
780 pdesc = pindesc + pin;
781 pdesc->name = pin_names;
782 pin_names += PIN_NAME_LENGTH;
783 }
784
785 ret = exynos5440_pinctrl_parse_dt(pdev, priv);
786 if (ret)
787 return ret;
788
789 pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, priv);
790 if (IS_ERR(pctl_dev)) {
791 dev_err(&pdev->dev, "could not register pinctrl driver\n");
792 return PTR_ERR(pctl_dev);
793 }
794
795 priv->range.name = "exynos5440-pctrl-gpio-range";
796 priv->range.id = 0;
797 priv->range.base = 0;
798 priv->range.npins = EXYNOS5440_MAX_PINS;
799 priv->range.gc = priv->gc;
800 pinctrl_add_gpio_range(pctl_dev, &priv->range);
801 return 0;
802}
803
804
805static int exynos5440_gpiolib_register(struct platform_device *pdev,
806 struct exynos5440_pinctrl_priv_data *priv)
807{
808 struct gpio_chip *gc;
809 int ret;
810
811 gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
812 if (!gc)
813 return -ENOMEM;
814
815 priv->gc = gc;
816 gc->base = 0;
817 gc->ngpio = EXYNOS5440_MAX_PINS;
818 gc->parent = &pdev->dev;
819 gc->set = exynos5440_gpio_set;
820 gc->get = exynos5440_gpio_get;
821 gc->direction_input = exynos5440_gpio_direction_input;
822 gc->direction_output = exynos5440_gpio_direction_output;
823 gc->to_irq = exynos5440_gpio_to_irq;
824 gc->label = "gpiolib-exynos5440";
825 gc->owner = THIS_MODULE;
826 ret = gpiochip_add_data(gc, priv);
827 if (ret) {
828 dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
829 "code: %d\n", gc->label, ret);
830 return ret;
831 }
832
833 return 0;
834}
835
836
837static int exynos5440_gpiolib_unregister(struct platform_device *pdev,
838 struct exynos5440_pinctrl_priv_data *priv)
839{
840 gpiochip_remove(priv->gc);
841 return 0;
842}
843
844static void exynos5440_gpio_irq_unmask(struct irq_data *irqd)
845{
846 struct exynos5440_pinctrl_priv_data *d;
847 unsigned long gpio_int;
848
849 d = irq_data_get_irq_chip_data(irqd);
850 gpio_int = readl(d->reg_base + GPIO_INT);
851 gpio_int |= 1 << irqd->hwirq;
852 writel(gpio_int, d->reg_base + GPIO_INT);
853}
854
855static void exynos5440_gpio_irq_mask(struct irq_data *irqd)
856{
857 struct exynos5440_pinctrl_priv_data *d;
858 unsigned long gpio_int;
859
860 d = irq_data_get_irq_chip_data(irqd);
861 gpio_int = readl(d->reg_base + GPIO_INT);
862 gpio_int &= ~(1 << irqd->hwirq);
863 writel(gpio_int, d->reg_base + GPIO_INT);
864}
865
866
867static struct irq_chip exynos5440_gpio_irq_chip = {
868 .name = "exynos5440_gpio_irq_chip",
869 .irq_unmask = exynos5440_gpio_irq_unmask,
870 .irq_mask = exynos5440_gpio_irq_mask,
871};
872
873
874static irqreturn_t exynos5440_gpio_irq(int irq, void *data)
875{
876 struct exynos5440_gpio_intr_data *intd = data;
877 struct exynos5440_pinctrl_priv_data *d = intd->priv;
878 int virq;
879
880 virq = irq_linear_revmap(d->irq_domain, intd->gpio_int);
881 if (!virq)
882 return IRQ_NONE;
883 generic_handle_irq(virq);
884 return IRQ_HANDLED;
885}
886
887static int exynos5440_gpio_irq_map(struct irq_domain *h, unsigned int virq,
888 irq_hw_number_t hw)
889{
890 struct exynos5440_pinctrl_priv_data *d = h->host_data;
891
892 irq_set_chip_data(virq, d);
893 irq_set_chip_and_handler(virq, &exynos5440_gpio_irq_chip,
894 handle_level_irq);
895 return 0;
896}
897
898
899static const struct irq_domain_ops exynos5440_gpio_irqd_ops = {
900 .map = exynos5440_gpio_irq_map,
901 .xlate = irq_domain_xlate_twocell,
902};
903
904
905static int exynos5440_gpio_irq_init(struct platform_device *pdev,
906 struct exynos5440_pinctrl_priv_data *priv)
907{
908 struct device *dev = &pdev->dev;
909 struct exynos5440_gpio_intr_data *intd;
910 int i, irq, ret;
911
912 intd = devm_kzalloc(dev, sizeof(*intd) * EXYNOS5440_MAX_GPIO_INT,
913 GFP_KERNEL);
914 if (!intd)
915 return -ENOMEM;
916
917 for (i = 0; i < EXYNOS5440_MAX_GPIO_INT; i++) {
918 irq = irq_of_parse_and_map(dev->of_node, i);
919 if (irq <= 0) {
920 dev_err(dev, "irq parsing failed\n");
921 return -EINVAL;
922 }
923
924 intd->gpio_int = i;
925 intd->priv = priv;
926 ret = devm_request_irq(dev, irq, exynos5440_gpio_irq,
927 0, dev_name(dev), intd++);
928 if (ret) {
929 dev_err(dev, "irq request failed\n");
930 return -ENXIO;
931 }
932 }
933
934 priv->irq_domain = irq_domain_add_linear(dev->of_node,
935 EXYNOS5440_MAX_GPIO_INT,
936 &exynos5440_gpio_irqd_ops, priv);
937 if (!priv->irq_domain) {
938 dev_err(dev, "failed to create irq domain\n");
939 return -ENXIO;
940 }
941
942 return 0;
943}
944
945static int exynos5440_pinctrl_probe(struct platform_device *pdev)
946{
947 struct device *dev = &pdev->dev;
948 struct exynos5440_pinctrl_priv_data *priv;
949 struct resource *res;
950 int ret;
951
952 if (!dev->of_node) {
953 dev_err(dev, "device tree node not found\n");
954 return -ENODEV;
955 }
956
957 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
958 if (!priv)
959 return -ENOMEM;
960
961 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
962 priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
963 if (IS_ERR(priv->reg_base))
964 return PTR_ERR(priv->reg_base);
965
966 ret = exynos5440_gpiolib_register(pdev, priv);
967 if (ret)
968 return ret;
969
970 ret = exynos5440_pinctrl_register(pdev, priv);
971 if (ret) {
972 exynos5440_gpiolib_unregister(pdev, priv);
973 return ret;
974 }
975
976 ret = exynos5440_gpio_irq_init(pdev, priv);
977 if (ret) {
978 dev_err(dev, "failed to setup gpio interrupts\n");
979 return ret;
980 }
981
982 platform_set_drvdata(pdev, priv);
983 dev_info(dev, "EXYNOS5440 pinctrl driver registered\n");
984 return 0;
985}
986
987static const struct of_device_id exynos5440_pinctrl_dt_match[] = {
988 { .compatible = "samsung,exynos5440-pinctrl" },
989 {},
990};
991
992static struct platform_driver exynos5440_pinctrl_driver = {
993 .probe = exynos5440_pinctrl_probe,
994 .driver = {
995 .name = "exynos5440-pinctrl",
996 .of_match_table = exynos5440_pinctrl_dt_match,
997 .suppress_bind_attrs = true,
998 },
999};
1000
1001static int __init exynos5440_pinctrl_drv_register(void)
1002{
1003 return platform_driver_register(&exynos5440_pinctrl_driver);
1004}
1005postcore_initcall(exynos5440_pinctrl_drv_register);
1006