2 * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 * Copyright (c) 2012 Linaro Ltd
7 * http://www.linaro.org
9 * Author: Thomas Abraham <thomas.ab@samsung.com>
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.
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.
23 #include <linux/module.h>
24 #include <linux/platform_device.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>
34 #include "pinctrl-samsung.h"
36 /* list of all possible config options supported */
37 static struct pin_config
{
39 enum pincfg_type param
;
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
},
48 /* Global list of devices (struct samsung_pinctrl_drv_data) */
49 static LIST_HEAD(drvdata_list
);
51 static unsigned int pin_base
;
53 static inline struct samsung_pin_bank
*gc_to_pin_bank(struct gpio_chip
*gc
)
55 return container_of(gc
, struct samsung_pin_bank
, gpio_chip
);
58 static int samsung_get_group_count(struct pinctrl_dev
*pctldev
)
60 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
62 return pmx
->nr_groups
;
65 static const char *samsung_get_group_name(struct pinctrl_dev
*pctldev
,
68 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
70 return pmx
->pin_groups
[group
].name
;
73 static int samsung_get_group_pins(struct pinctrl_dev
*pctldev
,
75 const unsigned **pins
,
78 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
80 *pins
= pmx
->pin_groups
[group
].pins
;
81 *num_pins
= pmx
->pin_groups
[group
].num_pins
;
86 static int reserve_map(struct device
*dev
, struct pinctrl_map
**map
,
87 unsigned *reserved_maps
, unsigned *num_maps
,
90 unsigned old_num
= *reserved_maps
;
91 unsigned new_num
= *num_maps
+ reserve
;
92 struct pinctrl_map
*new_map
;
94 if (old_num
>= new_num
)
97 new_map
= krealloc(*map
, sizeof(*new_map
) * new_num
, GFP_KERNEL
);
99 dev_err(dev
, "krealloc(map) failed\n");
103 memset(new_map
+ old_num
, 0, (new_num
- old_num
) * sizeof(*new_map
));
106 *reserved_maps
= new_num
;
111 static int add_map_mux(struct pinctrl_map
**map
, unsigned *reserved_maps
,
112 unsigned *num_maps
, const char *group
,
113 const char *function
)
115 if (WARN_ON(*num_maps
== *reserved_maps
))
118 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
119 (*map
)[*num_maps
].data
.mux
.group
= group
;
120 (*map
)[*num_maps
].data
.mux
.function
= function
;
126 static int add_map_configs(struct device
*dev
, struct pinctrl_map
**map
,
127 unsigned *reserved_maps
, unsigned *num_maps
,
128 const char *group
, unsigned long *configs
,
129 unsigned num_configs
)
131 unsigned long *dup_configs
;
133 if (WARN_ON(*num_maps
== *reserved_maps
))
136 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
139 dev_err(dev
, "kmemdup(configs) failed\n");
143 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
144 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
145 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
146 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
152 static int add_config(struct device
*dev
, unsigned long **configs
,
153 unsigned *num_configs
, unsigned long config
)
155 unsigned old_num
= *num_configs
;
156 unsigned new_num
= old_num
+ 1;
157 unsigned long *new_configs
;
159 new_configs
= krealloc(*configs
, sizeof(*new_configs
) * new_num
,
162 dev_err(dev
, "krealloc(configs) failed\n");
166 new_configs
[old_num
] = config
;
168 *configs
= new_configs
;
169 *num_configs
= new_num
;
174 static void samsung_dt_free_map(struct pinctrl_dev
*pctldev
,
175 struct pinctrl_map
*map
,
180 for (i
= 0; i
< num_maps
; i
++)
181 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
182 kfree(map
[i
].data
.configs
.configs
);
187 static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data
*drvdata
,
189 struct device_node
*np
,
190 struct pinctrl_map
**map
,
191 unsigned *reserved_maps
,
196 unsigned long config
;
197 unsigned long *configs
= NULL
;
198 unsigned num_configs
= 0;
200 struct property
*prop
;
202 bool has_func
= false;
204 ret
= of_property_read_u32(np
, "samsung,pin-function", &val
);
208 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
209 ret
= of_property_read_u32(np
, cfg_params
[i
].property
, &val
);
211 config
= PINCFG_PACK(cfg_params
[i
].param
, val
);
212 ret
= add_config(dev
, &configs
, &num_configs
, config
);
215 /* EINVAL=missing, which is fine since it's optional */
216 } else if (ret
!= -EINVAL
) {
217 dev_err(dev
, "could not parse property %s\n",
218 cfg_params
[i
].property
);
227 ret
= of_property_count_strings(np
, "samsung,pins");
229 dev_err(dev
, "could not parse property samsung,pins\n");
234 ret
= reserve_map(dev
, map
, reserved_maps
, num_maps
, reserve
);
238 of_property_for_each_string(np
, "samsung,pins", prop
, group
) {
240 ret
= add_map_mux(map
, reserved_maps
,
241 num_maps
, group
, np
->full_name
);
247 ret
= add_map_configs(dev
, map
, reserved_maps
,
248 num_maps
, group
, configs
,
262 static int samsung_dt_node_to_map(struct pinctrl_dev
*pctldev
,
263 struct device_node
*np_config
,
264 struct pinctrl_map
**map
,
267 struct samsung_pinctrl_drv_data
*drvdata
;
268 unsigned reserved_maps
;
269 struct device_node
*np
;
272 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
278 if (!of_get_child_count(np_config
))
279 return samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
,
284 for_each_child_of_node(np_config
, np
) {
285 ret
= samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
, np
, map
,
286 &reserved_maps
, num_maps
);
288 samsung_dt_free_map(pctldev
, *map
, *num_maps
);
297 /* list of pinctrl callbacks for the pinctrl core */
298 static const struct pinctrl_ops samsung_pctrl_ops
= {
299 .get_groups_count
= samsung_get_group_count
,
300 .get_group_name
= samsung_get_group_name
,
301 .get_group_pins
= samsung_get_group_pins
,
302 .dt_node_to_map
= samsung_dt_node_to_map
,
303 .dt_free_map
= samsung_dt_free_map
,
306 /* check if the selector is a valid pin function selector */
307 static int samsung_get_functions_count(struct pinctrl_dev
*pctldev
)
309 struct samsung_pinctrl_drv_data
*drvdata
;
311 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
312 return drvdata
->nr_functions
;
315 /* return the name of the pin function specified */
316 static const char *samsung_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
319 struct samsung_pinctrl_drv_data
*drvdata
;
321 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
322 return drvdata
->pmx_functions
[selector
].name
;
325 /* return the groups associated for the specified function selector */
326 static int samsung_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
327 unsigned selector
, const char * const **groups
,
328 unsigned * const num_groups
)
330 struct samsung_pinctrl_drv_data
*drvdata
;
332 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
333 *groups
= drvdata
->pmx_functions
[selector
].groups
;
334 *num_groups
= drvdata
->pmx_functions
[selector
].num_groups
;
339 * given a pin number that is local to a pin controller, find out the pin bank
340 * and the register base of the pin bank.
342 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data
*drvdata
,
343 unsigned pin
, void __iomem
**reg
, u32
*offset
,
344 struct samsung_pin_bank
**bank
)
346 struct samsung_pin_bank
*b
;
348 b
= drvdata
->pin_banks
;
350 while ((pin
>= b
->pin_base
) &&
351 ((b
->pin_base
+ b
->nr_pins
- 1) < pin
))
354 *reg
= drvdata
->virt_base
+ b
->pctl_offset
;
355 *offset
= pin
- b
->pin_base
;
360 /* enable or disable a pinmux function */
361 static void samsung_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
362 unsigned group
, bool enable
)
364 struct samsung_pinctrl_drv_data
*drvdata
;
365 const struct samsung_pin_bank_type
*type
;
366 struct samsung_pin_bank
*bank
;
368 u32 mask
, shift
, data
, pin_offset
;
370 const struct samsung_pmx_func
*func
;
371 const struct samsung_pin_group
*grp
;
373 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
374 func
= &drvdata
->pmx_functions
[selector
];
375 grp
= &drvdata
->pin_groups
[group
];
377 pin_to_reg_bank(drvdata
, grp
->pins
[0] - drvdata
->pin_base
,
378 ®
, &pin_offset
, &bank
);
380 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
381 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
383 /* Some banks have two config registers */
388 spin_lock_irqsave(&bank
->slock
, flags
);
390 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
391 data
&= ~(mask
<< shift
);
393 data
|= func
->val
<< shift
;
394 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
396 spin_unlock_irqrestore(&bank
->slock
, flags
);
399 /* enable a specified pinmux by writing to registers */
400 static int samsung_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
404 samsung_pinmux_setup(pctldev
, selector
, group
, true);
408 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
409 static const struct pinmux_ops samsung_pinmux_ops
= {
410 .get_functions_count
= samsung_get_functions_count
,
411 .get_function_name
= samsung_pinmux_get_fname
,
412 .get_function_groups
= samsung_pinmux_get_groups
,
413 .set_mux
= samsung_pinmux_set_mux
,
416 /* set or get the pin config settings for a specified pin */
417 static int samsung_pinconf_rw(struct pinctrl_dev
*pctldev
, unsigned int pin
,
418 unsigned long *config
, bool set
)
420 struct samsung_pinctrl_drv_data
*drvdata
;
421 const struct samsung_pin_bank_type
*type
;
422 struct samsung_pin_bank
*bank
;
423 void __iomem
*reg_base
;
424 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
425 u32 data
, width
, pin_offset
, mask
, shift
;
426 u32 cfg_value
, cfg_reg
;
429 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
430 pin_to_reg_bank(drvdata
, pin
- drvdata
->pin_base
, ®_base
,
434 if (cfg_type
>= PINCFG_TYPE_NUM
|| !type
->fld_width
[cfg_type
])
437 width
= type
->fld_width
[cfg_type
];
438 cfg_reg
= type
->reg_offset
[cfg_type
];
440 spin_lock_irqsave(&bank
->slock
, flags
);
442 mask
= (1 << width
) - 1;
443 shift
= pin_offset
* width
;
444 data
= readl(reg_base
+ cfg_reg
);
447 cfg_value
= PINCFG_UNPACK_VALUE(*config
);
448 data
&= ~(mask
<< shift
);
449 data
|= (cfg_value
<< shift
);
450 writel(data
, reg_base
+ cfg_reg
);
454 *config
= PINCFG_PACK(cfg_type
, data
);
457 spin_unlock_irqrestore(&bank
->slock
, flags
);
462 /* set the pin config settings for a specified pin */
463 static int samsung_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
464 unsigned long *configs
, unsigned num_configs
)
468 for (i
= 0; i
< num_configs
; i
++) {
469 ret
= samsung_pinconf_rw(pctldev
, pin
, &configs
[i
], true);
472 } /* for each config */
477 /* get the pin config settings for a specified pin */
478 static int samsung_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
479 unsigned long *config
)
481 return samsung_pinconf_rw(pctldev
, pin
, config
, false);
484 /* set the pin config settings for a specified pin group */
485 static int samsung_pinconf_group_set(struct pinctrl_dev
*pctldev
,
486 unsigned group
, unsigned long *configs
,
487 unsigned num_configs
)
489 struct samsung_pinctrl_drv_data
*drvdata
;
490 const unsigned int *pins
;
493 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
494 pins
= drvdata
->pin_groups
[group
].pins
;
496 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++)
497 samsung_pinconf_set(pctldev
, pins
[cnt
], configs
, num_configs
);
502 /* get the pin config settings for a specified pin group */
503 static int samsung_pinconf_group_get(struct pinctrl_dev
*pctldev
,
504 unsigned int group
, unsigned long *config
)
506 struct samsung_pinctrl_drv_data
*drvdata
;
507 const unsigned int *pins
;
509 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
510 pins
= drvdata
->pin_groups
[group
].pins
;
511 samsung_pinconf_get(pctldev
, pins
[0], config
);
515 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
516 static const struct pinconf_ops samsung_pinconf_ops
= {
517 .pin_config_get
= samsung_pinconf_get
,
518 .pin_config_set
= samsung_pinconf_set
,
519 .pin_config_group_get
= samsung_pinconf_group_get
,
520 .pin_config_group_set
= samsung_pinconf_group_set
,
523 /* gpiolib gpio_set callback function */
524 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
526 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
527 const struct samsung_pin_bank_type
*type
= bank
->type
;
532 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
534 spin_lock_irqsave(&bank
->slock
, flags
);
536 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
537 data
&= ~(1 << offset
);
540 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
542 spin_unlock_irqrestore(&bank
->slock
, flags
);
545 /* gpiolib gpio_get callback function */
546 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
550 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
551 const struct samsung_pin_bank_type
*type
= bank
->type
;
553 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
555 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
562 * The calls to gpio_direction_output() and gpio_direction_input()
563 * leads to this function call.
565 static int samsung_gpio_set_direction(struct gpio_chip
*gc
,
566 unsigned offset
, bool input
)
568 const struct samsung_pin_bank_type
*type
;
569 struct samsung_pin_bank
*bank
;
570 struct samsung_pinctrl_drv_data
*drvdata
;
572 u32 data
, mask
, shift
;
575 bank
= gc_to_pin_bank(gc
);
577 drvdata
= bank
->drvdata
;
579 reg
= drvdata
->virt_base
+ bank
->pctl_offset
+
580 type
->reg_offset
[PINCFG_TYPE_FUNC
];
582 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
583 shift
= offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
585 /* Some banks have two config registers */
590 spin_lock_irqsave(&bank
->slock
, flags
);
593 data
&= ~(mask
<< shift
);
595 data
|= FUNC_OUTPUT
<< shift
;
598 spin_unlock_irqrestore(&bank
->slock
, flags
);
603 /* gpiolib gpio_direction_input callback function. */
604 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
606 return samsung_gpio_set_direction(gc
, offset
, true);
609 /* gpiolib gpio_direction_output callback function. */
610 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
613 samsung_gpio_set(gc
, offset
, value
);
614 return samsung_gpio_set_direction(gc
, offset
, false);
618 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
619 * and a virtual IRQ, if not already present.
621 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
623 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
626 if (!bank
->irq_domain
)
629 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
631 return (virq
) ? : -ENXIO
;
634 static struct samsung_pin_group
*samsung_pinctrl_create_groups(
636 struct samsung_pinctrl_drv_data
*drvdata
,
639 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
640 struct samsung_pin_group
*groups
, *grp
;
641 const struct pinctrl_pin_desc
*pdesc
;
644 groups
= devm_kzalloc(dev
, ctrldesc
->npins
* sizeof(*groups
),
647 return ERR_PTR(-EINVAL
);
650 pdesc
= ctrldesc
->pins
;
651 for (i
= 0; i
< ctrldesc
->npins
; ++i
, ++pdesc
, ++grp
) {
652 grp
->name
= pdesc
->name
;
653 grp
->pins
= &pdesc
->number
;
657 *cnt
= ctrldesc
->npins
;
661 static int samsung_pinctrl_create_function(struct device
*dev
,
662 struct samsung_pinctrl_drv_data
*drvdata
,
663 struct device_node
*func_np
,
664 struct samsung_pmx_func
*func
)
670 if (of_property_read_u32(func_np
, "samsung,pin-function", &func
->val
))
673 npins
= of_property_count_strings(func_np
, "samsung,pins");
675 dev_err(dev
, "invalid pin list in %s node", func_np
->name
);
679 func
->name
= func_np
->full_name
;
681 func
->groups
= devm_kzalloc(dev
, npins
* sizeof(char *), GFP_KERNEL
);
685 for (i
= 0; i
< npins
; ++i
) {
688 ret
= of_property_read_string_index(func_np
, "samsung,pins",
692 "failed to read pin name %d from %s node\n",
697 func
->groups
[i
] = gname
;
700 func
->num_groups
= npins
;
704 static struct samsung_pmx_func
*samsung_pinctrl_create_functions(
706 struct samsung_pinctrl_drv_data
*drvdata
,
709 struct samsung_pmx_func
*functions
, *func
;
710 struct device_node
*dev_np
= dev
->of_node
;
711 struct device_node
*cfg_np
;
712 unsigned int func_cnt
= 0;
716 * Iterate over all the child nodes of the pin controller node
717 * and create pin groups and pin function lists.
719 for_each_child_of_node(dev_np
, cfg_np
) {
720 struct device_node
*func_np
;
722 if (!of_get_child_count(cfg_np
)) {
723 if (!of_find_property(cfg_np
,
724 "samsung,pin-function", NULL
))
730 for_each_child_of_node(cfg_np
, func_np
) {
731 if (!of_find_property(func_np
,
732 "samsung,pin-function", NULL
))
738 functions
= devm_kzalloc(dev
, func_cnt
* sizeof(*functions
),
741 dev_err(dev
, "failed to allocate memory for function list\n");
742 return ERR_PTR(-EINVAL
);
747 * Iterate over all the child nodes of the pin controller node
748 * and create pin groups and pin function lists.
751 for_each_child_of_node(dev_np
, cfg_np
) {
752 struct device_node
*func_np
;
754 if (!of_get_child_count(cfg_np
)) {
755 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
768 for_each_child_of_node(cfg_np
, func_np
) {
769 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
772 of_node_put(func_np
);
788 * Parse the information about all the available pin groups and pin functions
789 * from device node of the pin-controller. A pin group is formed with all
790 * the pins listed in the "samsung,pins" property.
793 static int samsung_pinctrl_parse_dt(struct platform_device
*pdev
,
794 struct samsung_pinctrl_drv_data
*drvdata
)
796 struct device
*dev
= &pdev
->dev
;
797 struct samsung_pin_group
*groups
;
798 struct samsung_pmx_func
*functions
;
799 unsigned int grp_cnt
= 0, func_cnt
= 0;
801 groups
= samsung_pinctrl_create_groups(dev
, drvdata
, &grp_cnt
);
802 if (IS_ERR(groups
)) {
803 dev_err(dev
, "failed to parse pin groups\n");
804 return PTR_ERR(groups
);
807 functions
= samsung_pinctrl_create_functions(dev
, drvdata
, &func_cnt
);
808 if (IS_ERR(functions
)) {
809 dev_err(dev
, "failed to parse pin functions\n");
810 return PTR_ERR(functions
);
813 drvdata
->pin_groups
= groups
;
814 drvdata
->nr_groups
= grp_cnt
;
815 drvdata
->pmx_functions
= functions
;
816 drvdata
->nr_functions
= func_cnt
;
821 /* register the pinctrl interface with the pinctrl subsystem */
822 static int samsung_pinctrl_register(struct platform_device
*pdev
,
823 struct samsung_pinctrl_drv_data
*drvdata
)
825 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
826 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
827 struct samsung_pin_bank
*pin_bank
;
831 ctrldesc
->name
= "samsung-pinctrl";
832 ctrldesc
->owner
= THIS_MODULE
;
833 ctrldesc
->pctlops
= &samsung_pctrl_ops
;
834 ctrldesc
->pmxops
= &samsung_pinmux_ops
;
835 ctrldesc
->confops
= &samsung_pinconf_ops
;
837 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
838 drvdata
->nr_pins
, GFP_KERNEL
);
840 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
843 ctrldesc
->pins
= pindesc
;
844 ctrldesc
->npins
= drvdata
->nr_pins
;
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
;
851 * allocate space for storing the dynamically generated names for all
852 * the pins which belong to this pin-controller.
854 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
855 drvdata
->nr_pins
, GFP_KERNEL
);
857 dev_err(&pdev
->dev
, "mem alloc for pin names failed\n");
861 /* for each pin, the name of the pin is pin-bank name + pin number */
862 for (bank
= 0; bank
< drvdata
->nr_banks
; bank
++) {
863 pin_bank
= &drvdata
->pin_banks
[bank
];
864 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
865 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
866 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
867 pdesc
->name
= pin_names
;
868 pin_names
+= PIN_NAME_LENGTH
;
872 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
876 drvdata
->pctl_dev
= pinctrl_register(ctrldesc
, &pdev
->dev
, drvdata
);
877 if (IS_ERR(drvdata
->pctl_dev
)) {
878 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
879 return PTR_ERR(drvdata
->pctl_dev
);
882 for (bank
= 0; bank
< drvdata
->nr_banks
; ++bank
) {
883 pin_bank
= &drvdata
->pin_banks
[bank
];
884 pin_bank
->grange
.name
= pin_bank
->name
;
885 pin_bank
->grange
.id
= bank
;
886 pin_bank
->grange
.pin_base
= drvdata
->pin_base
887 + pin_bank
->pin_base
;
888 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
889 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
890 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
891 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
897 static const struct gpio_chip samsung_gpiolib_chip
= {
898 .request
= gpiochip_generic_request
,
899 .free
= gpiochip_generic_free
,
900 .set
= samsung_gpio_set
,
901 .get
= samsung_gpio_get
,
902 .direction_input
= samsung_gpio_direction_input
,
903 .direction_output
= samsung_gpio_direction_output
,
904 .to_irq
= samsung_gpio_to_irq
,
905 .owner
= THIS_MODULE
,
908 /* register the gpiolib interface with the gpiolib subsystem */
909 static int samsung_gpiolib_register(struct platform_device
*pdev
,
910 struct samsung_pinctrl_drv_data
*drvdata
)
912 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
913 struct gpio_chip
*gc
;
917 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
) {
918 bank
->gpio_chip
= samsung_gpiolib_chip
;
920 gc
= &bank
->gpio_chip
;
921 gc
->base
= drvdata
->pin_base
+ bank
->pin_base
;
922 gc
->ngpio
= bank
->nr_pins
;
923 gc
->dev
= &pdev
->dev
;
924 gc
->of_node
= bank
->of_node
;
925 gc
->label
= bank
->name
;
927 ret
= gpiochip_add(gc
);
929 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
938 for (--i
, --bank
; i
>= 0; --i
, --bank
)
939 gpiochip_remove(&bank
->gpio_chip
);
943 /* unregister the gpiolib interface with the gpiolib subsystem */
944 static int samsung_gpiolib_unregister(struct platform_device
*pdev
,
945 struct samsung_pinctrl_drv_data
*drvdata
)
947 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
950 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
951 gpiochip_remove(&bank
->gpio_chip
);
956 static const struct of_device_id samsung_pinctrl_dt_match
[];
958 /* retrieve the soc specific data */
959 static const struct samsung_pin_ctrl
*
960 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data
*d
,
961 struct platform_device
*pdev
)
964 const struct of_device_id
*match
;
965 struct device_node
*node
= pdev
->dev
.of_node
;
966 struct device_node
*np
;
967 const struct samsung_pin_bank_data
*bdata
;
968 const struct samsung_pin_ctrl
*ctrl
;
969 struct samsung_pin_bank
*bank
;
972 id
= of_alias_get_id(node
, "pinctrl");
974 dev_err(&pdev
->dev
, "failed to get alias id\n");
975 return ERR_PTR(-ENOENT
);
977 match
= of_match_node(samsung_pinctrl_dt_match
, node
);
978 ctrl
= (struct samsung_pin_ctrl
*)match
->data
+ id
;
980 d
->suspend
= ctrl
->suspend
;
981 d
->resume
= ctrl
->resume
;
982 d
->nr_banks
= ctrl
->nr_banks
;
983 d
->pin_banks
= devm_kcalloc(&pdev
->dev
, d
->nr_banks
,
984 sizeof(*d
->pin_banks
), GFP_KERNEL
);
986 return ERR_PTR(-ENOMEM
);
989 bdata
= ctrl
->pin_banks
;
990 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bdata
, ++bank
) {
991 bank
->type
= bdata
->type
;
992 bank
->pctl_offset
= bdata
->pctl_offset
;
993 bank
->nr_pins
= bdata
->nr_pins
;
994 bank
->eint_func
= bdata
->eint_func
;
995 bank
->eint_type
= bdata
->eint_type
;
996 bank
->eint_mask
= bdata
->eint_mask
;
997 bank
->eint_offset
= bdata
->eint_offset
;
998 bank
->name
= bdata
->name
;
1000 spin_lock_init(&bank
->slock
);
1002 bank
->pin_base
= d
->nr_pins
;
1003 d
->nr_pins
+= bank
->nr_pins
;
1006 for_each_child_of_node(node
, np
) {
1007 if (!of_find_property(np
, "gpio-controller", NULL
))
1009 bank
= d
->pin_banks
;
1010 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
1011 if (!strcmp(bank
->name
, np
->name
)) {
1018 d
->pin_base
= pin_base
;
1019 pin_base
+= d
->nr_pins
;
1024 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
1026 struct samsung_pinctrl_drv_data
*drvdata
;
1027 const struct samsung_pin_ctrl
*ctrl
;
1028 struct device
*dev
= &pdev
->dev
;
1029 struct resource
*res
;
1032 if (!dev
->of_node
) {
1033 dev_err(dev
, "device tree node not found\n");
1037 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
1039 dev_err(dev
, "failed to allocate memory for driver's "
1044 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
1046 dev_err(&pdev
->dev
, "driver data not available\n");
1047 return PTR_ERR(ctrl
);
1051 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1052 drvdata
->virt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1053 if (IS_ERR(drvdata
->virt_base
))
1054 return PTR_ERR(drvdata
->virt_base
);
1056 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1058 drvdata
->irq
= res
->start
;
1060 ret
= samsung_gpiolib_register(pdev
, drvdata
);
1064 ret
= samsung_pinctrl_register(pdev
, drvdata
);
1066 samsung_gpiolib_unregister(pdev
, drvdata
);
1070 if (ctrl
->eint_gpio_init
)
1071 ctrl
->eint_gpio_init(drvdata
);
1072 if (ctrl
->eint_wkup_init
)
1073 ctrl
->eint_wkup_init(drvdata
);
1075 platform_set_drvdata(pdev
, drvdata
);
1077 /* Add to the global list */
1078 list_add_tail(&drvdata
->node
, &drvdata_list
);
1086 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device
1088 * Save data for all banks handled by this device.
1090 static void samsung_pinctrl_suspend_dev(
1091 struct samsung_pinctrl_drv_data
*drvdata
)
1093 void __iomem
*virt_base
= drvdata
->virt_base
;
1096 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1097 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1098 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1099 const u8
*offs
= bank
->type
->reg_offset
;
1100 const u8
*widths
= bank
->type
->fld_width
;
1101 enum pincfg_type type
;
1103 /* Registers without a powerdown config aren't lost */
1104 if (!widths
[PINCFG_TYPE_CON_PDN
])
1107 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1109 bank
->pm_save
[type
] = readl(reg
+ offs
[type
]);
1111 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1112 /* Some banks have two config registers */
1113 bank
->pm_save
[PINCFG_TYPE_NUM
] =
1114 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1115 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1117 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1118 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1120 pr_debug("Save %s @ %p (con %#010x)\n", bank
->name
,
1121 reg
, bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1125 if (drvdata
->suspend
)
1126 drvdata
->suspend(drvdata
);
1130 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device
1132 * Restore one of the banks that was saved during suspend.
1134 * We don't bother doing anything complicated to avoid glitching lines since
1135 * we're called before pad retention is turned off.
1137 static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data
*drvdata
)
1139 void __iomem
*virt_base
= drvdata
->virt_base
;
1142 if (drvdata
->resume
)
1143 drvdata
->resume(drvdata
);
1145 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1146 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1147 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1148 const u8
*offs
= bank
->type
->reg_offset
;
1149 const u8
*widths
= bank
->type
->fld_width
;
1150 enum pincfg_type type
;
1152 /* Registers without a powerdown config aren't lost */
1153 if (!widths
[PINCFG_TYPE_CON_PDN
])
1156 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1157 /* Some banks have two config registers */
1158 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1160 readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1161 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4),
1162 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1163 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1164 writel(bank
->pm_save
[PINCFG_TYPE_NUM
],
1165 reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1167 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank
->name
,
1168 reg
, readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1169 bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1171 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1173 writel(bank
->pm_save
[type
], reg
+ offs
[type
]);
1178 * samsung_pinctrl_suspend - save pinctrl state for suspend
1180 * Save data for all banks across all devices.
1182 static int samsung_pinctrl_suspend(void)
1184 struct samsung_pinctrl_drv_data
*drvdata
;
1186 list_for_each_entry(drvdata
, &drvdata_list
, node
) {
1187 samsung_pinctrl_suspend_dev(drvdata
);
1194 * samsung_pinctrl_resume - restore pinctrl state for suspend
1196 * Restore data for all banks across all devices.
1198 static void samsung_pinctrl_resume(void)
1200 struct samsung_pinctrl_drv_data
*drvdata
;
1202 list_for_each_entry_reverse(drvdata
, &drvdata_list
, node
) {
1203 samsung_pinctrl_resume_dev(drvdata
);
1208 #define samsung_pinctrl_suspend NULL
1209 #define samsung_pinctrl_resume NULL
1212 static struct syscore_ops samsung_pinctrl_syscore_ops
= {
1213 .suspend
= samsung_pinctrl_suspend
,
1214 .resume
= samsung_pinctrl_resume
,
1217 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
1218 #ifdef CONFIG_PINCTRL_EXYNOS
1219 { .compatible
= "samsung,exynos3250-pinctrl",
1220 .data
= (void *)exynos3250_pin_ctrl
},
1221 { .compatible
= "samsung,exynos4210-pinctrl",
1222 .data
= (void *)exynos4210_pin_ctrl
},
1223 { .compatible
= "samsung,exynos4x12-pinctrl",
1224 .data
= (void *)exynos4x12_pin_ctrl
},
1225 { .compatible
= "samsung,exynos4415-pinctrl",
1226 .data
= (void *)exynos4415_pin_ctrl
},
1227 { .compatible
= "samsung,exynos5250-pinctrl",
1228 .data
= (void *)exynos5250_pin_ctrl
},
1229 { .compatible
= "samsung,exynos5260-pinctrl",
1230 .data
= (void *)exynos5260_pin_ctrl
},
1231 { .compatible
= "samsung,exynos5420-pinctrl",
1232 .data
= (void *)exynos5420_pin_ctrl
},
1233 { .compatible
= "samsung,exynos5433-pinctrl",
1234 .data
= (void *)exynos5433_pin_ctrl
},
1235 { .compatible
= "samsung,s5pv210-pinctrl",
1236 .data
= (void *)s5pv210_pin_ctrl
},
1237 { .compatible
= "samsung,exynos7-pinctrl",
1238 .data
= (void *)exynos7_pin_ctrl
},
1240 #ifdef CONFIG_PINCTRL_S3C64XX
1241 { .compatible
= "samsung,s3c64xx-pinctrl",
1242 .data
= s3c64xx_pin_ctrl
},
1244 #ifdef CONFIG_PINCTRL_S3C24XX
1245 { .compatible
= "samsung,s3c2412-pinctrl",
1246 .data
= s3c2412_pin_ctrl
},
1247 { .compatible
= "samsung,s3c2416-pinctrl",
1248 .data
= s3c2416_pin_ctrl
},
1249 { .compatible
= "samsung,s3c2440-pinctrl",
1250 .data
= s3c2440_pin_ctrl
},
1251 { .compatible
= "samsung,s3c2450-pinctrl",
1252 .data
= s3c2450_pin_ctrl
},
1256 MODULE_DEVICE_TABLE(of
, samsung_pinctrl_dt_match
);
1258 static struct platform_driver samsung_pinctrl_driver
= {
1259 .probe
= samsung_pinctrl_probe
,
1261 .name
= "samsung-pinctrl",
1262 .of_match_table
= samsung_pinctrl_dt_match
,
1266 static int __init
samsung_pinctrl_drv_register(void)
1269 * Register syscore ops for save/restore of registers across suspend.
1270 * It's important to ensure that this driver is running at an earlier
1271 * initcall level than any arch-specific init calls that install syscore
1272 * ops that turn off pad retention (like exynos_pm_resume).
1274 register_syscore_ops(&samsung_pinctrl_syscore_ops
);
1276 return platform_driver_register(&samsung_pinctrl_driver
);
1278 postcore_initcall(samsung_pinctrl_drv_register
);
1280 static void __exit
samsung_pinctrl_drv_unregister(void)
1282 platform_driver_unregister(&samsung_pinctrl_driver
);
1284 module_exit(samsung_pinctrl_drv_unregister
);
1286 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1287 MODULE_DESCRIPTION("Samsung pinctrl driver");
1288 MODULE_LICENSE("GPL v2");