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
);
296 /* list of pinctrl callbacks for the pinctrl core */
297 static const struct pinctrl_ops samsung_pctrl_ops
= {
298 .get_groups_count
= samsung_get_group_count
,
299 .get_group_name
= samsung_get_group_name
,
300 .get_group_pins
= samsung_get_group_pins
,
301 .dt_node_to_map
= samsung_dt_node_to_map
,
302 .dt_free_map
= samsung_dt_free_map
,
305 /* check if the selector is a valid pin function selector */
306 static int samsung_get_functions_count(struct pinctrl_dev
*pctldev
)
308 struct samsung_pinctrl_drv_data
*drvdata
;
310 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
311 return drvdata
->nr_functions
;
314 /* return the name of the pin function specified */
315 static const char *samsung_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
318 struct samsung_pinctrl_drv_data
*drvdata
;
320 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
321 return drvdata
->pmx_functions
[selector
].name
;
324 /* return the groups associated for the specified function selector */
325 static int samsung_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
326 unsigned selector
, const char * const **groups
,
327 unsigned * const num_groups
)
329 struct samsung_pinctrl_drv_data
*drvdata
;
331 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
332 *groups
= drvdata
->pmx_functions
[selector
].groups
;
333 *num_groups
= drvdata
->pmx_functions
[selector
].num_groups
;
338 * given a pin number that is local to a pin controller, find out the pin bank
339 * and the register base of the pin bank.
341 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data
*drvdata
,
342 unsigned pin
, void __iomem
**reg
, u32
*offset
,
343 struct samsung_pin_bank
**bank
)
345 struct samsung_pin_bank
*b
;
347 b
= drvdata
->pin_banks
;
349 while ((pin
>= b
->pin_base
) &&
350 ((b
->pin_base
+ b
->nr_pins
- 1) < pin
))
353 *reg
= drvdata
->virt_base
+ b
->pctl_offset
;
354 *offset
= pin
- b
->pin_base
;
359 /* enable or disable a pinmux function */
360 static void samsung_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
361 unsigned group
, bool enable
)
363 struct samsung_pinctrl_drv_data
*drvdata
;
364 const struct samsung_pin_bank_type
*type
;
365 struct samsung_pin_bank
*bank
;
367 u32 mask
, shift
, data
, pin_offset
;
369 const struct samsung_pmx_func
*func
;
370 const struct samsung_pin_group
*grp
;
372 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
373 func
= &drvdata
->pmx_functions
[selector
];
374 grp
= &drvdata
->pin_groups
[group
];
376 pin_to_reg_bank(drvdata
, grp
->pins
[0] - drvdata
->pin_base
,
377 ®
, &pin_offset
, &bank
);
379 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
380 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
382 /* Some banks have two config registers */
387 spin_lock_irqsave(&bank
->slock
, flags
);
389 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
390 data
&= ~(mask
<< shift
);
392 data
|= func
->val
<< shift
;
393 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
395 spin_unlock_irqrestore(&bank
->slock
, flags
);
398 /* enable a specified pinmux by writing to registers */
399 static int samsung_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
403 samsung_pinmux_setup(pctldev
, selector
, group
, true);
407 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
408 static const struct pinmux_ops samsung_pinmux_ops
= {
409 .get_functions_count
= samsung_get_functions_count
,
410 .get_function_name
= samsung_pinmux_get_fname
,
411 .get_function_groups
= samsung_pinmux_get_groups
,
412 .set_mux
= samsung_pinmux_set_mux
,
415 /* set or get the pin config settings for a specified pin */
416 static int samsung_pinconf_rw(struct pinctrl_dev
*pctldev
, unsigned int pin
,
417 unsigned long *config
, bool set
)
419 struct samsung_pinctrl_drv_data
*drvdata
;
420 const struct samsung_pin_bank_type
*type
;
421 struct samsung_pin_bank
*bank
;
422 void __iomem
*reg_base
;
423 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
424 u32 data
, width
, pin_offset
, mask
, shift
;
425 u32 cfg_value
, cfg_reg
;
428 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
429 pin_to_reg_bank(drvdata
, pin
- drvdata
->pin_base
, ®_base
,
433 if (cfg_type
>= PINCFG_TYPE_NUM
|| !type
->fld_width
[cfg_type
])
436 width
= type
->fld_width
[cfg_type
];
437 cfg_reg
= type
->reg_offset
[cfg_type
];
439 spin_lock_irqsave(&bank
->slock
, flags
);
441 mask
= (1 << width
) - 1;
442 shift
= pin_offset
* width
;
443 data
= readl(reg_base
+ cfg_reg
);
446 cfg_value
= PINCFG_UNPACK_VALUE(*config
);
447 data
&= ~(mask
<< shift
);
448 data
|= (cfg_value
<< shift
);
449 writel(data
, reg_base
+ cfg_reg
);
453 *config
= PINCFG_PACK(cfg_type
, data
);
456 spin_unlock_irqrestore(&bank
->slock
, flags
);
461 /* set the pin config settings for a specified pin */
462 static int samsung_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
463 unsigned long *configs
, unsigned num_configs
)
467 for (i
= 0; i
< num_configs
; i
++) {
468 ret
= samsung_pinconf_rw(pctldev
, pin
, &configs
[i
], true);
471 } /* for each config */
476 /* get the pin config settings for a specified pin */
477 static int samsung_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
478 unsigned long *config
)
480 return samsung_pinconf_rw(pctldev
, pin
, config
, false);
483 /* set the pin config settings for a specified pin group */
484 static int samsung_pinconf_group_set(struct pinctrl_dev
*pctldev
,
485 unsigned group
, unsigned long *configs
,
486 unsigned num_configs
)
488 struct samsung_pinctrl_drv_data
*drvdata
;
489 const unsigned int *pins
;
492 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
493 pins
= drvdata
->pin_groups
[group
].pins
;
495 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++)
496 samsung_pinconf_set(pctldev
, pins
[cnt
], configs
, num_configs
);
501 /* get the pin config settings for a specified pin group */
502 static int samsung_pinconf_group_get(struct pinctrl_dev
*pctldev
,
503 unsigned int group
, unsigned long *config
)
505 struct samsung_pinctrl_drv_data
*drvdata
;
506 const unsigned int *pins
;
508 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
509 pins
= drvdata
->pin_groups
[group
].pins
;
510 samsung_pinconf_get(pctldev
, pins
[0], config
);
514 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
515 static const struct pinconf_ops samsung_pinconf_ops
= {
516 .pin_config_get
= samsung_pinconf_get
,
517 .pin_config_set
= samsung_pinconf_set
,
518 .pin_config_group_get
= samsung_pinconf_group_get
,
519 .pin_config_group_set
= samsung_pinconf_group_set
,
522 /* gpiolib gpio_set callback function */
523 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
525 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
526 const struct samsung_pin_bank_type
*type
= bank
->type
;
531 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
533 spin_lock_irqsave(&bank
->slock
, flags
);
535 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
536 data
&= ~(1 << offset
);
539 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
541 spin_unlock_irqrestore(&bank
->slock
, flags
);
544 /* gpiolib gpio_get callback function */
545 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
549 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
550 const struct samsung_pin_bank_type
*type
= bank
->type
;
552 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
554 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
561 * The calls to gpio_direction_output() and gpio_direction_input()
562 * leads to this function call.
564 static int samsung_gpio_set_direction(struct gpio_chip
*gc
,
565 unsigned offset
, bool input
)
567 const struct samsung_pin_bank_type
*type
;
568 struct samsung_pin_bank
*bank
;
569 struct samsung_pinctrl_drv_data
*drvdata
;
571 u32 data
, mask
, shift
;
574 bank
= gc_to_pin_bank(gc
);
576 drvdata
= bank
->drvdata
;
578 reg
= drvdata
->virt_base
+ bank
->pctl_offset
+
579 type
->reg_offset
[PINCFG_TYPE_FUNC
];
581 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
582 shift
= offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
584 /* Some banks have two config registers */
589 spin_lock_irqsave(&bank
->slock
, flags
);
592 data
&= ~(mask
<< shift
);
594 data
|= FUNC_OUTPUT
<< shift
;
597 spin_unlock_irqrestore(&bank
->slock
, flags
);
602 /* gpiolib gpio_direction_input callback function. */
603 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
605 return samsung_gpio_set_direction(gc
, offset
, true);
608 /* gpiolib gpio_direction_output callback function. */
609 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
612 samsung_gpio_set(gc
, offset
, value
);
613 return samsung_gpio_set_direction(gc
, offset
, false);
617 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
618 * and a virtual IRQ, if not already present.
620 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
622 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
625 if (!bank
->irq_domain
)
628 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
630 return (virq
) ? : -ENXIO
;
633 static struct samsung_pin_group
*samsung_pinctrl_create_groups(
635 struct samsung_pinctrl_drv_data
*drvdata
,
638 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
639 struct samsung_pin_group
*groups
, *grp
;
640 const struct pinctrl_pin_desc
*pdesc
;
643 groups
= devm_kzalloc(dev
, ctrldesc
->npins
* sizeof(*groups
),
646 return ERR_PTR(-EINVAL
);
649 pdesc
= ctrldesc
->pins
;
650 for (i
= 0; i
< ctrldesc
->npins
; ++i
, ++pdesc
, ++grp
) {
651 grp
->name
= pdesc
->name
;
652 grp
->pins
= &pdesc
->number
;
656 *cnt
= ctrldesc
->npins
;
660 static int samsung_pinctrl_create_function(struct device
*dev
,
661 struct samsung_pinctrl_drv_data
*drvdata
,
662 struct device_node
*func_np
,
663 struct samsung_pmx_func
*func
)
669 if (of_property_read_u32(func_np
, "samsung,pin-function", &func
->val
))
672 npins
= of_property_count_strings(func_np
, "samsung,pins");
674 dev_err(dev
, "invalid pin list in %s node", func_np
->name
);
678 func
->name
= func_np
->full_name
;
680 func
->groups
= devm_kzalloc(dev
, npins
* sizeof(char *), GFP_KERNEL
);
684 for (i
= 0; i
< npins
; ++i
) {
687 ret
= of_property_read_string_index(func_np
, "samsung,pins",
691 "failed to read pin name %d from %s node\n",
696 func
->groups
[i
] = gname
;
699 func
->num_groups
= npins
;
703 static struct samsung_pmx_func
*samsung_pinctrl_create_functions(
705 struct samsung_pinctrl_drv_data
*drvdata
,
708 struct samsung_pmx_func
*functions
, *func
;
709 struct device_node
*dev_np
= dev
->of_node
;
710 struct device_node
*cfg_np
;
711 unsigned int func_cnt
= 0;
715 * Iterate over all the child nodes of the pin controller node
716 * and create pin groups and pin function lists.
718 for_each_child_of_node(dev_np
, cfg_np
) {
719 struct device_node
*func_np
;
721 if (!of_get_child_count(cfg_np
)) {
722 if (!of_find_property(cfg_np
,
723 "samsung,pin-function", NULL
))
729 for_each_child_of_node(cfg_np
, func_np
) {
730 if (!of_find_property(func_np
,
731 "samsung,pin-function", NULL
))
737 functions
= devm_kzalloc(dev
, func_cnt
* sizeof(*functions
),
740 dev_err(dev
, "failed to allocate memory for function list\n");
741 return ERR_PTR(-EINVAL
);
746 * Iterate over all the child nodes of the pin controller node
747 * and create pin groups and pin function lists.
750 for_each_child_of_node(dev_np
, cfg_np
) {
751 struct device_node
*func_np
;
753 if (!of_get_child_count(cfg_np
)) {
754 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
765 for_each_child_of_node(cfg_np
, func_np
) {
766 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
782 * Parse the information about all the available pin groups and pin functions
783 * from device node of the pin-controller. A pin group is formed with all
784 * the pins listed in the "samsung,pins" property.
787 static int samsung_pinctrl_parse_dt(struct platform_device
*pdev
,
788 struct samsung_pinctrl_drv_data
*drvdata
)
790 struct device
*dev
= &pdev
->dev
;
791 struct samsung_pin_group
*groups
;
792 struct samsung_pmx_func
*functions
;
793 unsigned int grp_cnt
= 0, func_cnt
= 0;
795 groups
= samsung_pinctrl_create_groups(dev
, drvdata
, &grp_cnt
);
796 if (IS_ERR(groups
)) {
797 dev_err(dev
, "failed to parse pin groups\n");
798 return PTR_ERR(groups
);
801 functions
= samsung_pinctrl_create_functions(dev
, drvdata
, &func_cnt
);
802 if (IS_ERR(functions
)) {
803 dev_err(dev
, "failed to parse pin functions\n");
804 return PTR_ERR(functions
);
807 drvdata
->pin_groups
= groups
;
808 drvdata
->nr_groups
= grp_cnt
;
809 drvdata
->pmx_functions
= functions
;
810 drvdata
->nr_functions
= func_cnt
;
815 /* register the pinctrl interface with the pinctrl subsystem */
816 static int samsung_pinctrl_register(struct platform_device
*pdev
,
817 struct samsung_pinctrl_drv_data
*drvdata
)
819 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
820 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
821 struct samsung_pin_bank
*pin_bank
;
825 ctrldesc
->name
= "samsung-pinctrl";
826 ctrldesc
->owner
= THIS_MODULE
;
827 ctrldesc
->pctlops
= &samsung_pctrl_ops
;
828 ctrldesc
->pmxops
= &samsung_pinmux_ops
;
829 ctrldesc
->confops
= &samsung_pinconf_ops
;
831 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
832 drvdata
->nr_pins
, GFP_KERNEL
);
834 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
837 ctrldesc
->pins
= pindesc
;
838 ctrldesc
->npins
= drvdata
->nr_pins
;
840 /* dynamically populate the pin number and pin name for pindesc */
841 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
842 pdesc
->number
= pin
+ drvdata
->pin_base
;
845 * allocate space for storing the dynamically generated names for all
846 * the pins which belong to this pin-controller.
848 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
849 drvdata
->nr_pins
, GFP_KERNEL
);
851 dev_err(&pdev
->dev
, "mem alloc for pin names failed\n");
855 /* for each pin, the name of the pin is pin-bank name + pin number */
856 for (bank
= 0; bank
< drvdata
->nr_banks
; bank
++) {
857 pin_bank
= &drvdata
->pin_banks
[bank
];
858 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
859 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
860 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
861 pdesc
->name
= pin_names
;
862 pin_names
+= PIN_NAME_LENGTH
;
866 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
870 drvdata
->pctl_dev
= pinctrl_register(ctrldesc
, &pdev
->dev
, drvdata
);
871 if (IS_ERR(drvdata
->pctl_dev
)) {
872 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
873 return PTR_ERR(drvdata
->pctl_dev
);
876 for (bank
= 0; bank
< drvdata
->nr_banks
; ++bank
) {
877 pin_bank
= &drvdata
->pin_banks
[bank
];
878 pin_bank
->grange
.name
= pin_bank
->name
;
879 pin_bank
->grange
.id
= bank
;
880 pin_bank
->grange
.pin_base
= drvdata
->pin_base
881 + pin_bank
->pin_base
;
882 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
883 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
884 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
885 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
891 static const struct gpio_chip samsung_gpiolib_chip
= {
892 .request
= gpiochip_generic_request
,
893 .free
= gpiochip_generic_free
,
894 .set
= samsung_gpio_set
,
895 .get
= samsung_gpio_get
,
896 .direction_input
= samsung_gpio_direction_input
,
897 .direction_output
= samsung_gpio_direction_output
,
898 .to_irq
= samsung_gpio_to_irq
,
899 .owner
= THIS_MODULE
,
902 /* register the gpiolib interface with the gpiolib subsystem */
903 static int samsung_gpiolib_register(struct platform_device
*pdev
,
904 struct samsung_pinctrl_drv_data
*drvdata
)
906 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
907 struct gpio_chip
*gc
;
911 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
) {
912 bank
->gpio_chip
= samsung_gpiolib_chip
;
914 gc
= &bank
->gpio_chip
;
915 gc
->base
= drvdata
->pin_base
+ bank
->pin_base
;
916 gc
->ngpio
= bank
->nr_pins
;
917 gc
->dev
= &pdev
->dev
;
918 gc
->of_node
= bank
->of_node
;
919 gc
->label
= bank
->name
;
921 ret
= gpiochip_add(gc
);
923 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
932 for (--i
, --bank
; i
>= 0; --i
, --bank
)
933 gpiochip_remove(&bank
->gpio_chip
);
937 /* unregister the gpiolib interface with the gpiolib subsystem */
938 static int samsung_gpiolib_unregister(struct platform_device
*pdev
,
939 struct samsung_pinctrl_drv_data
*drvdata
)
941 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
944 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
945 gpiochip_remove(&bank
->gpio_chip
);
950 static const struct of_device_id samsung_pinctrl_dt_match
[];
952 /* retrieve the soc specific data */
953 static const struct samsung_pin_ctrl
*
954 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data
*d
,
955 struct platform_device
*pdev
)
958 const struct of_device_id
*match
;
959 struct device_node
*node
= pdev
->dev
.of_node
;
960 struct device_node
*np
;
961 const struct samsung_pin_bank_data
*bdata
;
962 const struct samsung_pin_ctrl
*ctrl
;
963 struct samsung_pin_bank
*bank
;
966 id
= of_alias_get_id(node
, "pinctrl");
968 dev_err(&pdev
->dev
, "failed to get alias id\n");
969 return ERR_PTR(-ENOENT
);
971 match
= of_match_node(samsung_pinctrl_dt_match
, node
);
972 ctrl
= (struct samsung_pin_ctrl
*)match
->data
+ id
;
974 d
->suspend
= ctrl
->suspend
;
975 d
->resume
= ctrl
->resume
;
976 d
->nr_banks
= ctrl
->nr_banks
;
977 d
->pin_banks
= devm_kcalloc(&pdev
->dev
, d
->nr_banks
,
978 sizeof(*d
->pin_banks
), GFP_KERNEL
);
980 return ERR_PTR(-ENOMEM
);
983 bdata
= ctrl
->pin_banks
;
984 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bdata
, ++bank
) {
985 bank
->type
= bdata
->type
;
986 bank
->pctl_offset
= bdata
->pctl_offset
;
987 bank
->nr_pins
= bdata
->nr_pins
;
988 bank
->eint_func
= bdata
->eint_func
;
989 bank
->eint_type
= bdata
->eint_type
;
990 bank
->eint_mask
= bdata
->eint_mask
;
991 bank
->eint_offset
= bdata
->eint_offset
;
992 bank
->name
= bdata
->name
;
994 spin_lock_init(&bank
->slock
);
996 bank
->pin_base
= d
->nr_pins
;
997 d
->nr_pins
+= bank
->nr_pins
;
1000 for_each_child_of_node(node
, np
) {
1001 if (!of_find_property(np
, "gpio-controller", NULL
))
1003 bank
= d
->pin_banks
;
1004 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
1005 if (!strcmp(bank
->name
, np
->name
)) {
1012 d
->pin_base
= pin_base
;
1013 pin_base
+= d
->nr_pins
;
1018 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
1020 struct samsung_pinctrl_drv_data
*drvdata
;
1021 const struct samsung_pin_ctrl
*ctrl
;
1022 struct device
*dev
= &pdev
->dev
;
1023 struct resource
*res
;
1026 if (!dev
->of_node
) {
1027 dev_err(dev
, "device tree node not found\n");
1031 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
1033 dev_err(dev
, "failed to allocate memory for driver's "
1038 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
1040 dev_err(&pdev
->dev
, "driver data not available\n");
1041 return PTR_ERR(ctrl
);
1045 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1046 drvdata
->virt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1047 if (IS_ERR(drvdata
->virt_base
))
1048 return PTR_ERR(drvdata
->virt_base
);
1050 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1052 drvdata
->irq
= res
->start
;
1054 ret
= samsung_gpiolib_register(pdev
, drvdata
);
1058 ret
= samsung_pinctrl_register(pdev
, drvdata
);
1060 samsung_gpiolib_unregister(pdev
, drvdata
);
1064 if (ctrl
->eint_gpio_init
)
1065 ctrl
->eint_gpio_init(drvdata
);
1066 if (ctrl
->eint_wkup_init
)
1067 ctrl
->eint_wkup_init(drvdata
);
1069 platform_set_drvdata(pdev
, drvdata
);
1071 /* Add to the global list */
1072 list_add_tail(&drvdata
->node
, &drvdata_list
);
1080 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device
1082 * Save data for all banks handled by this device.
1084 static void samsung_pinctrl_suspend_dev(
1085 struct samsung_pinctrl_drv_data
*drvdata
)
1087 void __iomem
*virt_base
= drvdata
->virt_base
;
1090 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1091 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1092 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1093 const u8
*offs
= bank
->type
->reg_offset
;
1094 const u8
*widths
= bank
->type
->fld_width
;
1095 enum pincfg_type type
;
1097 /* Registers without a powerdown config aren't lost */
1098 if (!widths
[PINCFG_TYPE_CON_PDN
])
1101 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1103 bank
->pm_save
[type
] = readl(reg
+ offs
[type
]);
1105 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1106 /* Some banks have two config registers */
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",
1111 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1112 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1114 pr_debug("Save %s @ %p (con %#010x)\n", bank
->name
,
1115 reg
, bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1119 if (drvdata
->suspend
)
1120 drvdata
->suspend(drvdata
);
1124 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device
1126 * Restore one of the banks that was saved during suspend.
1128 * We don't bother doing anything complicated to avoid glitching lines since
1129 * we're called before pad retention is turned off.
1131 static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data
*drvdata
)
1133 void __iomem
*virt_base
= drvdata
->virt_base
;
1136 if (drvdata
->resume
)
1137 drvdata
->resume(drvdata
);
1139 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1140 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1141 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1142 const u8
*offs
= bank
->type
->reg_offset
;
1143 const u8
*widths
= bank
->type
->fld_width
;
1144 enum pincfg_type type
;
1146 /* Registers without a powerdown config aren't lost */
1147 if (!widths
[PINCFG_TYPE_CON_PDN
])
1150 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1151 /* Some banks have two config registers */
1152 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1154 readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1155 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4),
1156 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1157 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1158 writel(bank
->pm_save
[PINCFG_TYPE_NUM
],
1159 reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1161 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank
->name
,
1162 reg
, readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1163 bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1165 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1167 writel(bank
->pm_save
[type
], reg
+ offs
[type
]);
1172 * samsung_pinctrl_suspend - save pinctrl state for suspend
1174 * Save data for all banks across all devices.
1176 static int samsung_pinctrl_suspend(void)
1178 struct samsung_pinctrl_drv_data
*drvdata
;
1180 list_for_each_entry(drvdata
, &drvdata_list
, node
) {
1181 samsung_pinctrl_suspend_dev(drvdata
);
1188 * samsung_pinctrl_resume - restore pinctrl state for suspend
1190 * Restore data for all banks across all devices.
1192 static void samsung_pinctrl_resume(void)
1194 struct samsung_pinctrl_drv_data
*drvdata
;
1196 list_for_each_entry_reverse(drvdata
, &drvdata_list
, node
) {
1197 samsung_pinctrl_resume_dev(drvdata
);
1202 #define samsung_pinctrl_suspend NULL
1203 #define samsung_pinctrl_resume NULL
1206 static struct syscore_ops samsung_pinctrl_syscore_ops
= {
1207 .suspend
= samsung_pinctrl_suspend
,
1208 .resume
= samsung_pinctrl_resume
,
1211 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
1212 #ifdef CONFIG_PINCTRL_EXYNOS
1213 { .compatible
= "samsung,exynos3250-pinctrl",
1214 .data
= (void *)exynos3250_pin_ctrl
},
1215 { .compatible
= "samsung,exynos4210-pinctrl",
1216 .data
= (void *)exynos4210_pin_ctrl
},
1217 { .compatible
= "samsung,exynos4x12-pinctrl",
1218 .data
= (void *)exynos4x12_pin_ctrl
},
1219 { .compatible
= "samsung,exynos4415-pinctrl",
1220 .data
= (void *)exynos4415_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,exynos5433-pinctrl",
1228 .data
= (void *)exynos5433_pin_ctrl
},
1229 { .compatible
= "samsung,s5pv210-pinctrl",
1230 .data
= (void *)s5pv210_pin_ctrl
},
1231 { .compatible
= "samsung,exynos7-pinctrl",
1232 .data
= (void *)exynos7_pin_ctrl
},
1234 #ifdef CONFIG_PINCTRL_S3C64XX
1235 { .compatible
= "samsung,s3c64xx-pinctrl",
1236 .data
= s3c64xx_pin_ctrl
},
1238 #ifdef CONFIG_PINCTRL_S3C24XX
1239 { .compatible
= "samsung,s3c2412-pinctrl",
1240 .data
= s3c2412_pin_ctrl
},
1241 { .compatible
= "samsung,s3c2416-pinctrl",
1242 .data
= s3c2416_pin_ctrl
},
1243 { .compatible
= "samsung,s3c2440-pinctrl",
1244 .data
= s3c2440_pin_ctrl
},
1245 { .compatible
= "samsung,s3c2450-pinctrl",
1246 .data
= s3c2450_pin_ctrl
},
1250 MODULE_DEVICE_TABLE(of
, samsung_pinctrl_dt_match
);
1252 static struct platform_driver samsung_pinctrl_driver
= {
1253 .probe
= samsung_pinctrl_probe
,
1255 .name
= "samsung-pinctrl",
1256 .of_match_table
= samsung_pinctrl_dt_match
,
1260 static int __init
samsung_pinctrl_drv_register(void)
1263 * Register syscore ops for save/restore of registers across suspend.
1264 * It's important to ensure that this driver is running at an earlier
1265 * initcall level than any arch-specific init calls that install syscore
1266 * ops that turn off pad retention (like exynos_pm_resume).
1268 register_syscore_ops(&samsung_pinctrl_syscore_ops
);
1270 return platform_driver_register(&samsung_pinctrl_driver
);
1272 postcore_initcall(samsung_pinctrl_drv_register
);
1274 static void __exit
samsung_pinctrl_drv_unregister(void)
1276 platform_driver_unregister(&samsung_pinctrl_driver
);
1278 module_exit(samsung_pinctrl_drv_unregister
);
1280 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1281 MODULE_DESCRIPTION("Samsung pinctrl driver");
1282 MODULE_LICENSE("GPL v2");