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 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
);
94 dev_err(dev
, "krealloc(map) failed\n");
98 memset(new_map
+ old_num
, 0, (new_num
- old_num
) * sizeof(*new_map
));
101 *reserved_maps
= new_num
;
106 static int add_map_mux(struct pinctrl_map
**map
, unsigned *reserved_maps
,
107 unsigned *num_maps
, const char *group
,
108 const char *function
)
110 if (WARN_ON(*num_maps
== *reserved_maps
))
113 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
114 (*map
)[*num_maps
].data
.mux
.group
= group
;
115 (*map
)[*num_maps
].data
.mux
.function
= function
;
121 static int add_map_configs(struct device
*dev
, struct pinctrl_map
**map
,
122 unsigned *reserved_maps
, unsigned *num_maps
,
123 const char *group
, unsigned long *configs
,
124 unsigned num_configs
)
126 unsigned long *dup_configs
;
128 if (WARN_ON(*num_maps
== *reserved_maps
))
131 dup_configs
= kmemdup(configs
, num_configs
* sizeof(*dup_configs
),
134 dev_err(dev
, "kmemdup(configs) failed\n");
138 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
139 (*map
)[*num_maps
].data
.configs
.group_or_pin
= group
;
140 (*map
)[*num_maps
].data
.configs
.configs
= dup_configs
;
141 (*map
)[*num_maps
].data
.configs
.num_configs
= num_configs
;
147 static int add_config(struct device
*dev
, unsigned long **configs
,
148 unsigned *num_configs
, unsigned long config
)
150 unsigned old_num
= *num_configs
;
151 unsigned new_num
= old_num
+ 1;
152 unsigned long *new_configs
;
154 new_configs
= krealloc(*configs
, sizeof(*new_configs
) * new_num
,
157 dev_err(dev
, "krealloc(configs) failed\n");
161 new_configs
[old_num
] = config
;
163 *configs
= new_configs
;
164 *num_configs
= new_num
;
169 static void samsung_dt_free_map(struct pinctrl_dev
*pctldev
,
170 struct pinctrl_map
*map
,
175 for (i
= 0; i
< num_maps
; i
++)
176 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
)
177 kfree(map
[i
].data
.configs
.configs
);
182 static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data
*drvdata
,
184 struct device_node
*np
,
185 struct pinctrl_map
**map
,
186 unsigned *reserved_maps
,
191 unsigned long config
;
192 unsigned long *configs
= NULL
;
193 unsigned num_configs
= 0;
195 struct property
*prop
;
197 bool has_func
= false;
199 ret
= of_property_read_u32(np
, "samsung,pin-function", &val
);
203 for (i
= 0; i
< ARRAY_SIZE(cfg_params
); i
++) {
204 ret
= of_property_read_u32(np
, cfg_params
[i
].property
, &val
);
206 config
= PINCFG_PACK(cfg_params
[i
].param
, val
);
207 ret
= add_config(dev
, &configs
, &num_configs
, config
);
210 /* EINVAL=missing, which is fine since it's optional */
211 } else if (ret
!= -EINVAL
) {
212 dev_err(dev
, "could not parse property %s\n",
213 cfg_params
[i
].property
);
222 ret
= of_property_count_strings(np
, "samsung,pins");
224 dev_err(dev
, "could not parse property samsung,pins\n");
229 ret
= reserve_map(dev
, map
, reserved_maps
, num_maps
, reserve
);
233 of_property_for_each_string(np
, "samsung,pins", prop
, group
) {
235 ret
= add_map_mux(map
, reserved_maps
,
236 num_maps
, group
, np
->full_name
);
242 ret
= add_map_configs(dev
, map
, reserved_maps
,
243 num_maps
, group
, configs
,
257 static int samsung_dt_node_to_map(struct pinctrl_dev
*pctldev
,
258 struct device_node
*np_config
,
259 struct pinctrl_map
**map
,
262 struct samsung_pinctrl_drv_data
*drvdata
;
263 unsigned reserved_maps
;
264 struct device_node
*np
;
267 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
273 if (!of_get_child_count(np_config
))
274 return samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
,
279 for_each_child_of_node(np_config
, np
) {
280 ret
= samsung_dt_subnode_to_map(drvdata
, pctldev
->dev
, np
, map
,
281 &reserved_maps
, num_maps
);
283 samsung_dt_free_map(pctldev
, *map
, *num_maps
);
291 /* list of pinctrl callbacks for the pinctrl core */
292 static const struct pinctrl_ops samsung_pctrl_ops
= {
293 .get_groups_count
= samsung_get_group_count
,
294 .get_group_name
= samsung_get_group_name
,
295 .get_group_pins
= samsung_get_group_pins
,
296 .dt_node_to_map
= samsung_dt_node_to_map
,
297 .dt_free_map
= samsung_dt_free_map
,
300 /* check if the selector is a valid pin function selector */
301 static int samsung_get_functions_count(struct pinctrl_dev
*pctldev
)
303 struct samsung_pinctrl_drv_data
*drvdata
;
305 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
306 return drvdata
->nr_functions
;
309 /* return the name of the pin function specified */
310 static const char *samsung_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
313 struct samsung_pinctrl_drv_data
*drvdata
;
315 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
316 return drvdata
->pmx_functions
[selector
].name
;
319 /* return the groups associated for the specified function selector */
320 static int samsung_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
321 unsigned selector
, const char * const **groups
,
322 unsigned * const num_groups
)
324 struct samsung_pinctrl_drv_data
*drvdata
;
326 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
327 *groups
= drvdata
->pmx_functions
[selector
].groups
;
328 *num_groups
= drvdata
->pmx_functions
[selector
].num_groups
;
333 * given a pin number that is local to a pin controller, find out the pin bank
334 * and the register base of the pin bank.
336 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data
*drvdata
,
337 unsigned pin
, void __iomem
**reg
, u32
*offset
,
338 struct samsung_pin_bank
**bank
)
340 struct samsung_pin_bank
*b
;
342 b
= drvdata
->pin_banks
;
344 while ((pin
>= b
->pin_base
) &&
345 ((b
->pin_base
+ b
->nr_pins
- 1) < pin
))
348 *reg
= drvdata
->virt_base
+ b
->pctl_offset
;
349 *offset
= pin
- b
->pin_base
;
354 /* enable or disable a pinmux function */
355 static void samsung_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
356 unsigned group
, bool enable
)
358 struct samsung_pinctrl_drv_data
*drvdata
;
359 const struct samsung_pin_bank_type
*type
;
360 struct samsung_pin_bank
*bank
;
362 u32 mask
, shift
, data
, pin_offset
;
364 const struct samsung_pmx_func
*func
;
365 const struct samsung_pin_group
*grp
;
367 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
368 func
= &drvdata
->pmx_functions
[selector
];
369 grp
= &drvdata
->pin_groups
[group
];
371 pin_to_reg_bank(drvdata
, grp
->pins
[0] - drvdata
->pin_base
,
372 ®
, &pin_offset
, &bank
);
374 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
375 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
377 /* Some banks have two config registers */
382 spin_lock_irqsave(&bank
->slock
, flags
);
384 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
385 data
&= ~(mask
<< shift
);
387 data
|= func
->val
<< shift
;
388 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
390 spin_unlock_irqrestore(&bank
->slock
, flags
);
393 /* enable a specified pinmux by writing to registers */
394 static int samsung_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
398 samsung_pinmux_setup(pctldev
, selector
, group
, true);
402 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
403 static const struct pinmux_ops samsung_pinmux_ops
= {
404 .get_functions_count
= samsung_get_functions_count
,
405 .get_function_name
= samsung_pinmux_get_fname
,
406 .get_function_groups
= samsung_pinmux_get_groups
,
407 .set_mux
= samsung_pinmux_set_mux
,
410 /* set or get the pin config settings for a specified pin */
411 static int samsung_pinconf_rw(struct pinctrl_dev
*pctldev
, unsigned int pin
,
412 unsigned long *config
, bool set
)
414 struct samsung_pinctrl_drv_data
*drvdata
;
415 const struct samsung_pin_bank_type
*type
;
416 struct samsung_pin_bank
*bank
;
417 void __iomem
*reg_base
;
418 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
419 u32 data
, width
, pin_offset
, mask
, shift
;
420 u32 cfg_value
, cfg_reg
;
423 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
424 pin_to_reg_bank(drvdata
, pin
- drvdata
->pin_base
, ®_base
,
428 if (cfg_type
>= PINCFG_TYPE_NUM
|| !type
->fld_width
[cfg_type
])
431 width
= type
->fld_width
[cfg_type
];
432 cfg_reg
= type
->reg_offset
[cfg_type
];
434 spin_lock_irqsave(&bank
->slock
, flags
);
436 mask
= (1 << width
) - 1;
437 shift
= pin_offset
* width
;
438 data
= readl(reg_base
+ cfg_reg
);
441 cfg_value
= PINCFG_UNPACK_VALUE(*config
);
442 data
&= ~(mask
<< shift
);
443 data
|= (cfg_value
<< shift
);
444 writel(data
, reg_base
+ cfg_reg
);
448 *config
= PINCFG_PACK(cfg_type
, data
);
451 spin_unlock_irqrestore(&bank
->slock
, flags
);
456 /* set the pin config settings for a specified pin */
457 static int samsung_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
458 unsigned long *configs
, unsigned num_configs
)
462 for (i
= 0; i
< num_configs
; i
++) {
463 ret
= samsung_pinconf_rw(pctldev
, pin
, &configs
[i
], true);
466 } /* for each config */
471 /* get the pin config settings for a specified pin */
472 static int samsung_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
473 unsigned long *config
)
475 return samsung_pinconf_rw(pctldev
, pin
, config
, false);
478 /* set the pin config settings for a specified pin group */
479 static int samsung_pinconf_group_set(struct pinctrl_dev
*pctldev
,
480 unsigned group
, unsigned long *configs
,
481 unsigned num_configs
)
483 struct samsung_pinctrl_drv_data
*drvdata
;
484 const unsigned int *pins
;
487 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
488 pins
= drvdata
->pin_groups
[group
].pins
;
490 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++)
491 samsung_pinconf_set(pctldev
, pins
[cnt
], configs
, num_configs
);
496 /* get the pin config settings for a specified pin group */
497 static int samsung_pinconf_group_get(struct pinctrl_dev
*pctldev
,
498 unsigned int group
, unsigned long *config
)
500 struct samsung_pinctrl_drv_data
*drvdata
;
501 const unsigned int *pins
;
503 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
504 pins
= drvdata
->pin_groups
[group
].pins
;
505 samsung_pinconf_get(pctldev
, pins
[0], config
);
509 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
510 static const struct pinconf_ops samsung_pinconf_ops
= {
511 .pin_config_get
= samsung_pinconf_get
,
512 .pin_config_set
= samsung_pinconf_set
,
513 .pin_config_group_get
= samsung_pinconf_group_get
,
514 .pin_config_group_set
= samsung_pinconf_group_set
,
518 * The samsung_gpio_set_vlaue() should be called with "bank->slock" held
519 * to avoid race condition.
521 static void samsung_gpio_set_value(struct gpio_chip
*gc
,
522 unsigned offset
, int value
)
524 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
525 const struct samsung_pin_bank_type
*type
= bank
->type
;
529 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
531 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
532 data
&= ~(1 << offset
);
535 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
538 /* gpiolib gpio_set callback function */
539 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
541 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
544 spin_lock_irqsave(&bank
->slock
, flags
);
545 samsung_gpio_set_value(gc
, offset
, value
);
546 spin_unlock_irqrestore(&bank
->slock
, flags
);
549 /* gpiolib gpio_get callback function */
550 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
554 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
555 const struct samsung_pin_bank_type
*type
= bank
->type
;
557 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
559 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
566 * The samsung_gpio_set_direction() should be called with "bank->slock" held
567 * to avoid race condition.
568 * The calls to gpio_direction_output() and gpio_direction_input()
569 * leads to this function call.
571 static int samsung_gpio_set_direction(struct gpio_chip
*gc
,
572 unsigned offset
, bool input
)
574 const struct samsung_pin_bank_type
*type
;
575 struct samsung_pin_bank
*bank
;
576 struct samsung_pinctrl_drv_data
*drvdata
;
578 u32 data
, mask
, shift
;
580 bank
= gpiochip_get_data(gc
);
582 drvdata
= bank
->drvdata
;
584 reg
= drvdata
->virt_base
+ bank
->pctl_offset
+
585 type
->reg_offset
[PINCFG_TYPE_FUNC
];
587 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
588 shift
= offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
590 /* Some banks have two config registers */
596 data
&= ~(mask
<< shift
);
598 data
|= FUNC_OUTPUT
<< shift
;
604 /* gpiolib gpio_direction_input callback function. */
605 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
607 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
611 spin_lock_irqsave(&bank
->slock
, flags
);
612 ret
= samsung_gpio_set_direction(gc
, offset
, true);
613 spin_unlock_irqrestore(&bank
->slock
, flags
);
617 /* gpiolib gpio_direction_output callback function. */
618 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
621 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
625 spin_lock_irqsave(&bank
->slock
, flags
);
626 samsung_gpio_set_value(gc
, offset
, value
);
627 ret
= samsung_gpio_set_direction(gc
, offset
, false);
628 spin_unlock_irqrestore(&bank
->slock
, flags
);
634 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
635 * and a virtual IRQ, if not already present.
637 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
639 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
642 if (!bank
->irq_domain
)
645 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
647 return (virq
) ? : -ENXIO
;
650 static struct samsung_pin_group
*samsung_pinctrl_create_groups(
652 struct samsung_pinctrl_drv_data
*drvdata
,
655 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
656 struct samsung_pin_group
*groups
, *grp
;
657 const struct pinctrl_pin_desc
*pdesc
;
660 groups
= devm_kzalloc(dev
, ctrldesc
->npins
* sizeof(*groups
),
663 return ERR_PTR(-EINVAL
);
666 pdesc
= ctrldesc
->pins
;
667 for (i
= 0; i
< ctrldesc
->npins
; ++i
, ++pdesc
, ++grp
) {
668 grp
->name
= pdesc
->name
;
669 grp
->pins
= &pdesc
->number
;
673 *cnt
= ctrldesc
->npins
;
677 static int samsung_pinctrl_create_function(struct device
*dev
,
678 struct samsung_pinctrl_drv_data
*drvdata
,
679 struct device_node
*func_np
,
680 struct samsung_pmx_func
*func
)
686 if (of_property_read_u32(func_np
, "samsung,pin-function", &func
->val
))
689 npins
= of_property_count_strings(func_np
, "samsung,pins");
691 dev_err(dev
, "invalid pin list in %s node", func_np
->name
);
695 func
->name
= func_np
->full_name
;
697 func
->groups
= devm_kzalloc(dev
, npins
* sizeof(char *), GFP_KERNEL
);
701 for (i
= 0; i
< npins
; ++i
) {
704 ret
= of_property_read_string_index(func_np
, "samsung,pins",
708 "failed to read pin name %d from %s node\n",
713 func
->groups
[i
] = gname
;
716 func
->num_groups
= npins
;
720 static struct samsung_pmx_func
*samsung_pinctrl_create_functions(
722 struct samsung_pinctrl_drv_data
*drvdata
,
725 struct samsung_pmx_func
*functions
, *func
;
726 struct device_node
*dev_np
= dev
->of_node
;
727 struct device_node
*cfg_np
;
728 unsigned int func_cnt
= 0;
732 * Iterate over all the child nodes of the pin controller node
733 * and create pin groups and pin function lists.
735 for_each_child_of_node(dev_np
, cfg_np
) {
736 struct device_node
*func_np
;
738 if (!of_get_child_count(cfg_np
)) {
739 if (!of_find_property(cfg_np
,
740 "samsung,pin-function", NULL
))
746 for_each_child_of_node(cfg_np
, func_np
) {
747 if (!of_find_property(func_np
,
748 "samsung,pin-function", NULL
))
754 functions
= devm_kzalloc(dev
, func_cnt
* sizeof(*functions
),
757 dev_err(dev
, "failed to allocate memory for function list\n");
758 return ERR_PTR(-EINVAL
);
763 * Iterate over all the child nodes of the pin controller node
764 * and create pin groups and pin function lists.
767 for_each_child_of_node(dev_np
, cfg_np
) {
768 struct device_node
*func_np
;
770 if (!of_get_child_count(cfg_np
)) {
771 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
782 for_each_child_of_node(cfg_np
, func_np
) {
783 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
799 * Parse the information about all the available pin groups and pin functions
800 * from device node of the pin-controller. A pin group is formed with all
801 * the pins listed in the "samsung,pins" property.
804 static int samsung_pinctrl_parse_dt(struct platform_device
*pdev
,
805 struct samsung_pinctrl_drv_data
*drvdata
)
807 struct device
*dev
= &pdev
->dev
;
808 struct samsung_pin_group
*groups
;
809 struct samsung_pmx_func
*functions
;
810 unsigned int grp_cnt
= 0, func_cnt
= 0;
812 groups
= samsung_pinctrl_create_groups(dev
, drvdata
, &grp_cnt
);
813 if (IS_ERR(groups
)) {
814 dev_err(dev
, "failed to parse pin groups\n");
815 return PTR_ERR(groups
);
818 functions
= samsung_pinctrl_create_functions(dev
, drvdata
, &func_cnt
);
819 if (IS_ERR(functions
)) {
820 dev_err(dev
, "failed to parse pin functions\n");
821 return PTR_ERR(functions
);
824 drvdata
->pin_groups
= groups
;
825 drvdata
->nr_groups
= grp_cnt
;
826 drvdata
->pmx_functions
= functions
;
827 drvdata
->nr_functions
= func_cnt
;
832 /* register the pinctrl interface with the pinctrl subsystem */
833 static int samsung_pinctrl_register(struct platform_device
*pdev
,
834 struct samsung_pinctrl_drv_data
*drvdata
)
836 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
837 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
838 struct samsung_pin_bank
*pin_bank
;
842 ctrldesc
->name
= "samsung-pinctrl";
843 ctrldesc
->owner
= THIS_MODULE
;
844 ctrldesc
->pctlops
= &samsung_pctrl_ops
;
845 ctrldesc
->pmxops
= &samsung_pinmux_ops
;
846 ctrldesc
->confops
= &samsung_pinconf_ops
;
848 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
849 drvdata
->nr_pins
, GFP_KERNEL
);
851 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
854 ctrldesc
->pins
= pindesc
;
855 ctrldesc
->npins
= drvdata
->nr_pins
;
857 /* dynamically populate the pin number and pin name for pindesc */
858 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
859 pdesc
->number
= pin
+ drvdata
->pin_base
;
862 * allocate space for storing the dynamically generated names for all
863 * the pins which belong to this pin-controller.
865 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
866 drvdata
->nr_pins
, GFP_KERNEL
);
868 dev_err(&pdev
->dev
, "mem alloc for pin names failed\n");
872 /* for each pin, the name of the pin is pin-bank name + pin number */
873 for (bank
= 0; bank
< drvdata
->nr_banks
; bank
++) {
874 pin_bank
= &drvdata
->pin_banks
[bank
];
875 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
876 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
877 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
878 pdesc
->name
= pin_names
;
879 pin_names
+= PIN_NAME_LENGTH
;
883 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
887 drvdata
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, ctrldesc
,
889 if (IS_ERR(drvdata
->pctl_dev
)) {
890 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
891 return PTR_ERR(drvdata
->pctl_dev
);
894 for (bank
= 0; bank
< drvdata
->nr_banks
; ++bank
) {
895 pin_bank
= &drvdata
->pin_banks
[bank
];
896 pin_bank
->grange
.name
= pin_bank
->name
;
897 pin_bank
->grange
.id
= bank
;
898 pin_bank
->grange
.pin_base
= drvdata
->pin_base
899 + pin_bank
->pin_base
;
900 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
901 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
902 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
903 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
909 static const struct gpio_chip samsung_gpiolib_chip
= {
910 .request
= gpiochip_generic_request
,
911 .free
= gpiochip_generic_free
,
912 .set
= samsung_gpio_set
,
913 .get
= samsung_gpio_get
,
914 .direction_input
= samsung_gpio_direction_input
,
915 .direction_output
= samsung_gpio_direction_output
,
916 .to_irq
= samsung_gpio_to_irq
,
917 .owner
= THIS_MODULE
,
920 /* register the gpiolib interface with the gpiolib subsystem */
921 static int samsung_gpiolib_register(struct platform_device
*pdev
,
922 struct samsung_pinctrl_drv_data
*drvdata
)
924 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
925 struct gpio_chip
*gc
;
929 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
) {
930 bank
->gpio_chip
= samsung_gpiolib_chip
;
932 gc
= &bank
->gpio_chip
;
933 gc
->base
= drvdata
->pin_base
+ bank
->pin_base
;
934 gc
->ngpio
= bank
->nr_pins
;
935 gc
->parent
= &pdev
->dev
;
936 gc
->of_node
= bank
->of_node
;
937 gc
->label
= bank
->name
;
939 ret
= gpiochip_add_data(gc
, bank
);
941 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
950 for (--i
, --bank
; i
>= 0; --i
, --bank
)
951 gpiochip_remove(&bank
->gpio_chip
);
955 /* unregister the gpiolib interface with the gpiolib subsystem */
956 static int samsung_gpiolib_unregister(struct platform_device
*pdev
,
957 struct samsung_pinctrl_drv_data
*drvdata
)
959 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
962 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
963 gpiochip_remove(&bank
->gpio_chip
);
968 static const struct of_device_id samsung_pinctrl_dt_match
[];
970 /* retrieve the soc specific data */
971 static const struct samsung_pin_ctrl
*
972 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data
*d
,
973 struct platform_device
*pdev
)
976 const struct of_device_id
*match
;
977 struct device_node
*node
= pdev
->dev
.of_node
;
978 struct device_node
*np
;
979 const struct samsung_pin_bank_data
*bdata
;
980 const struct samsung_pin_ctrl
*ctrl
;
981 struct samsung_pin_bank
*bank
;
984 id
= of_alias_get_id(node
, "pinctrl");
986 dev_err(&pdev
->dev
, "failed to get alias id\n");
987 return ERR_PTR(-ENOENT
);
989 match
= of_match_node(samsung_pinctrl_dt_match
, node
);
990 ctrl
= (struct samsung_pin_ctrl
*)match
->data
+ id
;
992 d
->suspend
= ctrl
->suspend
;
993 d
->resume
= ctrl
->resume
;
994 d
->nr_banks
= ctrl
->nr_banks
;
995 d
->pin_banks
= devm_kcalloc(&pdev
->dev
, d
->nr_banks
,
996 sizeof(*d
->pin_banks
), GFP_KERNEL
);
998 return ERR_PTR(-ENOMEM
);
1000 bank
= d
->pin_banks
;
1001 bdata
= ctrl
->pin_banks
;
1002 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bdata
, ++bank
) {
1003 bank
->type
= bdata
->type
;
1004 bank
->pctl_offset
= bdata
->pctl_offset
;
1005 bank
->nr_pins
= bdata
->nr_pins
;
1006 bank
->eint_func
= bdata
->eint_func
;
1007 bank
->eint_type
= bdata
->eint_type
;
1008 bank
->eint_mask
= bdata
->eint_mask
;
1009 bank
->eint_offset
= bdata
->eint_offset
;
1010 bank
->name
= bdata
->name
;
1012 spin_lock_init(&bank
->slock
);
1014 bank
->pin_base
= d
->nr_pins
;
1015 d
->nr_pins
+= bank
->nr_pins
;
1018 for_each_child_of_node(node
, np
) {
1019 if (!of_find_property(np
, "gpio-controller", NULL
))
1021 bank
= d
->pin_banks
;
1022 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
1023 if (!strcmp(bank
->name
, np
->name
)) {
1030 d
->pin_base
= pin_base
;
1031 pin_base
+= d
->nr_pins
;
1036 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
1038 struct samsung_pinctrl_drv_data
*drvdata
;
1039 const struct samsung_pin_ctrl
*ctrl
;
1040 struct device
*dev
= &pdev
->dev
;
1041 struct resource
*res
;
1044 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
1048 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
1050 dev_err(&pdev
->dev
, "driver data not available\n");
1051 return PTR_ERR(ctrl
);
1055 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1056 drvdata
->virt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1057 if (IS_ERR(drvdata
->virt_base
))
1058 return PTR_ERR(drvdata
->virt_base
);
1060 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1062 drvdata
->irq
= res
->start
;
1064 ret
= samsung_gpiolib_register(pdev
, drvdata
);
1068 ret
= samsung_pinctrl_register(pdev
, drvdata
);
1070 samsung_gpiolib_unregister(pdev
, drvdata
);
1074 if (ctrl
->eint_gpio_init
)
1075 ctrl
->eint_gpio_init(drvdata
);
1076 if (ctrl
->eint_wkup_init
)
1077 ctrl
->eint_wkup_init(drvdata
);
1079 platform_set_drvdata(pdev
, drvdata
);
1081 /* Add to the global list */
1082 list_add_tail(&drvdata
->node
, &drvdata_list
);
1090 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device
1092 * Save data for all banks handled by this device.
1094 static void samsung_pinctrl_suspend_dev(
1095 struct samsung_pinctrl_drv_data
*drvdata
)
1097 void __iomem
*virt_base
= drvdata
->virt_base
;
1100 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1101 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1102 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1103 const u8
*offs
= bank
->type
->reg_offset
;
1104 const u8
*widths
= bank
->type
->fld_width
;
1105 enum pincfg_type type
;
1107 /* Registers without a powerdown config aren't lost */
1108 if (!widths
[PINCFG_TYPE_CON_PDN
])
1111 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1113 bank
->pm_save
[type
] = readl(reg
+ offs
[type
]);
1115 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1116 /* Some banks have two config registers */
1117 bank
->pm_save
[PINCFG_TYPE_NUM
] =
1118 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1119 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1121 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1122 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1124 pr_debug("Save %s @ %p (con %#010x)\n", bank
->name
,
1125 reg
, bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1129 if (drvdata
->suspend
)
1130 drvdata
->suspend(drvdata
);
1134 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device
1136 * Restore one of the banks that was saved during suspend.
1138 * We don't bother doing anything complicated to avoid glitching lines since
1139 * we're called before pad retention is turned off.
1141 static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data
*drvdata
)
1143 void __iomem
*virt_base
= drvdata
->virt_base
;
1146 if (drvdata
->resume
)
1147 drvdata
->resume(drvdata
);
1149 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1150 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1151 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1152 const u8
*offs
= bank
->type
->reg_offset
;
1153 const u8
*widths
= bank
->type
->fld_width
;
1154 enum pincfg_type type
;
1156 /* Registers without a powerdown config aren't lost */
1157 if (!widths
[PINCFG_TYPE_CON_PDN
])
1160 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1161 /* Some banks have two config registers */
1162 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1164 readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1165 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4),
1166 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1167 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1168 writel(bank
->pm_save
[PINCFG_TYPE_NUM
],
1169 reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1171 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank
->name
,
1172 reg
, readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1173 bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1175 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1177 writel(bank
->pm_save
[type
], reg
+ offs
[type
]);
1182 * samsung_pinctrl_suspend - save pinctrl state for suspend
1184 * Save data for all banks across all devices.
1186 static int samsung_pinctrl_suspend(void)
1188 struct samsung_pinctrl_drv_data
*drvdata
;
1190 list_for_each_entry(drvdata
, &drvdata_list
, node
) {
1191 samsung_pinctrl_suspend_dev(drvdata
);
1198 * samsung_pinctrl_resume - restore pinctrl state for suspend
1200 * Restore data for all banks across all devices.
1202 static void samsung_pinctrl_resume(void)
1204 struct samsung_pinctrl_drv_data
*drvdata
;
1206 list_for_each_entry_reverse(drvdata
, &drvdata_list
, node
) {
1207 samsung_pinctrl_resume_dev(drvdata
);
1212 #define samsung_pinctrl_suspend NULL
1213 #define samsung_pinctrl_resume NULL
1216 static struct syscore_ops samsung_pinctrl_syscore_ops
= {
1217 .suspend
= samsung_pinctrl_suspend
,
1218 .resume
= samsung_pinctrl_resume
,
1221 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
1222 #ifdef CONFIG_PINCTRL_EXYNOS
1223 { .compatible
= "samsung,exynos3250-pinctrl",
1224 .data
= (void *)exynos3250_pin_ctrl
},
1225 { .compatible
= "samsung,exynos4210-pinctrl",
1226 .data
= (void *)exynos4210_pin_ctrl
},
1227 { .compatible
= "samsung,exynos4x12-pinctrl",
1228 .data
= (void *)exynos4x12_pin_ctrl
},
1229 { .compatible
= "samsung,exynos4415-pinctrl",
1230 .data
= (void *)exynos4415_pin_ctrl
},
1231 { .compatible
= "samsung,exynos5250-pinctrl",
1232 .data
= (void *)exynos5250_pin_ctrl
},
1233 { .compatible
= "samsung,exynos5260-pinctrl",
1234 .data
= (void *)exynos5260_pin_ctrl
},
1235 { .compatible
= "samsung,exynos5410-pinctrl",
1236 .data
= (void *)exynos5410_pin_ctrl
},
1237 { .compatible
= "samsung,exynos5420-pinctrl",
1238 .data
= (void *)exynos5420_pin_ctrl
},
1239 { .compatible
= "samsung,exynos5433-pinctrl",
1240 .data
= (void *)exynos5433_pin_ctrl
},
1241 { .compatible
= "samsung,s5pv210-pinctrl",
1242 .data
= (void *)s5pv210_pin_ctrl
},
1243 { .compatible
= "samsung,exynos7-pinctrl",
1244 .data
= (void *)exynos7_pin_ctrl
},
1246 #ifdef CONFIG_PINCTRL_S3C64XX
1247 { .compatible
= "samsung,s3c64xx-pinctrl",
1248 .data
= s3c64xx_pin_ctrl
},
1250 #ifdef CONFIG_PINCTRL_S3C24XX
1251 { .compatible
= "samsung,s3c2412-pinctrl",
1252 .data
= s3c2412_pin_ctrl
},
1253 { .compatible
= "samsung,s3c2416-pinctrl",
1254 .data
= s3c2416_pin_ctrl
},
1255 { .compatible
= "samsung,s3c2440-pinctrl",
1256 .data
= s3c2440_pin_ctrl
},
1257 { .compatible
= "samsung,s3c2450-pinctrl",
1258 .data
= s3c2450_pin_ctrl
},
1262 MODULE_DEVICE_TABLE(of
, samsung_pinctrl_dt_match
);
1264 static struct platform_driver samsung_pinctrl_driver
= {
1265 .probe
= samsung_pinctrl_probe
,
1267 .name
= "samsung-pinctrl",
1268 .of_match_table
= samsung_pinctrl_dt_match
,
1269 .suppress_bind_attrs
= true,
1273 static int __init
samsung_pinctrl_drv_register(void)
1276 * Register syscore ops for save/restore of registers across suspend.
1277 * It's important to ensure that this driver is running at an earlier
1278 * initcall level than any arch-specific init calls that install syscore
1279 * ops that turn off pad retention (like exynos_pm_resume).
1281 register_syscore_ops(&samsung_pinctrl_syscore_ops
);
1283 return platform_driver_register(&samsung_pinctrl_driver
);
1285 postcore_initcall(samsung_pinctrl_drv_register
);
1287 static void __exit
samsung_pinctrl_drv_unregister(void)
1289 platform_driver_unregister(&samsung_pinctrl_driver
);
1291 module_exit(samsung_pinctrl_drv_unregister
);
1293 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1294 MODULE_DESCRIPTION("Samsung pinctrl driver");
1295 MODULE_LICENSE("GPL v2");