2 * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's EXYNOS5440 SoC.
4 * Author: Thomas Abraham <thomas.ab@samsung.com>
6 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/device.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/interrupt.h>
26 #include <linux/irqdomain.h>
27 #include <linux/of_irq.h>
30 /* EXYNOS5440 GPIO and Pinctrl register offsets */
34 #define GPIO_TYPE 0x0C
44 #define EXYNOS5440_MAX_PINS 23
45 #define EXYNOS5440_MAX_GPIO_INT 8
46 #define PIN_NAME_LENGTH 10
48 #define GROUP_SUFFIX "-grp"
49 #define FUNCTION_SUFFIX "-mux"
52 * pin configuration type and its value are packed together into a 16-bits.
53 * The upper 8-bits represent the configuration type and the lower 8-bits
54 * hold the value of the configuration type.
56 #define PINCFG_TYPE_MASK 0xFF
57 #define PINCFG_VALUE_SHIFT 8
58 #define PINCFG_VALUE_MASK (0xFF << PINCFG_VALUE_SHIFT)
59 #define PINCFG_PACK(type, value) (((value) << PINCFG_VALUE_SHIFT) | type)
60 #define PINCFG_UNPACK_TYPE(cfg) ((cfg) & PINCFG_TYPE_MASK)
61 #define PINCFG_UNPACK_VALUE(cfg) (((cfg) & PINCFG_VALUE_MASK) >> \
65 * enum pincfg_type - possible pin configuration types supported.
66 * @PINCFG_TYPE_PUD: Pull up/down configuration.
67 * @PINCFG_TYPE_DRV: Drive strength configuration.
68 * @PINCFG_TYPE_SKEW_RATE: Skew rate configuration.
69 * @PINCFG_TYPE_INPUT_TYPE: Pin input type configuration.
74 PINCFG_TYPE_SKEW_RATE
,
75 PINCFG_TYPE_INPUT_TYPE
79 * struct exynos5440_pin_group: represent group of pins for pincfg setting.
80 * @name: name of the pin group, used to lookup the group.
81 * @pins: the pins included in this group.
82 * @num_pins: number of pins included in this group.
84 struct exynos5440_pin_group
{
86 const unsigned int *pins
;
91 * struct exynos5440_pmx_func: represent a pin function.
92 * @name: name of the pin function, used to lookup the function.
93 * @groups: one or more names of pin groups that provide this function.
94 * @num_groups: number of groups included in @groups.
95 * @function: the function number to be programmed when selected.
97 struct exynos5440_pmx_func
{
101 unsigned long function
;
105 * struct exynos5440_pinctrl_priv_data: driver's private runtime data.
106 * @reg_base: ioremapped based address of the register space.
107 * @gc: gpio chip registered with gpiolib.
108 * @pin_groups: list of pin groups parsed from device tree.
109 * @nr_groups: number of pin groups available.
110 * @pmx_functions: list of pin functions parsed from device tree.
111 * @nr_functions: number of pin functions available.
112 * @range: gpio range to register with pinctrl
114 struct exynos5440_pinctrl_priv_data
{
115 void __iomem
*reg_base
;
116 struct gpio_chip
*gc
;
117 struct irq_domain
*irq_domain
;
119 const struct exynos5440_pin_group
*pin_groups
;
120 unsigned int nr_groups
;
121 const struct exynos5440_pmx_func
*pmx_functions
;
122 unsigned int nr_functions
;
123 struct pinctrl_gpio_range range
;
127 * struct exynos5440_gpio_intr_data: private data for gpio interrupts.
128 * @priv: driver's private runtime data.
129 * @gpio_int: gpio interrupt number.
131 struct exynos5440_gpio_intr_data
{
132 struct exynos5440_pinctrl_priv_data
*priv
;
133 unsigned int gpio_int
;
136 /* list of all possible config options supported */
137 static struct pin_config
{
139 unsigned int cfg_type
;
141 { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD
},
142 { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV
},
143 { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE
},
144 { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE
},
147 /* check if the selector is a valid pin group selector */
148 static int exynos5440_get_group_count(struct pinctrl_dev
*pctldev
)
150 struct exynos5440_pinctrl_priv_data
*priv
;
152 priv
= pinctrl_dev_get_drvdata(pctldev
);
153 return priv
->nr_groups
;
156 /* return the name of the group selected by the group selector */
157 static const char *exynos5440_get_group_name(struct pinctrl_dev
*pctldev
,
160 struct exynos5440_pinctrl_priv_data
*priv
;
162 priv
= pinctrl_dev_get_drvdata(pctldev
);
163 return priv
->pin_groups
[selector
].name
;
166 /* return the pin numbers associated with the specified group */
167 static int exynos5440_get_group_pins(struct pinctrl_dev
*pctldev
,
168 unsigned selector
, const unsigned **pins
, unsigned *num_pins
)
170 struct exynos5440_pinctrl_priv_data
*priv
;
172 priv
= pinctrl_dev_get_drvdata(pctldev
);
173 *pins
= priv
->pin_groups
[selector
].pins
;
174 *num_pins
= priv
->pin_groups
[selector
].num_pins
;
178 /* create pinctrl_map entries by parsing device tree nodes */
179 static int exynos5440_dt_node_to_map(struct pinctrl_dev
*pctldev
,
180 struct device_node
*np
, struct pinctrl_map
**maps
,
183 struct device
*dev
= pctldev
->dev
;
184 struct pinctrl_map
*map
;
185 unsigned long *cfg
= NULL
;
187 int cfg_cnt
= 0, map_cnt
= 0, idx
= 0;
189 /* count the number of config options specfied in the node */
190 for (idx
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++)
191 if (of_find_property(np
, pcfgs
[idx
].prop_cfg
, NULL
))
195 * Find out the number of map entries to create. All the config options
196 * can be accomadated into a single config map entry.
200 if (of_find_property(np
, "samsung,exynos5440-pin-function", NULL
))
203 dev_err(dev
, "node %s does not have either config or function "
204 "configurations\n", np
->name
);
208 /* Allocate memory for pin-map entries */
209 map
= kzalloc(sizeof(*map
) * map_cnt
, GFP_KERNEL
);
215 * Allocate memory for pin group name. The pin group name is derived
216 * from the node name from which these map entries are be created.
218 gname
= kasprintf(GFP_KERNEL
, "%s%s", np
->name
, GROUP_SUFFIX
);
223 * don't have config options? then skip over to creating function
229 /* Allocate memory for config entries */
230 cfg
= kzalloc(sizeof(*cfg
) * cfg_cnt
, GFP_KERNEL
);
234 /* Prepare a list of config settings */
235 for (idx
= 0, cfg_cnt
= 0; idx
< ARRAY_SIZE(pcfgs
); idx
++) {
237 if (!of_property_read_u32(np
, pcfgs
[idx
].prop_cfg
, &value
))
239 PINCFG_PACK(pcfgs
[idx
].cfg_type
, value
);
242 /* create the config map entry */
243 map
[*nmaps
].data
.configs
.group_or_pin
= gname
;
244 map
[*nmaps
].data
.configs
.configs
= cfg
;
245 map
[*nmaps
].data
.configs
.num_configs
= cfg_cnt
;
246 map
[*nmaps
].type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
250 /* create the function map entry */
251 if (of_find_property(np
, "samsung,exynos5440-pin-function", NULL
)) {
252 fname
= kasprintf(GFP_KERNEL
,
253 "%s%s", np
->name
, FUNCTION_SUFFIX
);
257 map
[*nmaps
].data
.mux
.group
= gname
;
258 map
[*nmaps
].data
.mux
.function
= fname
;
259 map
[*nmaps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
275 /* free the memory allocated to hold the pin-map table */
276 static void exynos5440_dt_free_map(struct pinctrl_dev
*pctldev
,
277 struct pinctrl_map
*map
, unsigned num_maps
)
281 for (idx
= 0; idx
< num_maps
; idx
++) {
282 if (map
[idx
].type
== PIN_MAP_TYPE_MUX_GROUP
) {
283 kfree(map
[idx
].data
.mux
.function
);
285 kfree(map
[idx
].data
.mux
.group
);
286 } else if (map
->type
== PIN_MAP_TYPE_CONFIGS_GROUP
) {
287 kfree(map
[idx
].data
.configs
.configs
);
289 kfree(map
[idx
].data
.configs
.group_or_pin
);
296 /* list of pinctrl callbacks for the pinctrl core */
297 static const struct pinctrl_ops exynos5440_pctrl_ops
= {
298 .get_groups_count
= exynos5440_get_group_count
,
299 .get_group_name
= exynos5440_get_group_name
,
300 .get_group_pins
= exynos5440_get_group_pins
,
301 .dt_node_to_map
= exynos5440_dt_node_to_map
,
302 .dt_free_map
= exynos5440_dt_free_map
,
305 /* check if the selector is a valid pin function selector */
306 static int exynos5440_get_functions_count(struct pinctrl_dev
*pctldev
)
308 struct exynos5440_pinctrl_priv_data
*priv
;
310 priv
= pinctrl_dev_get_drvdata(pctldev
);
311 return priv
->nr_functions
;
314 /* return the name of the pin function specified */
315 static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev
*pctldev
,
318 struct exynos5440_pinctrl_priv_data
*priv
;
320 priv
= pinctrl_dev_get_drvdata(pctldev
);
321 return priv
->pmx_functions
[selector
].name
;
324 /* return the groups associated for the specified function selector */
325 static int exynos5440_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
326 unsigned selector
, const char * const **groups
,
327 unsigned * const num_groups
)
329 struct exynos5440_pinctrl_priv_data
*priv
;
331 priv
= pinctrl_dev_get_drvdata(pctldev
);
332 *groups
= priv
->pmx_functions
[selector
].groups
;
333 *num_groups
= priv
->pmx_functions
[selector
].num_groups
;
337 /* enable or disable a pinmux function */
338 static void exynos5440_pinmux_setup(struct pinctrl_dev
*pctldev
, unsigned selector
,
339 unsigned group
, bool enable
)
341 struct exynos5440_pinctrl_priv_data
*priv
;
346 priv
= pinctrl_dev_get_drvdata(pctldev
);
347 base
= priv
->reg_base
;
348 function
= priv
->pmx_functions
[selector
].function
;
350 data
= readl(base
+ GPIO_MUX
);
352 data
|= (1 << function
);
354 data
&= ~(1 << function
);
355 writel(data
, base
+ GPIO_MUX
);
358 /* enable a specified pinmux by writing to registers */
359 static int exynos5440_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
363 exynos5440_pinmux_setup(pctldev
, selector
, group
, true);
368 * The calls to gpio_direction_output() and gpio_direction_input()
369 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
370 * function called from the gpiolib interface).
372 static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev
*pctldev
,
373 struct pinctrl_gpio_range
*range
, unsigned offset
, bool input
)
378 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
379 static const struct pinmux_ops exynos5440_pinmux_ops
= {
380 .get_functions_count
= exynos5440_get_functions_count
,
381 .get_function_name
= exynos5440_pinmux_get_fname
,
382 .get_function_groups
= exynos5440_pinmux_get_groups
,
383 .set_mux
= exynos5440_pinmux_set_mux
,
384 .gpio_set_direction
= exynos5440_pinmux_gpio_set_direction
,
387 /* set the pin config settings for a specified pin */
388 static int exynos5440_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
389 unsigned long *configs
,
390 unsigned num_configs
)
392 struct exynos5440_pinctrl_priv_data
*priv
;
394 enum pincfg_type cfg_type
;
399 priv
= pinctrl_dev_get_drvdata(pctldev
);
400 base
= priv
->reg_base
;
402 for (i
= 0; i
< num_configs
; i
++) {
403 cfg_type
= PINCFG_UNPACK_TYPE(configs
[i
]);
404 cfg_value
= PINCFG_UNPACK_VALUE(configs
[i
]);
407 case PINCFG_TYPE_PUD
:
408 /* first set pull enable/disable bit */
409 data
= readl(base
+ GPIO_PE
);
413 writel(data
, base
+ GPIO_PE
);
415 /* then set pull up/down bit */
416 data
= readl(base
+ GPIO_PS
);
420 writel(data
, base
+ GPIO_PS
);
423 case PINCFG_TYPE_DRV
:
424 /* set the first bit of the drive strength */
425 data
= readl(base
+ GPIO_DS0
);
427 data
|= ((cfg_value
& 1) << pin
);
428 writel(data
, base
+ GPIO_DS0
);
431 /* set the second bit of the driver strength */
432 data
= readl(base
+ GPIO_DS1
);
434 data
|= ((cfg_value
& 1) << pin
);
435 writel(data
, base
+ GPIO_DS1
);
437 case PINCFG_TYPE_SKEW_RATE
:
438 data
= readl(base
+ GPIO_SR
);
440 data
|= ((cfg_value
& 1) << pin
);
441 writel(data
, base
+ GPIO_SR
);
443 case PINCFG_TYPE_INPUT_TYPE
:
444 data
= readl(base
+ GPIO_TYPE
);
446 data
|= ((cfg_value
& 1) << pin
);
447 writel(data
, base
+ GPIO_TYPE
);
453 } /* for each config */
458 /* get the pin config settings for a specified pin */
459 static int exynos5440_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
460 unsigned long *config
)
462 struct exynos5440_pinctrl_priv_data
*priv
;
464 enum pincfg_type cfg_type
= PINCFG_UNPACK_TYPE(*config
);
467 priv
= pinctrl_dev_get_drvdata(pctldev
);
468 base
= priv
->reg_base
;
471 case PINCFG_TYPE_PUD
:
472 data
= readl(base
+ GPIO_PE
);
473 data
= (data
>> pin
) & 1;
477 *config
= ((readl(base
+ GPIO_PS
) >> pin
) & 1) + 1;
479 case PINCFG_TYPE_DRV
:
480 data
= readl(base
+ GPIO_DS0
);
481 data
= (data
>> pin
) & 1;
483 data
= readl(base
+ GPIO_DS1
);
484 data
= (data
>> pin
) & 1;
485 *config
|= (data
<< 1);
487 case PINCFG_TYPE_SKEW_RATE
:
488 data
= readl(base
+ GPIO_SR
);
489 *config
= (data
>> pin
) & 1;
491 case PINCFG_TYPE_INPUT_TYPE
:
492 data
= readl(base
+ GPIO_TYPE
);
493 *config
= (data
>> pin
) & 1;
503 /* set the pin config settings for a specified pin group */
504 static int exynos5440_pinconf_group_set(struct pinctrl_dev
*pctldev
,
505 unsigned group
, unsigned long *configs
,
506 unsigned num_configs
)
508 struct exynos5440_pinctrl_priv_data
*priv
;
509 const unsigned int *pins
;
512 priv
= pinctrl_dev_get_drvdata(pctldev
);
513 pins
= priv
->pin_groups
[group
].pins
;
515 for (cnt
= 0; cnt
< priv
->pin_groups
[group
].num_pins
; cnt
++)
516 exynos5440_pinconf_set(pctldev
, pins
[cnt
], configs
,
522 /* get the pin config settings for a specified pin group */
523 static int exynos5440_pinconf_group_get(struct pinctrl_dev
*pctldev
,
524 unsigned int group
, unsigned long *config
)
526 struct exynos5440_pinctrl_priv_data
*priv
;
527 const unsigned int *pins
;
529 priv
= pinctrl_dev_get_drvdata(pctldev
);
530 pins
= priv
->pin_groups
[group
].pins
;
531 exynos5440_pinconf_get(pctldev
, pins
[0], config
);
535 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
536 static const struct pinconf_ops exynos5440_pinconf_ops
= {
537 .pin_config_get
= exynos5440_pinconf_get
,
538 .pin_config_set
= exynos5440_pinconf_set
,
539 .pin_config_group_get
= exynos5440_pinconf_group_get
,
540 .pin_config_group_set
= exynos5440_pinconf_group_set
,
543 /* gpiolib gpio_set callback function */
544 static void exynos5440_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
546 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
547 void __iomem
*base
= priv
->reg_base
;
550 data
= readl(base
+ GPIO_VAL
);
551 data
&= ~(1 << offset
);
554 writel(data
, base
+ GPIO_VAL
);
557 /* gpiolib gpio_get callback function */
558 static int exynos5440_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
560 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
561 void __iomem
*base
= priv
->reg_base
;
564 data
= readl(base
+ GPIO_IN
);
570 /* gpiolib gpio_direction_input callback function */
571 static int exynos5440_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
573 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
574 void __iomem
*base
= priv
->reg_base
;
577 /* first disable the data output enable on this pin */
578 data
= readl(base
+ GPIO_OE
);
579 data
&= ~(1 << offset
);
580 writel(data
, base
+ GPIO_OE
);
582 /* now enable input on this pin */
583 data
= readl(base
+ GPIO_IE
);
585 writel(data
, base
+ GPIO_IE
);
589 /* gpiolib gpio_direction_output callback function */
590 static int exynos5440_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
,
593 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
594 void __iomem
*base
= priv
->reg_base
;
597 exynos5440_gpio_set(gc
, offset
, value
);
599 /* first disable the data input enable on this pin */
600 data
= readl(base
+ GPIO_IE
);
601 data
&= ~(1 << offset
);
602 writel(data
, base
+ GPIO_IE
);
604 /* now enable output on this pin */
605 data
= readl(base
+ GPIO_OE
);
607 writel(data
, base
+ GPIO_OE
);
611 /* gpiolib gpio_to_irq callback function */
612 static int exynos5440_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
614 struct exynos5440_pinctrl_priv_data
*priv
= gpiochip_get_data(gc
);
617 if (offset
< 16 || offset
> 23)
620 if (!priv
->irq_domain
)
623 virq
= irq_create_mapping(priv
->irq_domain
, offset
- 16);
624 return virq
? : -ENXIO
;
627 /* parse the pin numbers listed in the 'samsung,exynos5440-pins' property */
628 static int exynos5440_pinctrl_parse_dt_pins(struct platform_device
*pdev
,
629 struct device_node
*cfg_np
, unsigned int **pin_list
,
632 struct device
*dev
= &pdev
->dev
;
633 struct property
*prop
;
635 prop
= of_find_property(cfg_np
, "samsung,exynos5440-pins", NULL
);
639 *npins
= prop
->length
/ sizeof(unsigned long);
641 dev_err(dev
, "invalid pin list in %s node", cfg_np
->name
);
645 *pin_list
= devm_kzalloc(dev
, *npins
* sizeof(**pin_list
), GFP_KERNEL
);
649 return of_property_read_u32_array(cfg_np
, "samsung,exynos5440-pins",
654 * Parse the information about all the available pin groups and pin functions
655 * from device node of the pin-controller.
657 static int exynos5440_pinctrl_parse_dt(struct platform_device
*pdev
,
658 struct exynos5440_pinctrl_priv_data
*priv
)
660 struct device
*dev
= &pdev
->dev
;
661 struct device_node
*dev_np
= dev
->of_node
;
662 struct device_node
*cfg_np
;
663 struct exynos5440_pin_group
*groups
, *grp
;
664 struct exynos5440_pmx_func
*functions
, *func
;
666 unsigned int npins
, grp_cnt
, func_idx
= 0;
670 grp_cnt
= of_get_child_count(dev_np
);
674 groups
= devm_kzalloc(dev
, grp_cnt
* sizeof(*groups
), GFP_KERNEL
);
680 functions
= devm_kzalloc(dev
, grp_cnt
* sizeof(*functions
), GFP_KERNEL
);
687 * Iterate over all the child nodes of the pin controller node
688 * and create pin groups and pin function lists.
690 for_each_child_of_node(dev_np
, cfg_np
) {
693 ret
= exynos5440_pinctrl_parse_dt_pins(pdev
, cfg_np
,
697 goto skip_to_pin_function
;
700 /* derive pin group name from the node name */
701 gname
= devm_kasprintf(dev
, GFP_KERNEL
,
702 "%s%s", cfg_np
->name
, GROUP_SUFFIX
);
707 grp
->pins
= pin_list
;
708 grp
->num_pins
= npins
;
711 skip_to_pin_function
:
712 ret
= of_property_read_u32(cfg_np
, "samsung,exynos5440-pin-function",
717 /* derive function name from the node name */
718 fname
= devm_kasprintf(dev
, GFP_KERNEL
,
719 "%s%s", cfg_np
->name
, FUNCTION_SUFFIX
);
724 func
->groups
= devm_kzalloc(dev
, sizeof(char *), GFP_KERNEL
);
727 func
->groups
[0] = gname
;
728 func
->num_groups
= gname
? 1 : 0;
729 func
->function
= function
;
734 priv
->pin_groups
= groups
;
735 priv
->nr_groups
= grp_cnt
;
736 priv
->pmx_functions
= functions
;
737 priv
->nr_functions
= func_idx
;
741 /* register the pinctrl interface with the pinctrl subsystem */
742 static int exynos5440_pinctrl_register(struct platform_device
*pdev
,
743 struct exynos5440_pinctrl_priv_data
*priv
)
745 struct device
*dev
= &pdev
->dev
;
746 struct pinctrl_desc
*ctrldesc
;
747 struct pinctrl_dev
*pctl_dev
;
748 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
752 ctrldesc
= devm_kzalloc(dev
, sizeof(*ctrldesc
), GFP_KERNEL
);
756 ctrldesc
->name
= "exynos5440-pinctrl";
757 ctrldesc
->owner
= THIS_MODULE
;
758 ctrldesc
->pctlops
= &exynos5440_pctrl_ops
;
759 ctrldesc
->pmxops
= &exynos5440_pinmux_ops
;
760 ctrldesc
->confops
= &exynos5440_pinconf_ops
;
762 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
763 EXYNOS5440_MAX_PINS
, GFP_KERNEL
);
766 ctrldesc
->pins
= pindesc
;
767 ctrldesc
->npins
= EXYNOS5440_MAX_PINS
;
769 /* dynamically populate the pin number and pin name for pindesc */
770 for (pin
= 0, pdesc
= pindesc
; pin
< ctrldesc
->npins
; pin
++, pdesc
++)
774 * allocate space for storing the dynamically generated names for all
775 * the pins which belong to this pin-controller.
777 pin_names
= devm_kzalloc(&pdev
->dev
, sizeof(char) * PIN_NAME_LENGTH
*
778 ctrldesc
->npins
, GFP_KERNEL
);
782 /* for each pin, set the name of the pin */
783 for (pin
= 0; pin
< ctrldesc
->npins
; pin
++) {
784 snprintf(pin_names
, 6, "gpio%02d", pin
);
785 pdesc
= pindesc
+ pin
;
786 pdesc
->name
= pin_names
;
787 pin_names
+= PIN_NAME_LENGTH
;
790 ret
= exynos5440_pinctrl_parse_dt(pdev
, priv
);
794 pctl_dev
= devm_pinctrl_register(&pdev
->dev
, ctrldesc
, priv
);
795 if (IS_ERR(pctl_dev
)) {
796 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
797 return PTR_ERR(pctl_dev
);
800 priv
->range
.name
= "exynos5440-pctrl-gpio-range";
802 priv
->range
.base
= 0;
803 priv
->range
.npins
= EXYNOS5440_MAX_PINS
;
804 priv
->range
.gc
= priv
->gc
;
805 pinctrl_add_gpio_range(pctl_dev
, &priv
->range
);
809 /* register the gpiolib interface with the gpiolib subsystem */
810 static int exynos5440_gpiolib_register(struct platform_device
*pdev
,
811 struct exynos5440_pinctrl_priv_data
*priv
)
813 struct gpio_chip
*gc
;
816 gc
= devm_kzalloc(&pdev
->dev
, sizeof(*gc
), GFP_KERNEL
);
822 gc
->ngpio
= EXYNOS5440_MAX_PINS
;
823 gc
->parent
= &pdev
->dev
;
824 gc
->set
= exynos5440_gpio_set
;
825 gc
->get
= exynos5440_gpio_get
;
826 gc
->direction_input
= exynos5440_gpio_direction_input
;
827 gc
->direction_output
= exynos5440_gpio_direction_output
;
828 gc
->to_irq
= exynos5440_gpio_to_irq
;
829 gc
->label
= "gpiolib-exynos5440";
830 gc
->owner
= THIS_MODULE
;
831 ret
= gpiochip_add_data(gc
, priv
);
833 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error "
834 "code: %d\n", gc
->label
, ret
);
841 /* unregister the gpiolib interface with the gpiolib subsystem */
842 static int exynos5440_gpiolib_unregister(struct platform_device
*pdev
,
843 struct exynos5440_pinctrl_priv_data
*priv
)
845 gpiochip_remove(priv
->gc
);
849 static void exynos5440_gpio_irq_unmask(struct irq_data
*irqd
)
851 struct exynos5440_pinctrl_priv_data
*d
;
852 unsigned long gpio_int
;
854 d
= irq_data_get_irq_chip_data(irqd
);
855 gpio_int
= readl(d
->reg_base
+ GPIO_INT
);
856 gpio_int
|= 1 << irqd
->hwirq
;
857 writel(gpio_int
, d
->reg_base
+ GPIO_INT
);
860 static void exynos5440_gpio_irq_mask(struct irq_data
*irqd
)
862 struct exynos5440_pinctrl_priv_data
*d
;
863 unsigned long gpio_int
;
865 d
= irq_data_get_irq_chip_data(irqd
);
866 gpio_int
= readl(d
->reg_base
+ GPIO_INT
);
867 gpio_int
&= ~(1 << irqd
->hwirq
);
868 writel(gpio_int
, d
->reg_base
+ GPIO_INT
);
871 /* irq_chip for gpio interrupts */
872 static struct irq_chip exynos5440_gpio_irq_chip
= {
873 .name
= "exynos5440_gpio_irq_chip",
874 .irq_unmask
= exynos5440_gpio_irq_unmask
,
875 .irq_mask
= exynos5440_gpio_irq_mask
,
878 /* interrupt handler for GPIO interrupts 0..7 */
879 static irqreturn_t
exynos5440_gpio_irq(int irq
, void *data
)
881 struct exynos5440_gpio_intr_data
*intd
= data
;
882 struct exynos5440_pinctrl_priv_data
*d
= intd
->priv
;
885 virq
= irq_linear_revmap(d
->irq_domain
, intd
->gpio_int
);
888 generic_handle_irq(virq
);
892 static int exynos5440_gpio_irq_map(struct irq_domain
*h
, unsigned int virq
,
895 struct exynos5440_pinctrl_priv_data
*d
= h
->host_data
;
897 irq_set_chip_data(virq
, d
);
898 irq_set_chip_and_handler(virq
, &exynos5440_gpio_irq_chip
,
903 /* irq domain callbacks for gpio interrupt controller */
904 static const struct irq_domain_ops exynos5440_gpio_irqd_ops
= {
905 .map
= exynos5440_gpio_irq_map
,
906 .xlate
= irq_domain_xlate_twocell
,
909 /* setup handling of gpio interrupts */
910 static int exynos5440_gpio_irq_init(struct platform_device
*pdev
,
911 struct exynos5440_pinctrl_priv_data
*priv
)
913 struct device
*dev
= &pdev
->dev
;
914 struct exynos5440_gpio_intr_data
*intd
;
917 intd
= devm_kzalloc(dev
, sizeof(*intd
) * EXYNOS5440_MAX_GPIO_INT
,
922 for (i
= 0; i
< EXYNOS5440_MAX_GPIO_INT
; i
++) {
923 irq
= irq_of_parse_and_map(dev
->of_node
, i
);
925 dev_err(dev
, "irq parsing failed\n");
931 ret
= devm_request_irq(dev
, irq
, exynos5440_gpio_irq
,
932 0, dev_name(dev
), intd
++);
934 dev_err(dev
, "irq request failed\n");
939 priv
->irq_domain
= irq_domain_add_linear(dev
->of_node
,
940 EXYNOS5440_MAX_GPIO_INT
,
941 &exynos5440_gpio_irqd_ops
, priv
);
942 if (!priv
->irq_domain
) {
943 dev_err(dev
, "failed to create irq domain\n");
950 static int exynos5440_pinctrl_probe(struct platform_device
*pdev
)
952 struct device
*dev
= &pdev
->dev
;
953 struct exynos5440_pinctrl_priv_data
*priv
;
954 struct resource
*res
;
958 dev_err(dev
, "device tree node not found\n");
962 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
966 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
967 priv
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
968 if (IS_ERR(priv
->reg_base
))
969 return PTR_ERR(priv
->reg_base
);
971 ret
= exynos5440_gpiolib_register(pdev
, priv
);
975 ret
= exynos5440_pinctrl_register(pdev
, priv
);
977 exynos5440_gpiolib_unregister(pdev
, priv
);
981 ret
= exynos5440_gpio_irq_init(pdev
, priv
);
983 dev_err(dev
, "failed to setup gpio interrupts\n");
987 platform_set_drvdata(pdev
, priv
);
988 dev_info(dev
, "EXYNOS5440 pinctrl driver registered\n");
992 static const struct of_device_id exynos5440_pinctrl_dt_match
[] = {
993 { .compatible
= "samsung,exynos5440-pinctrl" },
997 static struct platform_driver exynos5440_pinctrl_driver
= {
998 .probe
= exynos5440_pinctrl_probe
,
1000 .name
= "exynos5440-pinctrl",
1001 .of_match_table
= exynos5440_pinctrl_dt_match
,
1002 .suppress_bind_attrs
= true,
1006 static int __init
exynos5440_pinctrl_drv_register(void)
1008 return platform_driver_register(&exynos5440_pinctrl_driver
);
1010 postcore_initcall(exynos5440_pinctrl_drv_register
);