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>
33 #include "pinctrl-samsung.h"
35 #define GROUP_SUFFIX "-grp"
36 #define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
37 #define FUNCTION_SUFFIX "-mux"
38 #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
40 /* list of all possible config options supported */
41 static struct pin_config
{
43 unsigned int cfg_type
;
45 { "samsung,pin-pud", PINCFG_TYPE_PUD
},
46 { "samsung,pin-drv", PINCFG_TYPE_DRV
},
47 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN
},
48 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN
},
51 static unsigned int pin_base
;
53 static inline struct samsung_pin_bank
*gc_to_pin_bank(struct gpio_chip
*gc
)
55 return container_of(gc
, struct samsung_pin_bank
, gpio_chip
);
58 /* check if the selector is a valid pin group selector */
59 static int samsung_get_group_count(struct pinctrl_dev
*pctldev
)
61 struct samsung_pinctrl_drv_data
*drvdata
;
63 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
64 return drvdata
->nr_groups
;
67 /* return the name of the group selected by the group selector */
68 static const char *samsung_get_group_name(struct pinctrl_dev
*pctldev
,
71 struct samsung_pinctrl_drv_data
*drvdata
;
73 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
74 return drvdata
->pin_groups
[selector
].name
;
77 /* return the pin numbers associated with the specified group */
78 static int samsung_get_group_pins(struct pinctrl_dev
*pctldev
,
79 unsigned selector
, const unsigned **pins
, unsigned *num_pins
)
81 struct samsung_pinctrl_drv_data
*drvdata
;
83 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
84 *pins
= drvdata
->pin_groups
[selector
].pins
;
85 *num_pins
= drvdata
->pin_groups
[selector
].num_pins
;
89 /* create pinctrl_map entries by parsing device tree nodes */
90 static int samsung_dt_node_to_map(struct pinctrl_dev
*pctldev
,
91 struct device_node
*np
, struct pinctrl_map
**maps
,
94 struct device
*dev
= pctldev
->dev
;
95 struct pinctrl_map
*map
;
96 unsigned long *cfg
= NULL
;
98 int cfg_cnt
= 0, map_cnt
= 0, idx
= 0;
100 /* count the number of config options specfied in the node */
101 for (idx
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++) {
102 if (of_find_property(np
, pcfgs
[idx
].prop_cfg
, NULL
))
107 * Find out the number of map entries to create. All the config options
108 * can be accomadated into a single config map entry.
112 if (of_find_property(np
, "samsung,pin-function", NULL
))
115 dev_err(dev
, "node %s does not have either config or function "
116 "configurations\n", np
->name
);
120 /* Allocate memory for pin-map entries */
121 map
= kzalloc(sizeof(*map
) * map_cnt
, GFP_KERNEL
);
123 dev_err(dev
, "could not alloc memory for pin-maps\n");
129 * Allocate memory for pin group name. The pin group name is derived
130 * from the node name from which these map entries are be created.
132 gname
= kzalloc(strlen(np
->name
) + GSUFFIX_LEN
, GFP_KERNEL
);
134 dev_err(dev
, "failed to alloc memory for group name\n");
137 sprintf(gname
, "%s%s", np
->name
, GROUP_SUFFIX
);
140 * don't have config options? then skip over to creating function
146 /* Allocate memory for config entries */
147 cfg
= kzalloc(sizeof(*cfg
) * cfg_cnt
, GFP_KERNEL
);
149 dev_err(dev
, "failed to alloc memory for configs\n");
153 /* Prepare a list of config settings */
154 for (idx
= 0, cfg_cnt
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++) {
156 if (!of_property_read_u32(np
, pcfgs
[idx
].prop_cfg
, &value
))
158 PINCFG_PACK(pcfgs
[idx
].cfg_type
, value
);
161 /* create the config map entry */
162 map
[*nmaps
].data
.configs
.group_or_pin
= gname
;
163 map
[*nmaps
].data
.configs
.configs
= cfg
;
164 map
[*nmaps
].data
.configs
.num_configs
= cfg_cnt
;
165 map
[*nmaps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
169 /* create the function map entry */
170 if (of_find_property(np
, "samsung,pin-function", NULL
)) {
171 fname
= kzalloc(strlen(np
->name
) + FSUFFIX_LEN
, GFP_KERNEL
);
173 dev_err(dev
, "failed to alloc memory for func name\n");
176 sprintf(fname
, "%s%s", np
->name
, FUNCTION_SUFFIX
);
178 map
[*nmaps
].data
.mux
.group
= gname
;
179 map
[*nmaps
].data
.mux
.function
= fname
;
180 map
[*nmaps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
196 /* free the memory allocated to hold the pin-map table */
197 static void samsung_dt_free_map(struct pinctrl_dev
*pctldev
,
198 struct pinctrl_map
*map
, unsigned num_maps
)
202 for (idx
= 0; idx
< num_maps
; idx
++) {
203 if (map
[idx
].type
== PIN_MAP_TYPE_MUX_GROUP
) {
204 kfree(map
[idx
].data
.mux
.function
);
206 kfree(map
[idx
].data
.mux
.group
);
207 } else if (map
->type
== PIN_MAP_TYPE_CONFIGS_GROUP
) {
208 kfree(map
[idx
].data
.configs
.configs
);
210 kfree(map
[idx
].data
.configs
.group_or_pin
);
217 /* list of pinctrl callbacks for the pinctrl core */
218 static const struct pinctrl_ops samsung_pctrl_ops
= {
219 .get_groups_count
= samsung_get_group_count
,
220 .get_group_name
= samsung_get_group_name
,
221 .get_group_pins
= samsung_get_group_pins
,
222 .dt_node_to_map
= samsung_dt_node_to_map
,
223 .dt_free_map
= samsung_dt_free_map
,
226 /* check if the selector is a valid pin function selector */
227 static int samsung_get_functions_count(struct pinctrl_dev
*pctldev
)
229 struct samsung_pinctrl_drv_data
*drvdata
;
231 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
232 return drvdata
->nr_functions
;
235 /* return the name of the pin function specified */
236 static const char *samsung_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
239 struct samsung_pinctrl_drv_data
*drvdata
;
241 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
242 return drvdata
->pmx_functions
[selector
].name
;
245 /* return the groups associated for the specified function selector */
246 static int samsung_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
247 unsigned selector
, const char * const **groups
,
248 unsigned * const num_groups
)
250 struct samsung_pinctrl_drv_data
*drvdata
;
252 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
253 *groups
= drvdata
->pmx_functions
[selector
].groups
;
254 *num_groups
= drvdata
->pmx_functions
[selector
].num_groups
;
259 * given a pin number that is local to a pin controller, find out the pin bank
260 * and the register base of the pin bank.
262 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data
*drvdata
,
263 unsigned pin
, void __iomem
**reg
, u32
*offset
,
264 struct samsung_pin_bank
**bank
)
266 struct samsung_pin_bank
*b
;
268 b
= drvdata
->ctrl
->pin_banks
;
270 while ((pin
>= b
->pin_base
) &&
271 ((b
->pin_base
+ b
->nr_pins
- 1) < pin
))
274 *reg
= drvdata
->virt_base
+ b
->pctl_offset
;
275 *offset
= pin
- b
->pin_base
;
280 /* enable or disable a pinmux function */
281 static void samsung_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
282 unsigned group
, bool enable
)
284 struct samsung_pinctrl_drv_data
*drvdata
;
285 const unsigned int *pins
;
286 struct samsung_pin_bank
*bank
;
288 u32 mask
, shift
, data
, pin_offset
, cnt
;
291 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
292 pins
= drvdata
->pin_groups
[group
].pins
;
295 * for each pin in the pin group selected, program the correspoding pin
296 * pin function number in the config register.
298 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++) {
299 struct samsung_pin_bank_type
*type
;
301 pin_to_reg_bank(drvdata
, pins
[cnt
] - drvdata
->ctrl
->base
,
302 ®
, &pin_offset
, &bank
);
304 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
305 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
307 /* Some banks have two config registers */
312 spin_lock_irqsave(&bank
->slock
, flags
);
314 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
315 data
&= ~(mask
<< shift
);
317 data
|= drvdata
->pin_groups
[group
].func
<< shift
;
318 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_FUNC
]);
320 spin_unlock_irqrestore(&bank
->slock
, flags
);
324 /* enable a specified pinmux by writing to registers */
325 static int samsung_pinmux_enable(struct pinctrl_dev
*pctldev
, unsigned selector
,
328 samsung_pinmux_setup(pctldev
, selector
, group
, true);
332 /* disable a specified pinmux by writing to registers */
333 static void samsung_pinmux_disable(struct pinctrl_dev
*pctldev
,
334 unsigned selector
, unsigned group
)
336 samsung_pinmux_setup(pctldev
, selector
, group
, false);
340 * The calls to gpio_direction_output() and gpio_direction_input()
341 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
342 * function called from the gpiolib interface).
344 static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev
*pctldev
,
345 struct pinctrl_gpio_range
*range
, unsigned offset
, bool input
)
347 struct samsung_pin_bank_type
*type
;
348 struct samsung_pin_bank
*bank
;
349 struct samsung_pinctrl_drv_data
*drvdata
;
351 u32 data
, pin_offset
, mask
, shift
;
354 bank
= gc_to_pin_bank(range
->gc
);
356 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
358 pin_offset
= offset
- bank
->pin_base
;
359 reg
= drvdata
->virt_base
+ bank
->pctl_offset
+
360 type
->reg_offset
[PINCFG_TYPE_FUNC
];
362 mask
= (1 << type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
363 shift
= pin_offset
* type
->fld_width
[PINCFG_TYPE_FUNC
];
365 /* Some banks have two config registers */
370 spin_lock_irqsave(&bank
->slock
, flags
);
373 data
&= ~(mask
<< shift
);
375 data
|= FUNC_OUTPUT
<< shift
;
378 spin_unlock_irqrestore(&bank
->slock
, flags
);
383 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
384 static const struct pinmux_ops samsung_pinmux_ops
= {
385 .get_functions_count
= samsung_get_functions_count
,
386 .get_function_name
= samsung_pinmux_get_fname
,
387 .get_function_groups
= samsung_pinmux_get_groups
,
388 .enable
= samsung_pinmux_enable
,
389 .disable
= samsung_pinmux_disable
,
390 .gpio_set_direction
= samsung_pinmux_gpio_set_direction
,
393 /* set or get the pin config settings for a specified pin */
394 static int samsung_pinconf_rw(struct pinctrl_dev
*pctldev
, unsigned int pin
,
395 unsigned long *config
, bool set
)
397 struct samsung_pinctrl_drv_data
*drvdata
;
398 struct samsung_pin_bank_type
*type
;
399 struct samsung_pin_bank
*bank
;
400 void __iomem
*reg_base
;
401 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
402 u32 data
, width
, pin_offset
, mask
, shift
;
403 u32 cfg_value
, cfg_reg
;
406 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
407 pin_to_reg_bank(drvdata
, pin
- drvdata
->ctrl
->base
, ®_base
,
411 if (cfg_type
>= PINCFG_TYPE_NUM
|| !type
->fld_width
[cfg_type
])
414 width
= type
->fld_width
[cfg_type
];
415 cfg_reg
= type
->reg_offset
[cfg_type
];
417 spin_lock_irqsave(&bank
->slock
, flags
);
419 mask
= (1 << width
) - 1;
420 shift
= pin_offset
* width
;
421 data
= readl(reg_base
+ cfg_reg
);
424 cfg_value
= PINCFG_UNPACK_VALUE(*config
);
425 data
&= ~(mask
<< shift
);
426 data
|= (cfg_value
<< shift
);
427 writel(data
, reg_base
+ cfg_reg
);
431 *config
= PINCFG_PACK(cfg_type
, data
);
434 spin_unlock_irqrestore(&bank
->slock
, flags
);
439 /* set the pin config settings for a specified pin */
440 static int samsung_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
441 unsigned long config
)
443 return samsung_pinconf_rw(pctldev
, pin
, &config
, true);
446 /* get the pin config settings for a specified pin */
447 static int samsung_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
448 unsigned long *config
)
450 return samsung_pinconf_rw(pctldev
, pin
, config
, false);
453 /* set the pin config settings for a specified pin group */
454 static int samsung_pinconf_group_set(struct pinctrl_dev
*pctldev
,
455 unsigned group
, unsigned long config
)
457 struct samsung_pinctrl_drv_data
*drvdata
;
458 const unsigned int *pins
;
461 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
462 pins
= drvdata
->pin_groups
[group
].pins
;
464 for (cnt
= 0; cnt
< drvdata
->pin_groups
[group
].num_pins
; cnt
++)
465 samsung_pinconf_set(pctldev
, pins
[cnt
], config
);
470 /* get the pin config settings for a specified pin group */
471 static int samsung_pinconf_group_get(struct pinctrl_dev
*pctldev
,
472 unsigned int group
, unsigned long *config
)
474 struct samsung_pinctrl_drv_data
*drvdata
;
475 const unsigned int *pins
;
477 drvdata
= pinctrl_dev_get_drvdata(pctldev
);
478 pins
= drvdata
->pin_groups
[group
].pins
;
479 samsung_pinconf_get(pctldev
, pins
[0], config
);
483 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
484 static const struct pinconf_ops samsung_pinconf_ops
= {
485 .pin_config_get
= samsung_pinconf_get
,
486 .pin_config_set
= samsung_pinconf_set
,
487 .pin_config_group_get
= samsung_pinconf_group_get
,
488 .pin_config_group_set
= samsung_pinconf_group_set
,
491 /* gpiolib gpio_set callback function */
492 static void samsung_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
494 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
495 struct samsung_pin_bank_type
*type
= bank
->type
;
500 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
502 spin_lock_irqsave(&bank
->slock
, flags
);
504 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
505 data
&= ~(1 << offset
);
508 writel(data
, reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
510 spin_unlock_irqrestore(&bank
->slock
, flags
);
513 /* gpiolib gpio_get callback function */
514 static int samsung_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
518 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
519 struct samsung_pin_bank_type
*type
= bank
->type
;
521 reg
= bank
->drvdata
->virt_base
+ bank
->pctl_offset
;
523 data
= readl(reg
+ type
->reg_offset
[PINCFG_TYPE_DAT
]);
530 * gpiolib gpio_direction_input callback function. The setting of the pin
531 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
534 static int samsung_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
536 return pinctrl_gpio_direction_input(gc
->base
+ offset
);
540 * gpiolib gpio_direction_output callback function. The setting of the pin
541 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
544 static int samsung_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
547 samsung_gpio_set(gc
, offset
, value
);
548 return pinctrl_gpio_direction_output(gc
->base
+ offset
);
552 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
553 * and a virtual IRQ, if not already present.
555 static int samsung_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
557 struct samsung_pin_bank
*bank
= gc_to_pin_bank(gc
);
560 if (!bank
->irq_domain
)
563 virq
= irq_create_mapping(bank
->irq_domain
, offset
);
565 return (virq
) ? : -ENXIO
;
569 * Parse the pin names listed in the 'samsung,pins' property and convert it
570 * into a list of gpio numbers are create a pin group from it.
572 static int samsung_pinctrl_parse_dt_pins(struct platform_device
*pdev
,
573 struct device_node
*cfg_np
,
574 struct pinctrl_desc
*pctl
,
575 unsigned int **pin_list
,
578 struct device
*dev
= &pdev
->dev
;
579 struct property
*prop
;
580 struct pinctrl_pin_desc
const *pdesc
= pctl
->pins
;
581 unsigned int idx
= 0, cnt
;
582 const char *pin_name
;
584 *npins
= of_property_count_strings(cfg_np
, "samsung,pins");
585 if (IS_ERR_VALUE(*npins
)) {
586 dev_err(dev
, "invalid pin list in %s node", cfg_np
->name
);
590 *pin_list
= devm_kzalloc(dev
, *npins
* sizeof(**pin_list
), GFP_KERNEL
);
592 dev_err(dev
, "failed to allocate memory for pin list\n");
596 of_property_for_each_string(cfg_np
, "samsung,pins", prop
, pin_name
) {
597 for (cnt
= 0; cnt
< pctl
->npins
; cnt
++) {
598 if (pdesc
[cnt
].name
) {
599 if (!strcmp(pin_name
, pdesc
[cnt
].name
)) {
600 (*pin_list
)[idx
++] = pdesc
[cnt
].number
;
605 if (cnt
== pctl
->npins
) {
606 dev_err(dev
, "pin %s not valid in %s node\n",
607 pin_name
, cfg_np
->name
);
608 devm_kfree(dev
, *pin_list
);
617 * Parse the information about all the available pin groups and pin functions
618 * from device node of the pin-controller. A pin group is formed with all
619 * the pins listed in the "samsung,pins" property.
621 static int samsung_pinctrl_parse_dt(struct platform_device
*pdev
,
622 struct samsung_pinctrl_drv_data
*drvdata
)
624 struct device
*dev
= &pdev
->dev
;
625 struct device_node
*dev_np
= dev
->of_node
;
626 struct device_node
*cfg_np
;
627 struct samsung_pin_group
*groups
, *grp
;
628 struct samsung_pmx_func
*functions
, *func
;
630 unsigned int npins
, grp_cnt
, func_idx
= 0;
634 grp_cnt
= of_get_child_count(dev_np
);
638 groups
= devm_kzalloc(dev
, grp_cnt
* sizeof(*groups
), GFP_KERNEL
);
640 dev_err(dev
, "failed allocate memory for ping group list\n");
645 functions
= devm_kzalloc(dev
, grp_cnt
* sizeof(*functions
), GFP_KERNEL
);
647 dev_err(dev
, "failed to allocate memory for function list\n");
653 * Iterate over all the child nodes of the pin controller node
654 * and create pin groups and pin function lists.
656 for_each_child_of_node(dev_np
, cfg_np
) {
658 if (!of_find_property(cfg_np
, "samsung,pins", NULL
))
661 ret
= samsung_pinctrl_parse_dt_pins(pdev
, cfg_np
,
662 &drvdata
->pctl
, &pin_list
, &npins
);
666 /* derive pin group name from the node name */
667 gname
= devm_kzalloc(dev
, strlen(cfg_np
->name
) + GSUFFIX_LEN
,
670 dev_err(dev
, "failed to alloc memory for group name\n");
673 sprintf(gname
, "%s%s", cfg_np
->name
, GROUP_SUFFIX
);
676 grp
->pins
= pin_list
;
677 grp
->num_pins
= npins
;
678 of_property_read_u32(cfg_np
, "samsung,pin-function", &function
);
679 grp
->func
= function
;
682 if (!of_find_property(cfg_np
, "samsung,pin-function", NULL
))
685 /* derive function name from the node name */
686 fname
= devm_kzalloc(dev
, strlen(cfg_np
->name
) + FSUFFIX_LEN
,
689 dev_err(dev
, "failed to alloc memory for func name\n");
692 sprintf(fname
, "%s%s", cfg_np
->name
, FUNCTION_SUFFIX
);
695 func
->groups
= devm_kzalloc(dev
, sizeof(char *), GFP_KERNEL
);
697 dev_err(dev
, "failed to alloc memory for group list "
701 func
->groups
[0] = gname
;
702 func
->num_groups
= 1;
707 drvdata
->pin_groups
= groups
;
708 drvdata
->nr_groups
= grp_cnt
;
709 drvdata
->pmx_functions
= functions
;
710 drvdata
->nr_functions
= func_idx
;
715 /* register the pinctrl interface with the pinctrl subsystem */
716 static int samsung_pinctrl_register(struct platform_device
*pdev
,
717 struct samsung_pinctrl_drv_data
*drvdata
)
719 struct pinctrl_desc
*ctrldesc
= &drvdata
->pctl
;
720 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
721 struct samsung_pin_bank
*pin_bank
;
725 ctrldesc
->name
= "samsung-pinctrl";
726 ctrldesc
->owner
= THIS_MODULE
;
727 ctrldesc
->pctlops
= &samsung_pctrl_ops
;
728 ctrldesc
->pmxops
= &samsung_pinmux_ops
;
729 ctrldesc
->confops
= &samsung_pinconf_ops
;
731 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
732 drvdata
->ctrl
->nr_pins
, GFP_KERNEL
);
734 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
737 ctrldesc
->pins
= pindesc
;
738 ctrldesc
->npins
= drvdata
->ctrl
->nr_pins
;
740 /* dynamically populate the pin number and pin name for pindesc */
741 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
742 pdesc
->number
= pin
+ drvdata
->ctrl
->base
;
745 * allocate space for storing the dynamically generated names for all
746 * the pins which belong to this pin-controller.
748 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
749 drvdata
->ctrl
->nr_pins
, GFP_KERNEL
);
751 dev_err(&pdev
->dev
, "mem alloc for pin names failed\n");
755 /* for each pin, the name of the pin is pin-bank name + pin number */
756 for (bank
= 0; bank
< drvdata
->ctrl
->nr_banks
; bank
++) {
757 pin_bank
= &drvdata
->ctrl
->pin_banks
[bank
];
758 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++) {
759 sprintf(pin_names
, "%s-%d", pin_bank
->name
, pin
);
760 pdesc
= pindesc
+ pin_bank
->pin_base
+ pin
;
761 pdesc
->name
= pin_names
;
762 pin_names
+= PIN_NAME_LENGTH
;
766 drvdata
->pctl_dev
= pinctrl_register(ctrldesc
, &pdev
->dev
, drvdata
);
767 if (!drvdata
->pctl_dev
) {
768 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
772 for (bank
= 0; bank
< drvdata
->ctrl
->nr_banks
; ++bank
) {
773 pin_bank
= &drvdata
->ctrl
->pin_banks
[bank
];
774 pin_bank
->grange
.name
= pin_bank
->name
;
775 pin_bank
->grange
.id
= bank
;
776 pin_bank
->grange
.pin_base
= pin_bank
->pin_base
;
777 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
778 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
779 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
780 pinctrl_add_gpio_range(drvdata
->pctl_dev
, &pin_bank
->grange
);
783 ret
= samsung_pinctrl_parse_dt(pdev
, drvdata
);
785 pinctrl_unregister(drvdata
->pctl_dev
);
792 static const struct gpio_chip samsung_gpiolib_chip
= {
793 .set
= samsung_gpio_set
,
794 .get
= samsung_gpio_get
,
795 .direction_input
= samsung_gpio_direction_input
,
796 .direction_output
= samsung_gpio_direction_output
,
797 .to_irq
= samsung_gpio_to_irq
,
798 .owner
= THIS_MODULE
,
801 /* register the gpiolib interface with the gpiolib subsystem */
802 static int samsung_gpiolib_register(struct platform_device
*pdev
,
803 struct samsung_pinctrl_drv_data
*drvdata
)
805 struct samsung_pin_ctrl
*ctrl
= drvdata
->ctrl
;
806 struct samsung_pin_bank
*bank
= ctrl
->pin_banks
;
807 struct gpio_chip
*gc
;
811 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
812 bank
->gpio_chip
= samsung_gpiolib_chip
;
814 gc
= &bank
->gpio_chip
;
815 gc
->base
= ctrl
->base
+ bank
->pin_base
;
816 gc
->ngpio
= bank
->nr_pins
;
817 gc
->dev
= &pdev
->dev
;
818 gc
->of_node
= bank
->of_node
;
819 gc
->label
= bank
->name
;
821 ret
= gpiochip_add(gc
);
823 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
832 for (--i
, --bank
; i
>= 0; --i
, --bank
)
833 if (gpiochip_remove(&bank
->gpio_chip
))
834 dev_err(&pdev
->dev
, "gpio chip %s remove failed\n",
835 bank
->gpio_chip
.label
);
839 /* unregister the gpiolib interface with the gpiolib subsystem */
840 static int samsung_gpiolib_unregister(struct platform_device
*pdev
,
841 struct samsung_pinctrl_drv_data
*drvdata
)
843 struct samsung_pin_ctrl
*ctrl
= drvdata
->ctrl
;
844 struct samsung_pin_bank
*bank
= ctrl
->pin_banks
;
848 for (i
= 0; !ret
&& i
< ctrl
->nr_banks
; ++i
, ++bank
)
849 ret
= gpiochip_remove(&bank
->gpio_chip
);
852 dev_err(&pdev
->dev
, "gpio chip remove failed\n");
857 static const struct of_device_id samsung_pinctrl_dt_match
[];
859 /* retrieve the soc specific data */
860 static struct samsung_pin_ctrl
*samsung_pinctrl_get_soc_data(
861 struct samsung_pinctrl_drv_data
*d
,
862 struct platform_device
*pdev
)
865 const struct of_device_id
*match
;
866 struct device_node
*node
= pdev
->dev
.of_node
;
867 struct device_node
*np
;
868 struct samsung_pin_ctrl
*ctrl
;
869 struct samsung_pin_bank
*bank
;
872 id
= of_alias_get_id(node
, "pinctrl");
874 dev_err(&pdev
->dev
, "failed to get alias id\n");
877 match
= of_match_node(samsung_pinctrl_dt_match
, node
);
878 ctrl
= (struct samsung_pin_ctrl
*)match
->data
+ id
;
880 bank
= ctrl
->pin_banks
;
881 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
882 spin_lock_init(&bank
->slock
);
884 bank
->pin_base
= ctrl
->nr_pins
;
885 ctrl
->nr_pins
+= bank
->nr_pins
;
888 for_each_child_of_node(node
, np
) {
889 if (!of_find_property(np
, "gpio-controller", NULL
))
891 bank
= ctrl
->pin_banks
;
892 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
893 if (!strcmp(bank
->name
, np
->name
)) {
900 ctrl
->base
= pin_base
;
901 pin_base
+= ctrl
->nr_pins
;
906 static int samsung_pinctrl_probe(struct platform_device
*pdev
)
908 struct samsung_pinctrl_drv_data
*drvdata
;
909 struct device
*dev
= &pdev
->dev
;
910 struct samsung_pin_ctrl
*ctrl
;
911 struct resource
*res
;
915 dev_err(dev
, "device tree node not found\n");
919 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
921 dev_err(dev
, "failed to allocate memory for driver's "
926 ctrl
= samsung_pinctrl_get_soc_data(drvdata
, pdev
);
928 dev_err(&pdev
->dev
, "driver data not available\n");
931 drvdata
->ctrl
= ctrl
;
934 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
936 dev_err(dev
, "cannot find IO resource\n");
940 drvdata
->virt_base
= devm_ioremap_resource(&pdev
->dev
, res
);
941 if (IS_ERR(drvdata
->virt_base
))
942 return PTR_ERR(drvdata
->virt_base
);
944 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
946 drvdata
->irq
= res
->start
;
948 ret
= samsung_gpiolib_register(pdev
, drvdata
);
952 ret
= samsung_pinctrl_register(pdev
, drvdata
);
954 samsung_gpiolib_unregister(pdev
, drvdata
);
958 if (ctrl
->eint_gpio_init
)
959 ctrl
->eint_gpio_init(drvdata
);
960 if (ctrl
->eint_wkup_init
)
961 ctrl
->eint_wkup_init(drvdata
);
963 platform_set_drvdata(pdev
, drvdata
);
967 static const struct of_device_id samsung_pinctrl_dt_match
[] = {
968 #ifdef CONFIG_PINCTRL_EXYNOS
969 { .compatible
= "samsung,exynos4210-pinctrl",
970 .data
= (void *)exynos4210_pin_ctrl
},
971 { .compatible
= "samsung,exynos4x12-pinctrl",
972 .data
= (void *)exynos4x12_pin_ctrl
},
973 { .compatible
= "samsung,exynos5250-pinctrl",
974 .data
= (void *)exynos5250_pin_ctrl
},
976 #ifdef CONFIG_PINCTRL_S3C64XX
977 { .compatible
= "samsung,s3c64xx-pinctrl",
978 .data
= s3c64xx_pin_ctrl
},
982 MODULE_DEVICE_TABLE(of
, samsung_pinctrl_dt_match
);
984 static struct platform_driver samsung_pinctrl_driver
= {
985 .probe
= samsung_pinctrl_probe
,
987 .name
= "samsung-pinctrl",
988 .owner
= THIS_MODULE
,
989 .of_match_table
= of_match_ptr(samsung_pinctrl_dt_match
),
993 static int __init
samsung_pinctrl_drv_register(void)
995 return platform_driver_register(&samsung_pinctrl_driver
);
997 postcore_initcall(samsung_pinctrl_drv_register
);
999 static void __exit
samsung_pinctrl_drv_unregister(void)
1001 platform_driver_unregister(&samsung_pinctrl_driver
);
1003 module_exit(samsung_pinctrl_drv_unregister
);
1005 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1006 MODULE_DESCRIPTION("Samsung pinctrl driver");
1007 MODULE_LICENSE("GPL v2");