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
,
517 /* gpiolib gpio_set callback function */
518 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
520 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
521 const struct samsung_pin_bank_type
*type
= bank
->type
;
526 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
528 spin_lock_irqsave(&bank
->slock
, flags
);
530 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
531 data
&= ~(1 << offset
);
534 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
536 spin_unlock_irqrestore(&bank
->slock
, flags
);
539 /* gpiolib gpio_get callback function */
540 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
544 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
545 const struct samsung_pin_bank_type
*type
= bank
->type
;
547 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
549 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
556 * The calls to gpio_direction_output() and gpio_direction_input()
557 * leads to this function call.
559 static int samsung_gpio_set_direction(struct gpio_chip
*gc
,
560 unsigned offset
, bool input
)
562 const struct samsung_pin_bank_type
*type
;
563 struct samsung_pin_bank
*bank
;
564 struct samsung_pinctrl_drv_data
*drvdata
;
566 u32 data
, mask
, shift
;
569 bank
= gpiochip_get_data(gc
);
571 drvdata
= bank
->drvdata
;
573 reg
= drvdata
->virt_base
+ bank
->pctl_offset
+
574 type
->reg_offset
[PINCFG_TYPE_FUNC
];
576 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
577 shift
= offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
579 /* Some banks have two config registers */
584 spin_lock_irqsave(&bank
->slock
, flags
);
587 data
&= ~(mask
<< shift
);
589 data
|= FUNC_OUTPUT
<< shift
;
592 spin_unlock_irqrestore(&bank
->slock
, flags
);
597 /* gpiolib gpio_direction_input callback function. */
598 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
600 return samsung_gpio_set_direction(gc
, offset
, true);
603 /* gpiolib gpio_direction_output callback function. */
604 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
607 samsung_gpio_set(gc
, offset
, value
);
608 return samsung_gpio_set_direction(gc
, offset
, false);
612 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
613 * and a virtual IRQ, if not already present.
615 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
617 struct samsung_pin_bank
*bank
= gpiochip_get_data(gc
);
620 if (!bank
->irq_domain
)
623 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
625 return (virq
) ? : -ENXIO
;
628 static struct samsung_pin_group
*samsung_pinctrl_create_groups(
630 struct samsung_pinctrl_drv_data
*drvdata
,
633 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
634 struct samsung_pin_group
*groups
, *grp
;
635 const struct pinctrl_pin_desc
*pdesc
;
638 groups
= devm_kzalloc(dev
, ctrldesc
->npins
* sizeof(*groups
),
641 return ERR_PTR(-EINVAL
);
644 pdesc
= ctrldesc
->pins
;
645 for (i
= 0; i
< ctrldesc
->npins
; ++i
, ++pdesc
, ++grp
) {
646 grp
->name
= pdesc
->name
;
647 grp
->pins
= &pdesc
->number
;
651 *cnt
= ctrldesc
->npins
;
655 static int samsung_pinctrl_create_function(struct device
*dev
,
656 struct samsung_pinctrl_drv_data
*drvdata
,
657 struct device_node
*func_np
,
658 struct samsung_pmx_func
*func
)
664 if (of_property_read_u32(func_np
, "samsung,pin-function", &func
->val
))
667 npins
= of_property_count_strings(func_np
, "samsung,pins");
669 dev_err(dev
, "invalid pin list in %s node", func_np
->name
);
673 func
->name
= func_np
->full_name
;
675 func
->groups
= devm_kzalloc(dev
, npins
* sizeof(char *), GFP_KERNEL
);
679 for (i
= 0; i
< npins
; ++i
) {
682 ret
= of_property_read_string_index(func_np
, "samsung,pins",
686 "failed to read pin name %d from %s node\n",
691 func
->groups
[i
] = gname
;
694 func
->num_groups
= npins
;
698 static struct samsung_pmx_func
*samsung_pinctrl_create_functions(
700 struct samsung_pinctrl_drv_data
*drvdata
,
703 struct samsung_pmx_func
*functions
, *func
;
704 struct device_node
*dev_np
= dev
->of_node
;
705 struct device_node
*cfg_np
;
706 unsigned int func_cnt
= 0;
710 * Iterate over all the child nodes of the pin controller node
711 * and create pin groups and pin function lists.
713 for_each_child_of_node(dev_np
, cfg_np
) {
714 struct device_node
*func_np
;
716 if (!of_get_child_count(cfg_np
)) {
717 if (!of_find_property(cfg_np
,
718 "samsung,pin-function", NULL
))
724 for_each_child_of_node(cfg_np
, func_np
) {
725 if (!of_find_property(func_np
,
726 "samsung,pin-function", NULL
))
732 functions
= devm_kzalloc(dev
, func_cnt
* sizeof(*functions
),
735 dev_err(dev
, "failed to allocate memory for function list\n");
736 return ERR_PTR(-EINVAL
);
741 * Iterate over all the child nodes of the pin controller node
742 * and create pin groups and pin function lists.
745 for_each_child_of_node(dev_np
, cfg_np
) {
746 struct device_node
*func_np
;
748 if (!of_get_child_count(cfg_np
)) {
749 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
760 for_each_child_of_node(cfg_np
, func_np
) {
761 ret
= samsung_pinctrl_create_function(dev
, drvdata
,
777 * Parse the information about all the available pin groups and pin functions
778 * from device node of the pin-controller. A pin group is formed with all
779 * the pins listed in the "samsung,pins" property.
782 static int samsung_pinctrl_parse_dt(struct platform_device
*pdev
,
783 struct samsung_pinctrl_drv_data
*drvdata
)
785 struct device
*dev
= &pdev
->dev
;
786 struct samsung_pin_group
*groups
;
787 struct samsung_pmx_func
*functions
;
788 unsigned int grp_cnt
= 0, func_cnt
= 0;
790 groups
= samsung_pinctrl_create_groups(dev
, drvdata
, &grp_cnt
);
791 if (IS_ERR(groups
)) {
792 dev_err(dev
, "failed to parse pin groups\n");
793 return PTR_ERR(groups
);
796 functions
= samsung_pinctrl_create_functions(dev
, drvdata
, &func_cnt
);
797 if (IS_ERR(functions
)) {
798 dev_err(dev
, "failed to parse pin functions\n");
799 return PTR_ERR(functions
);
802 drvdata
->pin_groups
= groups
;
803 drvdata
->nr_groups
= grp_cnt
;
804 drvdata
->pmx_functions
= functions
;
805 drvdata
->nr_functions
= func_cnt
;
810 /* register the pinctrl interface with the pinctrl subsystem */
811 static int samsung_pinctrl_register(struct platform_device
*pdev
,
812 struct samsung_pinctrl_drv_data
*drvdata
)
814 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
815 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
816 struct samsung_pin_bank
*pin_bank
;
820 ctrldesc
->name
= "samsung-pinctrl";
821 ctrldesc
->owner
= THIS_MODULE
;
822 ctrldesc
->pctlops
= &samsung_pctrl_ops
;
823 ctrldesc
->pmxops
= &samsung_pinmux_ops
;
824 ctrldesc
->confops
= &samsung_pinconf_ops
;
826 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
827 drvdata
->nr_pins
, GFP_KERNEL
);
829 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
832 ctrldesc
->pins
= pindesc
;
833 ctrldesc
->npins
= drvdata
->nr_pins
;
835 /* dynamically populate the pin number and pin name for pindesc */
836 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
837 pdesc
->number
= pin
+ drvdata
->pin_base
;
840 * allocate space for storing the dynamically generated names for all
841 * the pins which belong to this pin-controller.
843 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
844 drvdata
->nr_pins
, GFP_KERNEL
);
846 dev_err(&pdev
->dev
, "mem alloc for pin names failed\n");
850 /* for each pin, the name of the pin is pin-bank name + pin number */
851 for (bank
= 0; bank
< drvdata
->nr_banks
; bank
++) {
852 pin_bank
= &drvdata
->pin_banks
[bank
];
853 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
854 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
855 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
856 pdesc
->name
= pin_names
;
857 pin_names
+= PIN_NAME_LENGTH
;
861 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
865 drvdata
->pctl_dev
= pinctrl_register(ctrldesc
, &pdev
->dev
, drvdata
);
866 if (IS_ERR(drvdata
->pctl_dev
)) {
867 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
868 return PTR_ERR(drvdata
->pctl_dev
);
871 for (bank
= 0; bank
< drvdata
->nr_banks
; ++bank
) {
872 pin_bank
= &drvdata
->pin_banks
[bank
];
873 pin_bank
->grange
.name
= pin_bank
->name
;
874 pin_bank
->grange
.id
= bank
;
875 pin_bank
->grange
.pin_base
= drvdata
->pin_base
876 + pin_bank
->pin_base
;
877 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
878 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
879 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
880 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
886 static const struct gpio_chip samsung_gpiolib_chip
= {
887 .request
= gpiochip_generic_request
,
888 .free
= gpiochip_generic_free
,
889 .set
= samsung_gpio_set
,
890 .get
= samsung_gpio_get
,
891 .direction_input
= samsung_gpio_direction_input
,
892 .direction_output
= samsung_gpio_direction_output
,
893 .to_irq
= samsung_gpio_to_irq
,
894 .owner
= THIS_MODULE
,
897 /* register the gpiolib interface with the gpiolib subsystem */
898 static int samsung_gpiolib_register(struct platform_device
*pdev
,
899 struct samsung_pinctrl_drv_data
*drvdata
)
901 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
902 struct gpio_chip
*gc
;
906 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
) {
907 bank
->gpio_chip
= samsung_gpiolib_chip
;
909 gc
= &bank
->gpio_chip
;
910 gc
->base
= drvdata
->pin_base
+ bank
->pin_base
;
911 gc
->ngpio
= bank
->nr_pins
;
912 gc
->parent
= &pdev
->dev
;
913 gc
->of_node
= bank
->of_node
;
914 gc
->label
= bank
->name
;
916 ret
= gpiochip_add_data(gc
, bank
);
918 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
927 for (--i
, --bank
; i
>= 0; --i
, --bank
)
928 gpiochip_remove(&bank
->gpio_chip
);
932 /* unregister the gpiolib interface with the gpiolib subsystem */
933 static int samsung_gpiolib_unregister(struct platform_device
*pdev
,
934 struct samsung_pinctrl_drv_data
*drvdata
)
936 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
939 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
940 gpiochip_remove(&bank
->gpio_chip
);
945 static const struct of_device_id samsung_pinctrl_dt_match
[];
947 /* retrieve the soc specific data */
948 static const struct samsung_pin_ctrl
*
949 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data
*d
,
950 struct platform_device
*pdev
)
953 const struct of_device_id
*match
;
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
;
961 id
= of_alias_get_id(node
, "pinctrl");
963 dev_err(&pdev
->dev
, "failed to get alias id\n");
964 return ERR_PTR(-ENOENT
);
966 match
= of_match_node(samsung_pinctrl_dt_match
, node
);
967 ctrl
= (struct samsung_pin_ctrl
*)match
->data
+ id
;
969 d
->suspend
= ctrl
->suspend
;
970 d
->resume
= ctrl
->resume
;
971 d
->nr_banks
= ctrl
->nr_banks
;
972 d
->pin_banks
= devm_kcalloc(&pdev
->dev
, d
->nr_banks
,
973 sizeof(*d
->pin_banks
), GFP_KERNEL
);
975 return ERR_PTR(-ENOMEM
);
978 bdata
= ctrl
->pin_banks
;
979 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bdata
, ++bank
) {
980 bank
->type
= bdata
->type
;
981 bank
->pctl_offset
= bdata
->pctl_offset
;
982 bank
->nr_pins
= bdata
->nr_pins
;
983 bank
->eint_func
= bdata
->eint_func
;
984 bank
->eint_type
= bdata
->eint_type
;
985 bank
->eint_mask
= bdata
->eint_mask
;
986 bank
->eint_offset
= bdata
->eint_offset
;
987 bank
->name
= bdata
->name
;
989 spin_lock_init(&bank
->slock
);
991 bank
->pin_base
= d
->nr_pins
;
992 d
->nr_pins
+= bank
->nr_pins
;
995 for_each_child_of_node(node
, np
) {
996 if (!of_find_property(np
, "gpio-controller", NULL
))
999 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
1000 if (!strcmp(bank
->name
, np
->name
)) {
1007 d
->pin_base
= pin_base
;
1008 pin_base
+= d
->nr_pins
;
1013 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
1015 struct samsung_pinctrl_drv_data
*drvdata
;
1016 const struct samsung_pin_ctrl
*ctrl
;
1017 struct device
*dev
= &pdev
->dev
;
1018 struct resource
*res
;
1021 if (!dev
->of_node
) {
1022 dev_err(dev
, "device tree node not found\n");
1026 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
1028 dev_err(dev
, "failed to allocate memory for driver's "
1033 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
1035 dev_err(&pdev
->dev
, "driver data not available\n");
1036 return PTR_ERR(ctrl
);
1040 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1041 drvdata
->virt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1042 if (IS_ERR(drvdata
->virt_base
))
1043 return PTR_ERR(drvdata
->virt_base
);
1045 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1047 drvdata
->irq
= res
->start
;
1049 ret
= samsung_gpiolib_register(pdev
, drvdata
);
1053 ret
= samsung_pinctrl_register(pdev
, drvdata
);
1055 samsung_gpiolib_unregister(pdev
, drvdata
);
1059 if (ctrl
->eint_gpio_init
)
1060 ctrl
->eint_gpio_init(drvdata
);
1061 if (ctrl
->eint_wkup_init
)
1062 ctrl
->eint_wkup_init(drvdata
);
1064 platform_set_drvdata(pdev
, drvdata
);
1066 /* Add to the global list */
1067 list_add_tail(&drvdata
->node
, &drvdata_list
);
1075 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device
1077 * Save data for all banks handled by this device.
1079 static void samsung_pinctrl_suspend_dev(
1080 struct samsung_pinctrl_drv_data
*drvdata
)
1082 void __iomem
*virt_base
= drvdata
->virt_base
;
1085 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1086 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1087 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1088 const u8
*offs
= bank
->type
->reg_offset
;
1089 const u8
*widths
= bank
->type
->fld_width
;
1090 enum pincfg_type type
;
1092 /* Registers without a powerdown config aren't lost */
1093 if (!widths
[PINCFG_TYPE_CON_PDN
])
1096 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1098 bank
->pm_save
[type
] = readl(reg
+ offs
[type
]);
1100 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1101 /* Some banks have two config registers */
1102 bank
->pm_save
[PINCFG_TYPE_NUM
] =
1103 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1104 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1106 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1107 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1109 pr_debug("Save %s @ %p (con %#010x)\n", bank
->name
,
1110 reg
, bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1114 if (drvdata
->suspend
)
1115 drvdata
->suspend(drvdata
);
1119 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device
1121 * Restore one of the banks that was saved during suspend.
1123 * We don't bother doing anything complicated to avoid glitching lines since
1124 * we're called before pad retention is turned off.
1126 static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data
*drvdata
)
1128 void __iomem
*virt_base
= drvdata
->virt_base
;
1131 if (drvdata
->resume
)
1132 drvdata
->resume(drvdata
);
1134 for (i
= 0; i
< drvdata
->nr_banks
; i
++) {
1135 struct samsung_pin_bank
*bank
= &drvdata
->pin_banks
[i
];
1136 void __iomem
*reg
= virt_base
+ bank
->pctl_offset
;
1137 const u8
*offs
= bank
->type
->reg_offset
;
1138 const u8
*widths
= bank
->type
->fld_width
;
1139 enum pincfg_type type
;
1141 /* Registers without a powerdown config aren't lost */
1142 if (!widths
[PINCFG_TYPE_CON_PDN
])
1145 if (widths
[PINCFG_TYPE_FUNC
] * bank
->nr_pins
> 32) {
1146 /* Some banks have two config registers */
1147 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1149 readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1150 readl(reg
+ offs
[PINCFG_TYPE_FUNC
] + 4),
1151 bank
->pm_save
[PINCFG_TYPE_FUNC
],
1152 bank
->pm_save
[PINCFG_TYPE_NUM
]);
1153 writel(bank
->pm_save
[PINCFG_TYPE_NUM
],
1154 reg
+ offs
[PINCFG_TYPE_FUNC
] + 4);
1156 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank
->name
,
1157 reg
, readl(reg
+ offs
[PINCFG_TYPE_FUNC
]),
1158 bank
->pm_save
[PINCFG_TYPE_FUNC
]);
1160 for (type
= 0; type
< PINCFG_TYPE_NUM
; type
++)
1162 writel(bank
->pm_save
[type
], reg
+ offs
[type
]);
1167 * samsung_pinctrl_suspend - save pinctrl state for suspend
1169 * Save data for all banks across all devices.
1171 static int samsung_pinctrl_suspend(void)
1173 struct samsung_pinctrl_drv_data
*drvdata
;
1175 list_for_each_entry(drvdata
, &drvdata_list
, node
) {
1176 samsung_pinctrl_suspend_dev(drvdata
);
1183 * samsung_pinctrl_resume - restore pinctrl state for suspend
1185 * Restore data for all banks across all devices.
1187 static void samsung_pinctrl_resume(void)
1189 struct samsung_pinctrl_drv_data
*drvdata
;
1191 list_for_each_entry_reverse(drvdata
, &drvdata_list
, node
) {
1192 samsung_pinctrl_resume_dev(drvdata
);
1197 #define samsung_pinctrl_suspend NULL
1198 #define samsung_pinctrl_resume NULL
1201 static struct syscore_ops samsung_pinctrl_syscore_ops
= {
1202 .suspend
= samsung_pinctrl_suspend
,
1203 .resume
= samsung_pinctrl_resume
,
1206 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
1207 #ifdef CONFIG_PINCTRL_EXYNOS
1208 { .compatible
= "samsung,exynos3250-pinctrl",
1209 .data
= (void *)exynos3250_pin_ctrl
},
1210 { .compatible
= "samsung,exynos4210-pinctrl",
1211 .data
= (void *)exynos4210_pin_ctrl
},
1212 { .compatible
= "samsung,exynos4x12-pinctrl",
1213 .data
= (void *)exynos4x12_pin_ctrl
},
1214 { .compatible
= "samsung,exynos4415-pinctrl",
1215 .data
= (void *)exynos4415_pin_ctrl
},
1216 { .compatible
= "samsung,exynos5250-pinctrl",
1217 .data
= (void *)exynos5250_pin_ctrl
},
1218 { .compatible
= "samsung,exynos5260-pinctrl",
1219 .data
= (void *)exynos5260_pin_ctrl
},
1220 { .compatible
= "samsung,exynos5420-pinctrl",
1221 .data
= (void *)exynos5420_pin_ctrl
},
1222 { .compatible
= "samsung,exynos5433-pinctrl",
1223 .data
= (void *)exynos5433_pin_ctrl
},
1224 { .compatible
= "samsung,s5pv210-pinctrl",
1225 .data
= (void *)s5pv210_pin_ctrl
},
1226 { .compatible
= "samsung,exynos7-pinctrl",
1227 .data
= (void *)exynos7_pin_ctrl
},
1229 #ifdef CONFIG_PINCTRL_S3C64XX
1230 { .compatible
= "samsung,s3c64xx-pinctrl",
1231 .data
= s3c64xx_pin_ctrl
},
1233 #ifdef CONFIG_PINCTRL_S3C24XX
1234 { .compatible
= "samsung,s3c2412-pinctrl",
1235 .data
= s3c2412_pin_ctrl
},
1236 { .compatible
= "samsung,s3c2416-pinctrl",
1237 .data
= s3c2416_pin_ctrl
},
1238 { .compatible
= "samsung,s3c2440-pinctrl",
1239 .data
= s3c2440_pin_ctrl
},
1240 { .compatible
= "samsung,s3c2450-pinctrl",
1241 .data
= s3c2450_pin_ctrl
},
1245 MODULE_DEVICE_TABLE(of
, samsung_pinctrl_dt_match
);
1247 static struct platform_driver samsung_pinctrl_driver
= {
1248 .probe
= samsung_pinctrl_probe
,
1250 .name
= "samsung-pinctrl",
1251 .of_match_table
= samsung_pinctrl_dt_match
,
1255 static int __init
samsung_pinctrl_drv_register(void)
1258 * Register syscore ops for save/restore of registers across suspend.
1259 * It's important to ensure that this driver is running at an earlier
1260 * initcall level than any arch-specific init calls that install syscore
1261 * ops that turn off pad retention (like exynos_pm_resume).
1263 register_syscore_ops(&samsung_pinctrl_syscore_ops
);
1265 return platform_driver_register(&samsung_pinctrl_driver
);
1267 postcore_initcall(samsung_pinctrl_drv_register
);
1269 static void __exit
samsung_pinctrl_drv_unregister(void)
1271 platform_driver_unregister(&samsung_pinctrl_driver
);
1273 module_exit(samsung_pinctrl_drv_unregister
);
1275 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1276 MODULE_DESCRIPTION("Samsung pinctrl driver");
1277 MODULE_LICENSE("GPL v2");