1 // SPDX-License-Identifier: GPL-2.0+
3 // pin-controller/pin-mux/pin-config/gpio-driver for Samsung's EXYNOS5440 SoC.
5 // Author: Thomas Abraham <thomas.ab@samsung.com>
7 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
8 // http://www.samsung.com
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/device.h>
17 #include <linux/pinctrl/pinctrl.h>
18 #include <linux/pinctrl/pinmux.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/interrupt.h>
21 #include <linux/irqdomain.h>
22 #include <linux/of_irq.h>
25 /* EXYNOS5440 GPIO and Pinctrl register offsets */
29 #define GPIO_TYPE 0x0C
39 #define EXYNOS5440_MAX_PINS 23
40 #define EXYNOS5440_MAX_GPIO_INT 8
41 #define PIN_NAME_LENGTH 10
43 #define GROUP_SUFFIX "-grp"
44 #define FUNCTION_SUFFIX "-mux"
47 * pin configuration type and its value are packed together into a 16-bits.
48 * The upper 8-bits represent the configuration type and the lower 8-bits
49 * hold the value of the configuration type.
51 #define PINCFG_TYPE_MASK 0xFF
52 #define PINCFG_VALUE_SHIFT 8
53 #define PINCFG_VALUE_MASK (0xFF << PINCFG_VALUE_SHIFT)
54 #define PINCFG_PACK(type, value) (((value) << PINCFG_VALUE_SHIFT) | type)
55 #define PINCFG_UNPACK_TYPE(cfg) ((cfg) & PINCFG_TYPE_MASK)
56 #define PINCFG_UNPACK_VALUE(cfg) (((cfg) & PINCFG_VALUE_MASK) >> \
60 * enum pincfg_type - possible pin configuration types supported.
61 * @PINCFG_TYPE_PUD: Pull up/down configuration.
62 * @PINCFG_TYPE_DRV: Drive strength configuration.
63 * @PINCFG_TYPE_SKEW_RATE: Skew rate configuration.
64 * @PINCFG_TYPE_INPUT_TYPE: Pin input type configuration.
69 PINCFG_TYPE_SKEW_RATE
,
70 PINCFG_TYPE_INPUT_TYPE
74 * struct exynos5440_pin_group: represent group of pins for pincfg setting.
75 * @name: name of the pin group, used to lookup the group.
76 * @pins: the pins included in this group.
77 * @num_pins: number of pins included in this group.
79 struct exynos5440_pin_group
{
81 const unsigned int *pins
;
86 * struct exynos5440_pmx_func: represent a pin function.
87 * @name: name of the pin function, used to lookup the function.
88 * @groups: one or more names of pin groups that provide this function.
89 * @num_groups: number of groups included in @groups.
90 * @function: the function number to be programmed when selected.
92 struct exynos5440_pmx_func
{
96 unsigned long function
;
100 * struct exynos5440_pinctrl_priv_data: driver's private runtime data.
101 * @reg_base: ioremapped based address of the register space.
102 * @gc: gpio chip registered with gpiolib.
103 * @pin_groups: list of pin groups parsed from device tree.
104 * @nr_groups: number of pin groups available.
105 * @pmx_functions: list of pin functions parsed from device tree.
106 * @nr_functions: number of pin functions available.
107 * @range: gpio range to register with pinctrl
109 struct exynos5440_pinctrl_priv_data
{
110 void __iomem
*reg_base
;
111 struct gpio_chip
*gc
;
112 struct irq_domain
*irq_domain
;
114 const struct exynos5440_pin_group
*pin_groups
;
115 unsigned int nr_groups
;
116 const struct exynos5440_pmx_func
*pmx_functions
;
117 unsigned int nr_functions
;
118 struct pinctrl_gpio_range range
;
122 * struct exynos5440_gpio_intr_data: private data for gpio interrupts.
123 * @priv: driver's private runtime data.
124 * @gpio_int: gpio interrupt number.
126 struct exynos5440_gpio_intr_data
{
127 struct exynos5440_pinctrl_priv_data
*priv
;
128 unsigned int gpio_int
;
131 /* list of all possible config options supported */
132 static struct pin_config
{
134 unsigned int cfg_type
;
136 { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD
},
137 { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV
},
138 { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE
},
139 { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE
},
142 /* check if the selector is a valid pin group selector */
143 static int exynos5440_get_group_count(struct pinctrl_dev
*pctldev
)
145 struct exynos5440_pinctrl_priv_data
*priv
;
147 priv
= pinctrl_dev_get_drvdata(pctldev
);
148 return priv
->nr_groups
;
151 /* return the name of the group selected by the group selector */
152 static const char *exynos5440_get_group_name(struct pinctrl_dev
*pctldev
,
155 struct exynos5440_pinctrl_priv_data
*priv
;
157 priv
= pinctrl_dev_get_drvdata(pctldev
);
158 return priv
->pin_groups
[selector
].name
;
161 /* return the pin numbers associated with the specified group */
162 static int exynos5440_get_group_pins(struct pinctrl_dev
*pctldev
,
163 unsigned selector
, const unsigned **pins
, unsigned *num_pins
)
165 struct exynos5440_pinctrl_priv_data
*priv
;
167 priv
= pinctrl_dev_get_drvdata(pctldev
);
168 *pins
= priv
->pin_groups
[selector
].pins
;
169 *num_pins
= priv
->pin_groups
[selector
].num_pins
;
173 /* create pinctrl_map entries by parsing device tree nodes */
174 static int exynos5440_dt_node_to_map(struct pinctrl_dev
*pctldev
,
175 struct device_node
*np
, struct pinctrl_map
**maps
,
178 struct device
*dev
= pctldev
->dev
;
179 struct pinctrl_map
*map
;
180 unsigned long *cfg
= NULL
;
182 int cfg_cnt
= 0, map_cnt
= 0, idx
= 0;
184 /* count the number of config options specfied in the node */
185 for (idx
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++)
186 if (of_find_property(np
, pcfgs
[idx
].prop_cfg
, NULL
))
190 * Find out the number of map entries to create. All the config options
191 * can be accomadated into a single config map entry.
195 if (of_find_property(np
, "samsung,exynos5440-pin-function", NULL
))
198 dev_err(dev
, "node %s does not have either config or function "
199 "configurations\n", np
->name
);
203 /* Allocate memory for pin-map entries */
204 map
= kzalloc(sizeof(*map
) * map_cnt
, GFP_KERNEL
);
210 * Allocate memory for pin group name. The pin group name is derived
211 * from the node name from which these map entries are be created.
213 gname
= kasprintf(GFP_KERNEL
, "%s%s", np
->name
, GROUP_SUFFIX
);
218 * don't have config options? then skip over to creating function
224 /* Allocate memory for config entries */
225 cfg
= kzalloc(sizeof(*cfg
) * cfg_cnt
, GFP_KERNEL
);
229 /* Prepare a list of config settings */
230 for (idx
= 0, cfg_cnt
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++) {
232 if (!of_property_read_u32(np
, pcfgs
[idx
].prop_cfg
, &value
))
234 PINCFG_PACK(pcfgs
[idx
].cfg_type
, value
);
237 /* create the config map entry */
238 map
[*nmaps
].data
.configs
.group_or_pin
= gname
;
239 map
[*nmaps
].data
.configs
.configs
= cfg
;
240 map
[*nmaps
].data
.configs
.num_configs
= cfg_cnt
;
241 map
[*nmaps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
245 /* create the function map entry */
246 if (of_find_property(np
, "samsung,exynos5440-pin-function", NULL
)) {
247 fname
= kasprintf(GFP_KERNEL
,
248 "%s%s", np
->name
, FUNCTION_SUFFIX
);
252 map
[*nmaps
].data
.mux
.group
= gname
;
253 map
[*nmaps
].data
.mux
.function
= fname
;
254 map
[*nmaps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
270 /* free the memory allocated to hold the pin-map table */
271 static void exynos5440_dt_free_map(struct pinctrl_dev
*pctldev
,
272 struct pinctrl_map
*map
, unsigned num_maps
)
276 for (idx
= 0; idx
< num_maps
; idx
++) {
277 if (map
[idx
].type
== PIN_MAP_TYPE_MUX_GROUP
) {
278 kfree(map
[idx
].data
.mux
.function
);
280 kfree(map
[idx
].data
.mux
.group
);
281 } else if (map
->type
== PIN_MAP_TYPE_CONFIGS_GROUP
) {
282 kfree(map
[idx
].data
.configs
.configs
);
284 kfree(map
[idx
].data
.configs
.group_or_pin
);
291 /* list of pinctrl callbacks for the pinctrl core */
292 static const struct pinctrl_ops exynos5440_pctrl_ops
= {
293 .get_groups_count
= exynos5440_get_group_count
,
294 .get_group_name
= exynos5440_get_group_name
,
295 .get_group_pins
= exynos5440_get_group_pins
,
296 .dt_node_to_map
= exynos5440_dt_node_to_map
,
297 .dt_free_map
= exynos5440_dt_free_map
,
300 /* check if the selector is a valid pin function selector */
301 static int exynos5440_get_functions_count(struct pinctrl_dev
*pctldev
)
303 struct exynos5440_pinctrl_priv_data
*priv
;
305 priv
= pinctrl_dev_get_drvdata(pctldev
);
306 return priv
->nr_functions
;
309 /* return the name of the pin function specified */
310 static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
313 struct exynos5440_pinctrl_priv_data
*priv
;
315 priv
= pinctrl_dev_get_drvdata(pctldev
);
316 return priv
->pmx_functions
[selector
].name
;
319 /* return the groups associated for the specified function selector */
320 static int exynos5440_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
321 unsigned selector
, const char * const **groups
,
322 unsigned * const num_groups
)
324 struct exynos5440_pinctrl_priv_data
*priv
;
326 priv
= pinctrl_dev_get_drvdata(pctldev
);
327 *groups
= priv
->pmx_functions
[selector
].groups
;
328 *num_groups
= priv
->pmx_functions
[selector
].num_groups
;
332 /* enable or disable a pinmux function */
333 static void exynos5440_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
334 unsigned group
, bool enable
)
336 struct exynos5440_pinctrl_priv_data
*priv
;
341 priv
= pinctrl_dev_get_drvdata(pctldev
);
342 base
= priv
->reg_base
;
343 function
= priv
->pmx_functions
[selector
].function
;
345 data
= readl(base
+ GPIO_MUX
);
347 data
|= (1 << function
);
349 data
&= ~(1 << function
);
350 writel(data
, base
+ GPIO_MUX
);
353 /* enable a specified pinmux by writing to registers */
354 static int exynos5440_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
358 exynos5440_pinmux_setup(pctldev
, selector
, group
, true);
363 * The calls to gpio_direction_output() and gpio_direction_input()
364 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
365 * function called from the gpiolib interface).
367 static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev
*pctldev
,
368 struct pinctrl_gpio_range
*range
, unsigned offset
, bool input
)
373 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
374 static const struct pinmux_ops exynos5440_pinmux_ops
= {
375 .get_functions_count
= exynos5440_get_functions_count
,
376 .get_function_name
= exynos5440_pinmux_get_fname
,
377 .get_function_groups
= exynos5440_pinmux_get_groups
,
378 .set_mux
= exynos5440_pinmux_set_mux
,
379 .gpio_set_direction
= exynos5440_pinmux_gpio_set_direction
,
382 /* set the pin config settings for a specified pin */
383 static int exynos5440_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
384 unsigned long *configs
,
385 unsigned num_configs
)
387 struct exynos5440_pinctrl_priv_data
*priv
;
389 enum pincfg_type cfg_type
;
394 priv
= pinctrl_dev_get_drvdata(pctldev
);
395 base
= priv
->reg_base
;
397 for (i
= 0; i
< num_configs
; i
++) {
398 cfg_type
= PINCFG_UNPACK_TYPE(configs
[i
]);
399 cfg_value
= PINCFG_UNPACK_VALUE(configs
[i
]);
402 case PINCFG_TYPE_PUD
:
403 /* first set pull enable/disable bit */
404 data
= readl(base
+ GPIO_PE
);
408 writel(data
, base
+ GPIO_PE
);
410 /* then set pull up/down bit */
411 data
= readl(base
+ GPIO_PS
);
415 writel(data
, base
+ GPIO_PS
);
418 case PINCFG_TYPE_DRV
:
419 /* set the first bit of the drive strength */
420 data
= readl(base
+ GPIO_DS0
);
422 data
|= ((cfg_value
& 1) << pin
);
423 writel(data
, base
+ GPIO_DS0
);
426 /* set the second bit of the driver strength */
427 data
= readl(base
+ GPIO_DS1
);
429 data
|= ((cfg_value
& 1) << pin
);
430 writel(data
, base
+ GPIO_DS1
);
432 case PINCFG_TYPE_SKEW_RATE
:
433 data
= readl(base
+ GPIO_SR
);
435 data
|= ((cfg_value
& 1) << pin
);
436 writel(data
, base
+ GPIO_SR
);
438 case PINCFG_TYPE_INPUT_TYPE
:
439 data
= readl(base
+ GPIO_TYPE
);
441 data
|= ((cfg_value
& 1) << pin
);
442 writel(data
, base
+ GPIO_TYPE
);
448 } /* for each config */
453 /* get the pin config settings for a specified pin */
454 static int exynos5440_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
455 unsigned long *config
)
457 struct exynos5440_pinctrl_priv_data
*priv
;
459 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
462 priv
= pinctrl_dev_get_drvdata(pctldev
);
463 base
= priv
->reg_base
;
466 case PINCFG_TYPE_PUD
:
467 data
= readl(base
+ GPIO_PE
);
468 data
= (data
>> pin
) & 1;
472 *config
= ((readl(base
+ GPIO_PS
) >> pin
) & 1) + 1;
474 case PINCFG_TYPE_DRV
:
475 data
= readl(base
+ GPIO_DS0
);
476 data
= (data
>> pin
) & 1;
478 data
= readl(base
+ GPIO_DS1
);
479 data
= (data
>> pin
) & 1;
480 *config
|= (data
<< 1);
482 case PINCFG_TYPE_SKEW_RATE
:
483 data
= readl(base
+ GPIO_SR
);
484 *config
= (data
>> pin
) & 1;
486 case PINCFG_TYPE_INPUT_TYPE
:
487 data
= readl(base
+ GPIO_TYPE
);
488 *config
= (data
>> pin
) & 1;
498 /* set the pin config settings for a specified pin group */
499 static int exynos5440_pinconf_group_set(struct pinctrl_dev
*pctldev
,
500 unsigned group
, unsigned long *configs
,
501 unsigned num_configs
)
503 struct exynos5440_pinctrl_priv_data
*priv
;
504 const unsigned int *pins
;
507 priv
= pinctrl_dev_get_drvdata(pctldev
);
508 pins
= priv
->pin_groups
[group
].pins
;
510 for (cnt
= 0; cnt
< priv
->pin_groups
[group
].num_pins
; cnt
++)
511 exynos5440_pinconf_set(pctldev
, pins
[cnt
], configs
,
517 /* get the pin config settings for a specified pin group */
518 static int exynos5440_pinconf_group_get(struct pinctrl_dev
*pctldev
,
519 unsigned int group
, unsigned long *config
)
521 struct exynos5440_pinctrl_priv_data
*priv
;
522 const unsigned int *pins
;
524 priv
= pinctrl_dev_get_drvdata(pctldev
);
525 pins
= priv
->pin_groups
[group
].pins
;
526 exynos5440_pinconf_get(pctldev
, pins
[0], config
);
530 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
531 static const struct pinconf_ops exynos5440_pinconf_ops
= {
532 .pin_config_get
= exynos5440_pinconf_get
,
533 .pin_config_set
= exynos5440_pinconf_set
,
534 .pin_config_group_get
= exynos5440_pinconf_group_get
,
535 .pin_config_group_set
= exynos5440_pinconf_group_set
,
538 /* gpiolib gpio_set callback function */
539 static void exynos5440_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
541 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
542 void __iomem
*base
= priv
->reg_base
;
545 data
= readl(base
+ GPIO_VAL
);
546 data
&= ~(1 << offset
);
549 writel(data
, base
+ GPIO_VAL
);
552 /* gpiolib gpio_get callback function */
553 static int exynos5440_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
555 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
556 void __iomem
*base
= priv
->reg_base
;
559 data
= readl(base
+ GPIO_IN
);
565 /* gpiolib gpio_direction_input callback function */
566 static int exynos5440_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
568 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
569 void __iomem
*base
= priv
->reg_base
;
572 /* first disable the data output enable on this pin */
573 data
= readl(base
+ GPIO_OE
);
574 data
&= ~(1 << offset
);
575 writel(data
, base
+ GPIO_OE
);
577 /* now enable input on this pin */
578 data
= readl(base
+ GPIO_IE
);
580 writel(data
, base
+ GPIO_IE
);
584 /* gpiolib gpio_direction_output callback function */
585 static int exynos5440_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
588 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
589 void __iomem
*base
= priv
->reg_base
;
592 exynos5440_gpio_set(gc
, offset
, value
);
594 /* first disable the data input enable on this pin */
595 data
= readl(base
+ GPIO_IE
);
596 data
&= ~(1 << offset
);
597 writel(data
, base
+ GPIO_IE
);
599 /* now enable output on this pin */
600 data
= readl(base
+ GPIO_OE
);
602 writel(data
, base
+ GPIO_OE
);
606 /* gpiolib gpio_to_irq callback function */
607 static int exynos5440_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
609 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
612 if (offset
< 16 || offset
> 23)
615 if (!priv
->irq_domain
)
618 virq
= irq_create_mapping(priv
->irq_domain
, offset
- 16);
619 return virq
? : -ENXIO
;
622 /* parse the pin numbers listed in the 'samsung,exynos5440-pins' property */
623 static int exynos5440_pinctrl_parse_dt_pins(struct platform_device
*pdev
,
624 struct device_node
*cfg_np
, unsigned int **pin_list
,
627 struct device
*dev
= &pdev
->dev
;
628 struct property
*prop
;
630 prop
= of_find_property(cfg_np
, "samsung,exynos5440-pins", NULL
);
634 *npins
= prop
->length
/ sizeof(unsigned long);
636 dev_err(dev
, "invalid pin list in %s node", cfg_np
->name
);
640 *pin_list
= devm_kzalloc(dev
, *npins
* sizeof(**pin_list
), GFP_KERNEL
);
644 return of_property_read_u32_array(cfg_np
, "samsung,exynos5440-pins",
649 * Parse the information about all the available pin groups and pin functions
650 * from device node of the pin-controller.
652 static int exynos5440_pinctrl_parse_dt(struct platform_device
*pdev
,
653 struct exynos5440_pinctrl_priv_data
*priv
)
655 struct device
*dev
= &pdev
->dev
;
656 struct device_node
*dev_np
= dev
->of_node
;
657 struct device_node
*cfg_np
;
658 struct exynos5440_pin_group
*groups
, *grp
;
659 struct exynos5440_pmx_func
*functions
, *func
;
661 unsigned int npins
, grp_cnt
, func_idx
= 0;
665 grp_cnt
= of_get_child_count(dev_np
);
669 groups
= devm_kzalloc(dev
, grp_cnt
* sizeof(*groups
), GFP_KERNEL
);
675 functions
= devm_kzalloc(dev
, grp_cnt
* sizeof(*functions
), GFP_KERNEL
);
682 * Iterate over all the child nodes of the pin controller node
683 * and create pin groups and pin function lists.
685 for_each_child_of_node(dev_np
, cfg_np
) {
688 ret
= exynos5440_pinctrl_parse_dt_pins(pdev
, cfg_np
,
692 goto skip_to_pin_function
;
695 /* derive pin group name from the node name */
696 gname
= devm_kasprintf(dev
, GFP_KERNEL
,
697 "%s%s", cfg_np
->name
, GROUP_SUFFIX
);
702 grp
->pins
= pin_list
;
703 grp
->num_pins
= npins
;
706 skip_to_pin_function
:
707 ret
= of_property_read_u32(cfg_np
, "samsung,exynos5440-pin-function",
712 /* derive function name from the node name */
713 fname
= devm_kasprintf(dev
, GFP_KERNEL
,
714 "%s%s", cfg_np
->name
, FUNCTION_SUFFIX
);
719 func
->groups
= devm_kzalloc(dev
, sizeof(char *), GFP_KERNEL
);
722 func
->groups
[0] = gname
;
723 func
->num_groups
= gname
? 1 : 0;
724 func
->function
= function
;
729 priv
->pin_groups
= groups
;
730 priv
->nr_groups
= grp_cnt
;
731 priv
->pmx_functions
= functions
;
732 priv
->nr_functions
= func_idx
;
736 /* register the pinctrl interface with the pinctrl subsystem */
737 static int exynos5440_pinctrl_register(struct platform_device
*pdev
,
738 struct exynos5440_pinctrl_priv_data
*priv
)
740 struct device
*dev
= &pdev
->dev
;
741 struct pinctrl_desc
*ctrldesc
;
742 struct pinctrl_dev
*pctl_dev
;
743 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
747 ctrldesc
= devm_kzalloc(dev
, sizeof(*ctrldesc
), GFP_KERNEL
);
751 ctrldesc
->name
= "exynos5440-pinctrl";
752 ctrldesc
->owner
= THIS_MODULE
;
753 ctrldesc
->pctlops
= &exynos5440_pctrl_ops
;
754 ctrldesc
->pmxops
= &exynos5440_pinmux_ops
;
755 ctrldesc
->confops
= &exynos5440_pinconf_ops
;
757 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
758 EXYNOS5440_MAX_PINS
, GFP_KERNEL
);
761 ctrldesc
->pins
= pindesc
;
762 ctrldesc
->npins
= EXYNOS5440_MAX_PINS
;
764 /* dynamically populate the pin number and pin name for pindesc */
765 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
769 * allocate space for storing the dynamically generated names for all
770 * the pins which belong to this pin-controller.
772 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
773 ctrldesc
->npins
, GFP_KERNEL
);
777 /* for each pin, set the name of the pin */
778 for (pin
= 0; pin
< ctrldesc
->npins
; pin
++) {
779 snprintf(pin_names
, 6, "gpio%02d", pin
);
780 pdesc
= pindesc
+ pin
;
781 pdesc
->name
= pin_names
;
782 pin_names
+= PIN_NAME_LENGTH
;
785 ret
= exynos5440_pinctrl_parse_dt(pdev
, priv
);
789 pctl_dev
= devm_pinctrl_register(&pdev
->dev
, ctrldesc
, priv
);
790 if (IS_ERR(pctl_dev
)) {
791 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
792 return PTR_ERR(pctl_dev
);
795 priv
->range
.name
= "exynos5440-pctrl-gpio-range";
797 priv
->range
.base
= 0;
798 priv
->range
.npins
= EXYNOS5440_MAX_PINS
;
799 priv
->range
.gc
= priv
->gc
;
800 pinctrl_add_gpio_range(pctl_dev
, &priv
->range
);
804 /* register the gpiolib interface with the gpiolib subsystem */
805 static int exynos5440_gpiolib_register(struct platform_device
*pdev
,
806 struct exynos5440_pinctrl_priv_data
*priv
)
808 struct gpio_chip
*gc
;
811 gc
= devm_kzalloc(&pdev
->dev
, sizeof(*gc
), GFP_KERNEL
);
817 gc
->ngpio
= EXYNOS5440_MAX_PINS
;
818 gc
->parent
= &pdev
->dev
;
819 gc
->set
= exynos5440_gpio_set
;
820 gc
->get
= exynos5440_gpio_get
;
821 gc
->direction_input
= exynos5440_gpio_direction_input
;
822 gc
->direction_output
= exynos5440_gpio_direction_output
;
823 gc
->to_irq
= exynos5440_gpio_to_irq
;
824 gc
->label
= "gpiolib-exynos5440";
825 gc
->owner
= THIS_MODULE
;
826 ret
= gpiochip_add_data(gc
, priv
);
828 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error "
829 "code: %d\n", gc
->label
, ret
);
836 /* unregister the gpiolib interface with the gpiolib subsystem */
837 static int exynos5440_gpiolib_unregister(struct platform_device
*pdev
,
838 struct exynos5440_pinctrl_priv_data
*priv
)
840 gpiochip_remove(priv
->gc
);
844 static void exynos5440_gpio_irq_unmask(struct irq_data
*irqd
)
846 struct exynos5440_pinctrl_priv_data
*d
;
847 unsigned long gpio_int
;
849 d
= irq_data_get_irq_chip_data(irqd
);
850 gpio_int
= readl(d
->reg_base
+ GPIO_INT
);
851 gpio_int
|= 1 << irqd
->hwirq
;
852 writel(gpio_int
, d
->reg_base
+ GPIO_INT
);
855 static void exynos5440_gpio_irq_mask(struct irq_data
*irqd
)
857 struct exynos5440_pinctrl_priv_data
*d
;
858 unsigned long gpio_int
;
860 d
= irq_data_get_irq_chip_data(irqd
);
861 gpio_int
= readl(d
->reg_base
+ GPIO_INT
);
862 gpio_int
&= ~(1 << irqd
->hwirq
);
863 writel(gpio_int
, d
->reg_base
+ GPIO_INT
);
866 /* irq_chip for gpio interrupts */
867 static struct irq_chip exynos5440_gpio_irq_chip
= {
868 .name
= "exynos5440_gpio_irq_chip",
869 .irq_unmask
= exynos5440_gpio_irq_unmask
,
870 .irq_mask
= exynos5440_gpio_irq_mask
,
873 /* interrupt handler for GPIO interrupts 0..7 */
874 static irqreturn_t
exynos5440_gpio_irq(int irq
, void *data
)
876 struct exynos5440_gpio_intr_data
*intd
= data
;
877 struct exynos5440_pinctrl_priv_data
*d
= intd
->priv
;
880 virq
= irq_linear_revmap(d
->irq_domain
, intd
->gpio_int
);
883 generic_handle_irq(virq
);
887 static int exynos5440_gpio_irq_map(struct irq_domain
*h
, unsigned int virq
,
890 struct exynos5440_pinctrl_priv_data
*d
= h
->host_data
;
892 irq_set_chip_data(virq
, d
);
893 irq_set_chip_and_handler(virq
, &exynos5440_gpio_irq_chip
,
898 /* irq domain callbacks for gpio interrupt controller */
899 static const struct irq_domain_ops exynos5440_gpio_irqd_ops
= {
900 .map
= exynos5440_gpio_irq_map
,
901 .xlate
= irq_domain_xlate_twocell
,
904 /* setup handling of gpio interrupts */
905 static int exynos5440_gpio_irq_init(struct platform_device
*pdev
,
906 struct exynos5440_pinctrl_priv_data
*priv
)
908 struct device
*dev
= &pdev
->dev
;
909 struct exynos5440_gpio_intr_data
*intd
;
912 intd
= devm_kzalloc(dev
, sizeof(*intd
) * EXYNOS5440_MAX_GPIO_INT
,
917 for (i
= 0; i
< EXYNOS5440_MAX_GPIO_INT
; i
++) {
918 irq
= irq_of_parse_and_map(dev
->of_node
, i
);
920 dev_err(dev
, "irq parsing failed\n");
926 ret
= devm_request_irq(dev
, irq
, exynos5440_gpio_irq
,
927 0, dev_name(dev
), intd
++);
929 dev_err(dev
, "irq request failed\n");
934 priv
->irq_domain
= irq_domain_add_linear(dev
->of_node
,
935 EXYNOS5440_MAX_GPIO_INT
,
936 &exynos5440_gpio_irqd_ops
, priv
);
937 if (!priv
->irq_domain
) {
938 dev_err(dev
, "failed to create irq domain\n");
945 static int exynos5440_pinctrl_probe(struct platform_device
*pdev
)
947 struct device
*dev
= &pdev
->dev
;
948 struct exynos5440_pinctrl_priv_data
*priv
;
949 struct resource
*res
;
953 dev_err(dev
, "device tree node not found\n");
957 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
961 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
962 priv
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
963 if (IS_ERR(priv
->reg_base
))
964 return PTR_ERR(priv
->reg_base
);
966 ret
= exynos5440_gpiolib_register(pdev
, priv
);
970 ret
= exynos5440_pinctrl_register(pdev
, priv
);
972 exynos5440_gpiolib_unregister(pdev
, priv
);
976 ret
= exynos5440_gpio_irq_init(pdev
, priv
);
978 dev_err(dev
, "failed to setup gpio interrupts\n");
982 platform_set_drvdata(pdev
, priv
);
983 dev_info(dev
, "EXYNOS5440 pinctrl driver registered\n");
987 static const struct of_device_id exynos5440_pinctrl_dt_match
[] = {
988 { .compatible
= "samsung,exynos5440-pinctrl" },
992 static struct platform_driver exynos5440_pinctrl_driver
= {
993 .probe
= exynos5440_pinctrl_probe
,
995 .name
= "exynos5440-pinctrl",
996 .of_match_table
= exynos5440_pinctrl_dt_match
,
997 .suppress_bind_attrs
= true,
1001 static int __init
exynos5440_pinctrl_drv_register(void)
1003 return platform_driver_register(&exynos5440_pinctrl_driver
);
1005 postcore_initcall(exynos5440_pinctrl_drv_register
);