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/init.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/of_device.h>
31 #include <linux/spinlock.h>
34 #include "pinctrl-samsung.h"
36 /* maximum number of the memory resources */
37 #define SAMSUNG_PINCTRL_NUM_RESOURCES 2
39 /* list of all possible config options supported */
40 static struct pin_config
{
42 enum pincfg_type param
;
44 { "samsung,pin-pud", PINCFG_TYPE_PUD
},
45 { "samsung,pin-drv", PINCFG_TYPE_DRV
},
46 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN
},
47 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN
},
48 { "samsung,pin-val", PINCFG_TYPE_DAT
},
51 static unsigned int pin_base
;
53 static int samsung_get_group_count(struct pinctrl_dev
*pctldev
)
55 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
57 return pmx
->nr_groups
;
60 static const char *samsung_get_group_name(struct pinctrl_dev
*pctldev
,
63 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
65 return pmx
->pin_groups
[group
].name
;
68 static int samsung_get_group_pins(struct pinctrl_dev
*pctldev
,
70 const unsigned **pins
,
73 struct samsung_pinctrl_drv_data
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
75 *pins
= pmx
->pin_groups
[group
].pins
;
76 *num_pins
= pmx
->pin_groups
[group
].num_pins
;
81 static int reserve_map(struct device
*dev
, struct pinctrl_map
**map
,
82 unsigned *reserved_maps
, unsigned *num_maps
,
85 unsigned old_num
= *reserved_maps
;
86 unsigned new_num
= *num_maps
+ reserve
;
87 struct pinctrl_map
*new_map
;
89 if (old_num
>= new_num
)
92 new_map
= krealloc(*map
, sizeof(*new_map
) * new_num
, GFP_KERNEL
);
96 memset(new_map
+ old_num
, 0, (new_num
- old_num
) * sizeof(*new_map
));
99 *reserved_maps
= new_num
;
104 static int add_map_mux(struct pinctrl_map
**map
, unsigned *reserved_maps
,
105 unsigned *num_maps
, const char *group
,
106 const char *function
)
108 if (WARN_ON(*num_maps
== *reserved_maps
))
111 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
112 (*map
)[*num_maps
].data
.mux
.group
= group
;
113 (*map
)[*num_maps
].data
.mux
.function
= function
;
119 static int add_map_configs(struct device
*dev
, struct pinctrl_map
**map
,
120 unsigned *reserved_maps
, unsigned *num_maps
,
121 const char *group
, unsigned long *configs
,
122 unsigned num_configs
)
124 unsigned long *dup_configs
;
126 if (WARN_ON(*num_maps
== *reserved_maps
))
129 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
134 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
135 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
136 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
137 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
143 static int add_config(struct device
*dev
, unsigned long **configs
,
144 unsigned *num_configs
, unsigned long config
)
146 unsigned old_num
= *num_configs
;
147 unsigned new_num
= old_num
+ 1;
148 unsigned long *new_configs
;
150 new_configs
= krealloc(*configs
, sizeof(*new_configs
) * new_num
,
155 new_configs
[old_num
] = config
;
157 *configs
= new_configs
;
158 *num_configs
= new_num
;
163 static void samsung_dt_free_map(struct pinctrl_dev
*pctldev
,
164 struct pinctrl_map
*map
,
169 for (i
= 0; i
< num_maps
; i
++)
170 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
171 kfree(map
[i
].data
.configs
.configs
);
176 static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data
*drvdata
,
178 struct device_node
*np
,
179 struct pinctrl_map
**map
,
180 unsigned *reserved_maps
,
185 unsigned long config
;
186 unsigned long *configs
= NULL
;
187 unsigned num_configs
= 0;
189 struct property
*prop
;
191 bool has_func
= false;
193 ret
= of_property_read_u32(np
, "samsung,pin-function", &val
);
197 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
198 ret
= of_property_read_u32(np
, cfg_params
[i
].property
, &val
);
200 config
= PINCFG_PACK(cfg_params
[i
].param
, val
);
201 ret
= add_config(dev
, &configs
, &num_configs
, config
);
204 /* EINVAL=missing, which is fine since it's optional */
205 } else if (ret
!= -EINVAL
) {
206 dev_err(dev
, "could not parse property %s\n",
207 cfg_params
[i
].property
);
216 ret
= of_property_count_strings(np
, "samsung,pins");
218 dev_err(dev
, "could not parse property samsung,pins\n");
223 ret
= reserve_map(dev
, map
, reserved_maps
, num_maps
, reserve
);
227 of_property_for_each_string(np
, "samsung,pins", prop
, group
) {
229 ret
= add_map_mux(map
, reserved_maps
,
230 num_maps
, group
, np
->full_name
);
236 ret
= add_map_configs(dev
, map
, reserved_maps
,
237 num_maps
, group
, configs
,
251 static int samsung_dt_node_to_map(struct pinctrl_dev
*pctldev
,
252 struct device_node
*np_config
,
253 struct pinctrl_map
**map
,
256 struct samsung_pinctrl_drv_data
*drvdata
;
257 unsigned reserved_maps
;
258 struct device_node
*np
;
261 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
267 if (!of_get_child_count(np_config
))
268 return samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
,
273 for_each_child_of_node(np_config
, np
) {
274 ret
= samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
, np
, map
,
275 &reserved_maps
, num_maps
);
277 samsung_dt_free_map(pctldev
, *map
, *num_maps
);
285 /* list of pinctrl callbacks for the pinctrl core */
286 static const struct pinctrl_ops samsung_pctrl_ops
= {
287 .get_groups_count
= samsung_get_group_count
,
288 .get_group_name
= samsung_get_group_name
,
289 .get_group_pins
= samsung_get_group_pins
,
290 .dt_node_to_map
= samsung_dt_node_to_map
,
291 .dt_free_map
= samsung_dt_free_map
,
294 /* check if the selector is a valid pin function selector */
295 static int samsung_get_functions_count(struct pinctrl_dev
*pctldev
)
297 struct samsung_pinctrl_drv_data
*drvdata
;
299 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
300 return drvdata
->nr_functions
;
303 /* return the name of the pin function specified */
304 static const char *samsung_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
307 struct samsung_pinctrl_drv_data
*drvdata
;
309 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
310 return drvdata
->pmx_functions
[selector
].name
;
313 /* return the groups associated for the specified function selector */
314 static int samsung_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
315 unsigned selector
, const char * const **groups
,
316 unsigned * const num_groups
)
318 struct samsung_pinctrl_drv_data
*drvdata
;
320 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
321 *groups
= drvdata
->pmx_functions
[selector
].groups
;
322 *num_groups
= drvdata
->pmx_functions
[selector
].num_groups
;
327 * given a pin number that is local to a pin controller, find out the pin bank
328 * and the register base of the pin bank.
330 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data
*drvdata
,
331 unsigned pin
, void __iomem
**reg
, u32
*offset
,
332 struct samsung_pin_bank
**bank
)
334 struct samsung_pin_bank
*b
;
336 b
= drvdata
->pin_banks
;
338 while ((pin
>= b
->pin_base
) &&
339 ((b
->pin_base
+ b
->nr_pins
- 1) < pin
))
342 *reg
= b
->pctl_base
+ b
->pctl_offset
;
343 *offset
= pin
- b
->pin_base
;
348 /* enable or disable a pinmux function */
349 static void samsung_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
352 struct samsung_pinctrl_drv_data
*drvdata
;
353 const struct samsung_pin_bank_type
*type
;
354 struct samsung_pin_bank
*bank
;
356 u32 mask
, shift
, data
, pin_offset
;
358 const struct samsung_pmx_func
*func
;
359 const struct samsung_pin_group
*grp
;
361 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
362 func
= &drvdata
->pmx_functions
[selector
];
363 grp
= &drvdata
->pin_groups
[group
];
365 pin_to_reg_bank(drvdata
, grp
->pins
[0] - drvdata
->pin_base
,
366 ®
, &pin_offset
, &bank
);
368 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
369 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
371 /* Some banks have two config registers */
376 spin_lock_irqsave(&bank
->slock
, flags
);
378 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
379 data
&= ~(mask
<< shift
);
380 data
|= func
->val
<< shift
;
381 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
383 spin_unlock_irqrestore(&bank
->slock
, flags
);
386 /* enable a specified pinmux by writing to registers */
387 static int samsung_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
391 samsung_pinmux_setup(pctldev
, selector
, group
);
395 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
396 static const struct pinmux_ops samsung_pinmux_ops
= {
397 .get_functions_count
= samsung_get_functions_count
,
398 .get_function_name
= samsung_pinmux_get_fname
,
399 .get_function_groups
= samsung_pinmux_get_groups
,
400 .set_mux
= samsung_pinmux_set_mux
,
403 /* set or get the pin config settings for a specified pin */
404 static int samsung_pinconf_rw(struct pinctrl_dev
*pctldev
, unsigned int pin
,
405 unsigned long *config
, bool set
)
407 struct samsung_pinctrl_drv_data
*drvdata
;
408 const struct samsung_pin_bank_type
*type
;
409 struct samsung_pin_bank
*bank
;
410 void __iomem
*reg_base
;
411 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
412 u32 data
, width
, pin_offset
, mask
, shift
;
413 u32 cfg_value
, cfg_reg
;
416 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
417 pin_to_reg_bank(drvdata
, pin
- drvdata
->pin_base
, ®_base
,
421 if (cfg_type
>= PINCFG_TYPE_NUM
|| !type
->fld_width
[cfg_type
])
424 width
= type
->fld_width
[cfg_type
];
425 cfg_reg
= type
->reg_offset
[cfg_type
];
427 spin_lock_irqsave(&bank
->slock
, flags
);
429 mask
= (1 << width
) - 1;
430 shift
= pin_offset
* width
;
431 data
= readl(reg_base
+ cfg_reg
);
434 cfg_value
= PINCFG_UNPACK_VALUE(*config
);
435 data
&= ~(mask
<< shift
);
436 data
|= (cfg_value
<< shift
);
437 writel(data
, reg_base
+ cfg_reg
);
441 *config
= PINCFG_PACK(cfg_type
, data
);
444 spin_unlock_irqrestore(&bank
->slock
, flags
);
449 /* set the pin config settings for a specified pin */
450 static int samsung_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
451 unsigned long *configs
, unsigned num_configs
)
455 for (i
= 0; i
< num_configs
; i
++) {
456 ret
= samsung_pinconf_rw(pctldev
, pin
, &configs
[i
], true);
459 } /* for each config */
464 /* get the pin config settings for a specified pin */
465 static int samsung_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
466 unsigned long *config
)
468 return samsung_pinconf_rw(pctldev
, pin
, config
, false);
471 /* set the pin config settings for a specified pin group */
472 static int samsung_pinconf_group_set(struct pinctrl_dev
*pctldev
,
473 unsigned group
, unsigned long *configs
,
474 unsigned num_configs
)
476 struct samsung_pinctrl_drv_data
*drvdata
;
477 const unsigned int *pins
;
480 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
481 pins
= drvdata
->pin_groups
[group
].pins
;
483 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++)
484 samsung_pinconf_set(pctldev
, pins
[cnt
], configs
, num_configs
);
489 /* get the pin config settings for a specified pin group */
490 static int samsung_pinconf_group_get(struct pinctrl_dev
*pctldev
,
491 unsigned int group
, unsigned long *config
)
493 struct samsung_pinctrl_drv_data
*drvdata
;
494 const unsigned int *pins
;
496 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
497 pins
= drvdata
->pin_groups
[group
].pins
;
498 samsung_pinconf_get(pctldev
, pins
[0], config
);
502 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
503 static const struct pinconf_ops samsung_pinconf_ops
= {
504 .pin_config_get
= samsung_pinconf_get
,
505 .pin_config_set
= samsung_pinconf_set
,
506 .pin_config_group_get
= samsung_pinconf_group_get
,
507 .pin_config_group_set
= samsung_pinconf_group_set
,
511 * The samsung_gpio_set_vlaue() should be called with "bank->slock" held
512 * to avoid race condition.
514 static void samsung_gpio_set_value(struct gpio_chip
*gc
,
515 unsigned offset
, int value
)
517 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
518 const struct samsung_pin_bank_type
*type
= bank
->type
;
522 reg
= bank
->pctl_base
+ bank
->pctl_offset
;
524 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
525 data
&= ~(1 << offset
);
528 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
531 /* gpiolib gpio_set callback function */
532 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
534 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
537 spin_lock_irqsave(&bank
->slock
, flags
);
538 samsung_gpio_set_value(gc
, offset
, value
);
539 spin_unlock_irqrestore(&bank
->slock
, flags
);
542 /* gpiolib gpio_get callback function */
543 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
547 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
548 const struct samsung_pin_bank_type
*type
= bank
->type
;
550 reg
= bank
->pctl_base
+ bank
->pctl_offset
;
552 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
559 * The samsung_gpio_set_direction() should be called with "bank->slock" held
560 * to avoid race condition.
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
;
570 u32 data
, mask
, shift
;
572 bank
= gpiochip_get_data(gc
);
575 reg
= bank
->pctl_base
+ bank
->pctl_offset
576 + type
->reg_offset
[PINCFG_TYPE_FUNC
];
578 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
579 shift
= offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
581 /* Some banks have two config registers */
587 data
&= ~(mask
<< shift
);
589 data
|= FUNC_OUTPUT
<< shift
;
595 /* gpiolib gpio_direction_input callback function. */
596 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
598 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
602 spin_lock_irqsave(&bank
->slock
, flags
);
603 ret
= samsung_gpio_set_direction(gc
, offset
, true);
604 spin_unlock_irqrestore(&bank
->slock
, flags
);
608 /* gpiolib gpio_direction_output callback function. */
609 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
612 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
616 spin_lock_irqsave(&bank
->slock
, flags
);
617 samsung_gpio_set_value(gc
, offset
, value
);
618 ret
= samsung_gpio_set_direction(gc
, offset
, false);
619 spin_unlock_irqrestore(&bank
->slock
, flags
);
625 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
626 * and a virtual IRQ, if not already present.
628 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
630 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
633 if (!bank
->irq_domain
)
636 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
638 return (virq
) ? : -ENXIO
;
641 static struct samsung_pin_group
*samsung_pinctrl_create_groups(
643 struct samsung_pinctrl_drv_data
*drvdata
,
646 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
647 struct samsung_pin_group
*groups
, *grp
;
648 const struct pinctrl_pin_desc
*pdesc
;
651 groups
= devm_kzalloc(dev
, ctrldesc
->npins
* sizeof(*groups
),
654 return ERR_PTR(-EINVAL
);
657 pdesc
= ctrldesc
->pins
;
658 for (i
= 0; i
< ctrldesc
->npins
; ++i
, ++pdesc
, ++grp
) {
659 grp
->name
= pdesc
->name
;
660 grp
->pins
= &pdesc
->number
;
664 *cnt
= ctrldesc
->npins
;
668 static int samsung_pinctrl_create_function(struct device
*dev
,
669 struct samsung_pinctrl_drv_data
*drvdata
,
670 struct device_node
*func_np
,
671 struct samsung_pmx_func
*func
)
677 if (of_property_read_u32(func_np
, "samsung,pin-function", &func
->val
))
680 npins
= of_property_count_strings(func_np
, "samsung,pins");
682 dev_err(dev
, "invalid pin list in %s node", func_np
->name
);
686 func
->name
= func_np
->full_name
;
688 func
->groups
= devm_kzalloc(dev
, npins
* sizeof(char *), GFP_KERNEL
);
692 for (i
= 0; i
< npins
; ++i
) {
695 ret
= of_property_read_string_index(func_np
, "samsung,pins",
699 "failed to read pin name %d from %s node\n",
704 func
->groups
[i
] = gname
;
707 func
->num_groups
= npins
;
711 static struct samsung_pmx_func
*samsung_pinctrl_create_functions(
713 struct samsung_pinctrl_drv_data
*drvdata
,
716 struct samsung_pmx_func
*functions
, *func
;
717 struct device_node
*dev_np
= dev
->of_node
;
718 struct device_node
*cfg_np
;
719 unsigned int func_cnt
= 0;
723 * Iterate over all the child nodes of the pin controller node
724 * and create pin groups and pin function lists.
726 for_each_child_of_node(dev_np
, cfg_np
) {
727 struct device_node
*func_np
;
729 if (!of_get_child_count(cfg_np
)) {
730 if (!of_find_property(cfg_np
,
731 "samsung,pin-function", NULL
))
737 for_each_child_of_node(cfg_np
, func_np
) {
738 if (!of_find_property(func_np
,
739 "samsung,pin-function", NULL
))
745 functions
= devm_kzalloc(dev
, func_cnt
* sizeof(*functions
),
748 return ERR_PTR(-ENOMEM
);
752 * Iterate over all the child nodes of the pin controller node
753 * and create pin groups and pin function lists.
756 for_each_child_of_node(dev_np
, cfg_np
) {
757 struct device_node
*func_np
;
759 if (!of_get_child_count(cfg_np
)) {
760 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
771 for_each_child_of_node(cfg_np
, func_np
) {
772 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
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
);
841 ctrldesc
->pins
= pindesc
;
842 ctrldesc
->npins
= drvdata
->nr_pins
;
844 /* dynamically populate the pin number and pin name for pindesc */
845 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
846 pdesc
->number
= pin
+ drvdata
->pin_base
;
849 * allocate space for storing the dynamically generated names for all
850 * the pins which belong to this pin-controller.
852 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
853 drvdata
->nr_pins
, GFP_KERNEL
);
857 /* for each pin, the name of the pin is pin-bank name + pin number */
858 for (bank
= 0; bank
< drvdata
->nr_banks
; bank
++) {
859 pin_bank
= &drvdata
->pin_banks
[bank
];
860 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
861 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
862 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
863 pdesc
->name
= pin_names
;
864 pin_names
+= PIN_NAME_LENGTH
;
868 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
872 drvdata
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, ctrldesc
,
874 if (IS_ERR(drvdata
->pctl_dev
)) {
875 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
876 return PTR_ERR(drvdata
->pctl_dev
);
879 for (bank
= 0; bank
< drvdata
->nr_banks
; ++bank
) {
880 pin_bank
= &drvdata
->pin_banks
[bank
];
881 pin_bank
->grange
.name
= pin_bank
->name
;
882 pin_bank
->grange
.id
= bank
;
883 pin_bank
->grange
.pin_base
= drvdata
->pin_base
884 + pin_bank
->pin_base
;
885 pin_bank
->grange
.base
= pin_bank
->grange
.pin_base
;
886 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
887 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
888 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
894 /* unregister the pinctrl interface with the pinctrl subsystem */
895 static int samsung_pinctrl_unregister(struct platform_device
*pdev
,
896 struct samsung_pinctrl_drv_data
*drvdata
)
898 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
901 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
902 pinctrl_remove_gpio_range(drvdata
->pctl_dev
, &bank
->grange
);
907 static const struct gpio_chip samsung_gpiolib_chip
= {
908 .request
= gpiochip_generic_request
,
909 .free
= gpiochip_generic_free
,
910 .set
= samsung_gpio_set
,
911 .get
= samsung_gpio_get
,
912 .direction_input
= samsung_gpio_direction_input
,
913 .direction_output
= samsung_gpio_direction_output
,
914 .to_irq
= samsung_gpio_to_irq
,
915 .owner
= THIS_MODULE
,
918 /* register the gpiolib interface with the gpiolib subsystem */
919 static int samsung_gpiolib_register(struct platform_device
*pdev
,
920 struct samsung_pinctrl_drv_data
*drvdata
)
922 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
923 struct gpio_chip
*gc
;
927 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
) {
928 bank
->gpio_chip
= samsung_gpiolib_chip
;
930 gc
= &bank
->gpio_chip
;
931 gc
->base
= bank
->grange
.base
;
932 gc
->ngpio
= bank
->nr_pins
;
933 gc
->parent
= &pdev
->dev
;
934 gc
->of_node
= bank
->of_node
;
935 gc
->label
= bank
->name
;
937 ret
= devm_gpiochip_add_data(&pdev
->dev
, gc
, bank
);
939 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
948 /* retrieve the soc specific data */
949 static const struct samsung_pin_ctrl
*
950 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data
*d
,
951 struct platform_device
*pdev
)
954 struct device_node
*node
= pdev
->dev
.of_node
;
955 struct device_node
*np
;
956 const struct samsung_pin_bank_data
*bdata
;
957 const struct samsung_pin_ctrl
*ctrl
;
958 struct samsung_pin_bank
*bank
;
959 struct resource
*res
;
960 void __iomem
*virt_base
[SAMSUNG_PINCTRL_NUM_RESOURCES
];
963 id
= of_alias_get_id(node
, "pinctrl");
965 dev_err(&pdev
->dev
, "failed to get alias id\n");
966 return ERR_PTR(-ENOENT
);
968 ctrl
= of_device_get_match_data(&pdev
->dev
);
971 d
->suspend
= ctrl
->suspend
;
972 d
->resume
= ctrl
->resume
;
973 d
->nr_banks
= ctrl
->nr_banks
;
974 d
->pin_banks
= devm_kcalloc(&pdev
->dev
, d
->nr_banks
,
975 sizeof(*d
->pin_banks
), GFP_KERNEL
);
977 return ERR_PTR(-ENOMEM
);
979 if (ctrl
->nr_ext_resources
+ 1 > SAMSUNG_PINCTRL_NUM_RESOURCES
)
980 return ERR_PTR(-EINVAL
);
982 for (i
= 0; i
< ctrl
->nr_ext_resources
+ 1; i
++) {
983 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
985 dev_err(&pdev
->dev
, "failed to get mem%d resource\n", i
);
986 return ERR_PTR(-EINVAL
);
988 virt_base
[i
] = devm_ioremap(&pdev
->dev
, res
->start
,
991 dev_err(&pdev
->dev
, "failed to ioremap %pR\n", res
);
992 return ERR_PTR(-EIO
);
997 bdata
= ctrl
->pin_banks
;
998 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bdata
, ++bank
) {
999 bank
->type
= bdata
->type
;
1000 bank
->pctl_offset
= bdata
->pctl_offset
;
1001 bank
->nr_pins
= bdata
->nr_pins
;
1002 bank
->eint_func
= bdata
->eint_func
;
1003 bank
->eint_type
= bdata
->eint_type
;
1004 bank
->eint_mask
= bdata
->eint_mask
;
1005 bank
->eint_offset
= bdata
->eint_offset
;
1006 bank
->name
= bdata
->name
;
1008 spin_lock_init(&bank
->slock
);
1010 bank
->pin_base
= d
->nr_pins
;
1011 d
->nr_pins
+= bank
->nr_pins
;
1013 bank
->eint_base
= virt_base
[0];
1014 bank
->pctl_base
= virt_base
[bdata
->pctl_res_idx
];
1017 for_each_child_of_node(node
, np
) {
1018 if (!of_find_property(np
, "gpio-controller", NULL
))
1020 bank
= d
->pin_banks
;
1021 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
1022 if (!strcmp(bank
->name
, np
->name
)) {
1029 d
->pin_base
= pin_base
;
1030 pin_base
+= d
->nr_pins
;
1035 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
1037 struct samsung_pinctrl_drv_data
*drvdata
;
1038 const struct samsung_pin_ctrl
*ctrl
;
1039 struct device
*dev
= &pdev
->dev
;
1040 struct resource
*res
;
1043 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
1047 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
1049 dev_err(&pdev
->dev
, "driver data not available\n");
1050 return PTR_ERR(ctrl
);
1054 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1056 drvdata
->irq
= res
->start
;
1058 if (ctrl
->retention_data
) {
1059 drvdata
->retention_ctrl
= ctrl
->retention_data
->init(drvdata
,
1060 ctrl
->retention_data
);
1061 if (IS_ERR(drvdata
->retention_ctrl
))
1062 return PTR_ERR(drvdata
->retention_ctrl
);
1065 ret
= samsung_pinctrl_register(pdev
, drvdata
);
1069 ret
= samsung_gpiolib_register(pdev
, drvdata
);
1071 samsung_pinctrl_unregister(pdev
, drvdata
);
1075 if (ctrl
->eint_gpio_init
)
1076 ctrl
->eint_gpio_init(drvdata
);
1077 if (ctrl
->eint_wkup_init
)
1078 ctrl
->eint_wkup_init(drvdata
);
1080 platform_set_drvdata(pdev
, drvdata
);
1086 * samsung_pinctrl_suspend - save pinctrl state for suspend
1088 * Save data for all banks handled by this device.
1090 static int __maybe_unused
samsung_pinctrl_suspend(struct device
*dev
)
1092 struct samsung_pinctrl_drv_data
*drvdata
= dev_get_drvdata(dev
);
1095 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1096 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1097 void __iomem
*reg
= bank
->pctl_base
+ bank
->pctl_offset
;
1098 const u8
*offs
= bank
->type
->reg_offset
;
1099 const u8
*widths
= bank
->type
->fld_width
;
1100 enum pincfg_type type
;
1102 /* Registers without a powerdown config aren't lost */
1103 if (!widths
[PINCFG_TYPE_CON_PDN
])
1106 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1108 bank
->pm_save
[type
] = readl(reg
+ offs
[type
]);
1110 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1111 /* Some banks have two config registers */
1112 bank
->pm_save
[PINCFG_TYPE_NUM
] =
1113 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1114 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1116 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1117 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1119 pr_debug("Save %s @ %p (con %#010x)\n", bank
->name
,
1120 reg
, bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1124 if (drvdata
->suspend
)
1125 drvdata
->suspend(drvdata
);
1126 if (drvdata
->retention_ctrl
&& drvdata
->retention_ctrl
->enable
)
1127 drvdata
->retention_ctrl
->enable(drvdata
);
1133 * samsung_pinctrl_resume - restore pinctrl state from suspend
1135 * Restore one of the banks that was saved during suspend.
1137 * We don't bother doing anything complicated to avoid glitching lines since
1138 * we're called before pad retention is turned off.
1140 static int __maybe_unused
samsung_pinctrl_resume(struct device
*dev
)
1142 struct samsung_pinctrl_drv_data
*drvdata
= dev_get_drvdata(dev
);
1145 if (drvdata
->resume
)
1146 drvdata
->resume(drvdata
);
1148 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1149 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1150 void __iomem
*reg
= bank
->pctl_base
+ bank
->pctl_offset
;
1151 const u8
*offs
= bank
->type
->reg_offset
;
1152 const u8
*widths
= bank
->type
->fld_width
;
1153 enum pincfg_type type
;
1155 /* Registers without a powerdown config aren't lost */
1156 if (!widths
[PINCFG_TYPE_CON_PDN
])
1159 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1160 /* Some banks have two config registers */
1161 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1163 readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1164 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4),
1165 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1166 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1167 writel(bank
->pm_save
[PINCFG_TYPE_NUM
],
1168 reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1170 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank
->name
,
1171 reg
, readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1172 bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1174 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1176 writel(bank
->pm_save
[type
], reg
+ offs
[type
]);
1179 if (drvdata
->retention_ctrl
&& drvdata
->retention_ctrl
->disable
)
1180 drvdata
->retention_ctrl
->disable(drvdata
);
1185 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
1186 #ifdef CONFIG_PINCTRL_EXYNOS_ARM
1187 { .compatible
= "samsung,exynos3250-pinctrl",
1188 .data
= exynos3250_pin_ctrl
},
1189 { .compatible
= "samsung,exynos4210-pinctrl",
1190 .data
= exynos4210_pin_ctrl
},
1191 { .compatible
= "samsung,exynos4x12-pinctrl",
1192 .data
= exynos4x12_pin_ctrl
},
1193 { .compatible
= "samsung,exynos5250-pinctrl",
1194 .data
= exynos5250_pin_ctrl
},
1195 { .compatible
= "samsung,exynos5260-pinctrl",
1196 .data
= exynos5260_pin_ctrl
},
1197 { .compatible
= "samsung,exynos5410-pinctrl",
1198 .data
= exynos5410_pin_ctrl
},
1199 { .compatible
= "samsung,exynos5420-pinctrl",
1200 .data
= exynos5420_pin_ctrl
},
1201 { .compatible
= "samsung,s5pv210-pinctrl",
1202 .data
= s5pv210_pin_ctrl
},
1204 #ifdef CONFIG_PINCTRL_EXYNOS_ARM64
1205 { .compatible
= "samsung,exynos5433-pinctrl",
1206 .data
= exynos5433_pin_ctrl
},
1207 { .compatible
= "samsung,exynos7-pinctrl",
1208 .data
= exynos7_pin_ctrl
},
1210 #ifdef CONFIG_PINCTRL_S3C64XX
1211 { .compatible
= "samsung,s3c64xx-pinctrl",
1212 .data
= s3c64xx_pin_ctrl
},
1214 #ifdef CONFIG_PINCTRL_S3C24XX
1215 { .compatible
= "samsung,s3c2412-pinctrl",
1216 .data
= s3c2412_pin_ctrl
},
1217 { .compatible
= "samsung,s3c2416-pinctrl",
1218 .data
= s3c2416_pin_ctrl
},
1219 { .compatible
= "samsung,s3c2440-pinctrl",
1220 .data
= s3c2440_pin_ctrl
},
1221 { .compatible
= "samsung,s3c2450-pinctrl",
1222 .data
= s3c2450_pin_ctrl
},
1227 static const struct dev_pm_ops samsung_pinctrl_pm_ops
= {
1228 SET_LATE_SYSTEM_SLEEP_PM_OPS(samsung_pinctrl_suspend
,
1229 samsung_pinctrl_resume
)
1232 static struct platform_driver samsung_pinctrl_driver
= {
1233 .probe
= samsung_pinctrl_probe
,
1235 .name
= "samsung-pinctrl",
1236 .of_match_table
= samsung_pinctrl_dt_match
,
1237 .suppress_bind_attrs
= true,
1238 .pm
= &samsung_pinctrl_pm_ops
,
1242 static int __init
samsung_pinctrl_drv_register(void)
1244 return platform_driver_register(&samsung_pinctrl_driver
);
1246 postcore_initcall(samsung_pinctrl_drv_register
);