1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 Intel Corporation */
4 #include <linux/gpio/driver.h>
5 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/of_irq.h>
9 #include <linux/pinctrl/pinctrl.h>
10 #include <linux/pinctrl/pinconf.h>
11 #include <linux/pinctrl/pinconf-generic.h>
12 #include <linux/pinctrl/pinmux.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
19 #include "pinctrl-equilibrium.h"
21 #define PIN_NAME_FMT "io-%d"
22 #define PIN_NAME_LEN 10
23 #define PAD_REG_OFF 0x100
25 static void eqbr_gpio_disable_irq(struct irq_data
*d
)
27 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
28 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
29 unsigned int offset
= irqd_to_hwirq(d
);
32 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
33 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNENCLR
);
34 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
35 gpiochip_disable_irq(gc
, offset
);
38 static void eqbr_gpio_enable_irq(struct irq_data
*d
)
40 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
41 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
42 unsigned int offset
= irqd_to_hwirq(d
);
45 gc
->direction_input(gc
, offset
);
46 gpiochip_enable_irq(gc
, offset
);
47 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
48 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNRNSET
);
49 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
52 static void eqbr_gpio_ack_irq(struct irq_data
*d
)
54 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
55 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
56 unsigned int offset
= irqd_to_hwirq(d
);
59 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
60 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNCR
);
61 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
64 static void eqbr_gpio_mask_ack_irq(struct irq_data
*d
)
66 eqbr_gpio_disable_irq(d
);
70 static inline void eqbr_cfg_bit(void __iomem
*addr
,
71 unsigned int offset
, unsigned int set
)
74 writel(readl(addr
) | BIT(offset
), addr
);
76 writel(readl(addr
) & ~BIT(offset
), addr
);
79 static int eqbr_irq_type_cfg(struct gpio_irq_type
*type
,
80 struct eqbr_gpio_ctrl
*gctrl
,
85 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
86 eqbr_cfg_bit(gctrl
->membase
+ GPIO_IRNCFG
, offset
, type
->trig_type
);
87 eqbr_cfg_bit(gctrl
->membase
+ GPIO_EXINTCR1
, offset
, type
->trig_type
);
88 eqbr_cfg_bit(gctrl
->membase
+ GPIO_EXINTCR0
, offset
, type
->logic_type
);
89 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
94 static int eqbr_gpio_set_irq_type(struct irq_data
*d
, unsigned int type
)
96 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
97 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
98 unsigned int offset
= irqd_to_hwirq(d
);
99 struct gpio_irq_type it
;
101 memset(&it
, 0, sizeof(it
));
103 if ((type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_NONE
)
107 case IRQ_TYPE_EDGE_RISING
:
108 it
.trig_type
= GPIO_EDGE_TRIG
;
109 it
.edge_type
= GPIO_SINGLE_EDGE
;
110 it
.logic_type
= GPIO_POSITIVE_TRIG
;
113 case IRQ_TYPE_EDGE_FALLING
:
114 it
.trig_type
= GPIO_EDGE_TRIG
;
115 it
.edge_type
= GPIO_SINGLE_EDGE
;
116 it
.logic_type
= GPIO_NEGATIVE_TRIG
;
119 case IRQ_TYPE_EDGE_BOTH
:
120 it
.trig_type
= GPIO_EDGE_TRIG
;
121 it
.edge_type
= GPIO_BOTH_EDGE
;
122 it
.logic_type
= GPIO_POSITIVE_TRIG
;
125 case IRQ_TYPE_LEVEL_HIGH
:
126 it
.trig_type
= GPIO_LEVEL_TRIG
;
127 it
.edge_type
= GPIO_SINGLE_EDGE
;
128 it
.logic_type
= GPIO_POSITIVE_TRIG
;
131 case IRQ_TYPE_LEVEL_LOW
:
132 it
.trig_type
= GPIO_LEVEL_TRIG
;
133 it
.edge_type
= GPIO_SINGLE_EDGE
;
134 it
.logic_type
= GPIO_NEGATIVE_TRIG
;
141 eqbr_irq_type_cfg(&it
, gctrl
, offset
);
142 if (it
.trig_type
== GPIO_EDGE_TRIG
)
143 irq_set_handler_locked(d
, handle_edge_irq
);
145 irq_set_handler_locked(d
, handle_level_irq
);
150 static void eqbr_irq_handler(struct irq_desc
*desc
)
152 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
153 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
154 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
155 unsigned long pins
, offset
;
157 chained_irq_enter(ic
, desc
);
158 pins
= readl(gctrl
->membase
+ GPIO_IRNCR
);
160 for_each_set_bit(offset
, &pins
, gc
->ngpio
)
161 generic_handle_domain_irq(gc
->irq
.domain
, offset
);
163 chained_irq_exit(ic
, desc
);
166 static const struct irq_chip eqbr_irq_chip
= {
168 .irq_mask
= eqbr_gpio_disable_irq
,
169 .irq_unmask
= eqbr_gpio_enable_irq
,
170 .irq_ack
= eqbr_gpio_ack_irq
,
171 .irq_mask_ack
= eqbr_gpio_mask_ack_irq
,
172 .irq_set_type
= eqbr_gpio_set_irq_type
,
173 .flags
= IRQCHIP_IMMUTABLE
,
174 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
177 static int gpiochip_setup(struct device
*dev
, struct eqbr_gpio_ctrl
*gctrl
)
179 struct gpio_irq_chip
*girq
;
180 struct gpio_chip
*gc
;
183 gc
->label
= gctrl
->name
;
184 gc
->fwnode
= gctrl
->fwnode
;
186 if (!fwnode_property_read_bool(gctrl
->fwnode
, "interrupt-controller")) {
187 dev_dbg(dev
, "gc %s: doesn't act as interrupt controller!\n",
192 girq
= &gctrl
->chip
.irq
;
193 gpio_irq_chip_set_chip(girq
, &eqbr_irq_chip
);
194 girq
->parent_handler
= eqbr_irq_handler
;
195 girq
->num_parents
= 1;
196 girq
->parents
= devm_kcalloc(dev
, 1, sizeof(*girq
->parents
), GFP_KERNEL
);
200 girq
->default_type
= IRQ_TYPE_NONE
;
201 girq
->handler
= handle_bad_irq
;
202 girq
->parents
[0] = gctrl
->virq
;
207 static int gpiolib_reg(struct eqbr_pinctrl_drv_data
*drvdata
)
209 struct device
*dev
= drvdata
->dev
;
210 struct eqbr_gpio_ctrl
*gctrl
;
211 struct device_node
*np
;
215 for (i
= 0; i
< drvdata
->nr_gpio_ctrls
; i
++) {
216 gctrl
= drvdata
->gpio_ctrls
+ i
;
217 np
= to_of_node(gctrl
->fwnode
);
219 gctrl
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "gpiochip%d", i
);
223 if (of_address_to_resource(np
, 0, &res
)) {
224 dev_err(dev
, "Failed to get GPIO register address\n");
228 gctrl
->membase
= devm_ioremap_resource(dev
, &res
);
229 if (IS_ERR(gctrl
->membase
))
230 return PTR_ERR(gctrl
->membase
);
232 gctrl
->virq
= irq_of_parse_and_map(np
, 0);
234 dev_err(dev
, "%s: failed to parse and map irq\n",
238 raw_spin_lock_init(&gctrl
->lock
);
240 ret
= bgpio_init(&gctrl
->chip
, dev
, gctrl
->bank
->nr_pins
/ 8,
241 gctrl
->membase
+ GPIO_IN
,
242 gctrl
->membase
+ GPIO_OUTSET
,
243 gctrl
->membase
+ GPIO_OUTCLR
,
244 gctrl
->membase
+ GPIO_DIR
,
247 dev_err(dev
, "unable to init generic GPIO\n");
251 ret
= gpiochip_setup(dev
, gctrl
);
255 ret
= devm_gpiochip_add_data(dev
, &gctrl
->chip
, gctrl
);
263 static inline struct eqbr_pin_bank
264 *find_pinbank_via_pin(struct eqbr_pinctrl_drv_data
*pctl
, unsigned int pin
)
266 struct eqbr_pin_bank
*bank
;
269 for (i
= 0; i
< pctl
->nr_banks
; i
++) {
270 bank
= &pctl
->pin_banks
[i
];
271 if (pin
>= bank
->pin_base
&&
272 (pin
- bank
->pin_base
) < bank
->nr_pins
)
279 static const struct pinctrl_ops eqbr_pctl_ops
= {
280 .get_groups_count
= pinctrl_generic_get_group_count
,
281 .get_group_name
= pinctrl_generic_get_group_name
,
282 .get_group_pins
= pinctrl_generic_get_group_pins
,
283 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
284 .dt_free_map
= pinconf_generic_dt_free_map
,
287 static int eqbr_set_pin_mux(struct eqbr_pinctrl_drv_data
*pctl
,
288 unsigned int pmx
, unsigned int pin
)
290 struct eqbr_pin_bank
*bank
;
295 bank
= find_pinbank_via_pin(pctl
, pin
);
297 dev_err(pctl
->dev
, "Couldn't find pin bank for pin %u\n", pin
);
301 offset
= pin
- bank
->pin_base
;
303 if (!(bank
->aval_pinmap
& BIT(offset
))) {
305 "PIN: %u is not valid, pinbase: %u, bitmap: %u\n",
306 pin
, bank
->pin_base
, bank
->aval_pinmap
);
310 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
311 writel(pmx
, mem
+ (offset
* 4));
312 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
316 static int eqbr_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
317 unsigned int selector
, unsigned int group
)
319 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
320 struct function_desc
*func
;
321 struct group_desc
*grp
;
322 unsigned int *pinmux
;
325 func
= pinmux_generic_get_function(pctldev
, selector
);
329 grp
= pinctrl_generic_get_group(pctldev
, group
);
334 for (i
= 0; i
< grp
->grp
.npins
; i
++)
335 eqbr_set_pin_mux(pctl
, pinmux
[i
], grp
->grp
.pins
[i
]);
340 static int eqbr_pinmux_gpio_request(struct pinctrl_dev
*pctldev
,
341 struct pinctrl_gpio_range
*range
,
344 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
346 return eqbr_set_pin_mux(pctl
, EQBR_GPIO_MODE
, pin
);
349 static const struct pinmux_ops eqbr_pinmux_ops
= {
350 .get_functions_count
= pinmux_generic_get_function_count
,
351 .get_function_name
= pinmux_generic_get_function_name
,
352 .get_function_groups
= pinmux_generic_get_function_groups
,
353 .set_mux
= eqbr_pinmux_set_mux
,
354 .gpio_request_enable
= eqbr_pinmux_gpio_request
,
358 static int get_drv_cur(void __iomem
*mem
, unsigned int offset
)
360 unsigned int idx
= offset
/ DRV_CUR_PINS
; /* 0-15, 16-31 per register*/
361 unsigned int pin_offset
= offset
% DRV_CUR_PINS
;
363 return PARSE_DRV_CURRENT(readl(mem
+ REG_DRCC(idx
)), pin_offset
);
366 static struct eqbr_gpio_ctrl
367 *get_gpio_ctrls_via_bank(struct eqbr_pinctrl_drv_data
*pctl
,
368 struct eqbr_pin_bank
*bank
)
372 for (i
= 0; i
< pctl
->nr_gpio_ctrls
; i
++) {
373 if (pctl
->gpio_ctrls
[i
].bank
== bank
)
374 return &pctl
->gpio_ctrls
[i
];
380 static int eqbr_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
381 unsigned long *config
)
383 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
384 enum pin_config_param param
= pinconf_to_config_param(*config
);
385 struct eqbr_gpio_ctrl
*gctrl
;
386 struct eqbr_pin_bank
*bank
;
392 bank
= find_pinbank_via_pin(pctl
, pin
);
394 dev_err(pctl
->dev
, "Couldn't find pin bank for pin %u\n", pin
);
398 offset
= pin
- bank
->pin_base
;
400 if (!(bank
->aval_pinmap
& BIT(offset
))) {
402 "PIN: %u is not valid, pinbase: %u, bitmap: %u\n",
403 pin
, bank
->pin_base
, bank
->aval_pinmap
);
407 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
409 case PIN_CONFIG_BIAS_PULL_UP
:
410 val
= !!(readl(mem
+ REG_PUEN
) & BIT(offset
));
412 case PIN_CONFIG_BIAS_PULL_DOWN
:
413 val
= !!(readl(mem
+ REG_PDEN
) & BIT(offset
));
415 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
416 val
= !!(readl(mem
+ REG_OD
) & BIT(offset
));
418 case PIN_CONFIG_DRIVE_STRENGTH
:
419 val
= get_drv_cur(mem
, offset
);
421 case PIN_CONFIG_SLEW_RATE
:
422 val
= !!(readl(mem
+ REG_SRC
) & BIT(offset
));
424 case PIN_CONFIG_OUTPUT_ENABLE
:
425 gctrl
= get_gpio_ctrls_via_bank(pctl
, bank
);
427 dev_err(pctl
->dev
, "Failed to find gpio via bank pinbase: %u, pin: %u\n",
428 bank
->pin_base
, pin
);
429 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
432 val
= !!(readl(gctrl
->membase
+ GPIO_DIR
) & BIT(offset
));
435 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
438 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
439 *config
= pinconf_to_config_packed(param
, val
);
444 static int eqbr_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
445 unsigned long *configs
, unsigned int num_configs
)
447 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
448 struct eqbr_gpio_ctrl
*gctrl
;
449 enum pin_config_param param
;
450 struct eqbr_pin_bank
*bank
;
451 unsigned int val
, offset
;
452 struct gpio_chip
*gc
;
458 for (i
= 0; i
< num_configs
; i
++) {
459 param
= pinconf_to_config_param(configs
[i
]);
460 val
= pinconf_to_config_argument(configs
[i
]);
462 bank
= find_pinbank_via_pin(pctl
, pin
);
465 "Couldn't find pin bank for pin %u\n", pin
);
469 offset
= pin
- bank
->pin_base
;
472 case PIN_CONFIG_BIAS_PULL_UP
:
476 case PIN_CONFIG_BIAS_PULL_DOWN
:
480 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
484 case PIN_CONFIG_DRIVE_STRENGTH
:
485 mem
+= REG_DRCC(offset
/ DRV_CUR_PINS
);
486 offset
= (offset
% DRV_CUR_PINS
) * 2;
487 mask
= GENMASK(1, 0) << offset
;
489 case PIN_CONFIG_SLEW_RATE
:
493 case PIN_CONFIG_OUTPUT_ENABLE
:
494 gctrl
= get_gpio_ctrls_via_bank(pctl
, bank
);
496 dev_err(pctl
->dev
, "Failed to find gpio via bank pinbase: %u, pin: %u\n",
497 bank
->pin_base
, pin
);
501 gc
->direction_output(gc
, offset
, 0);
507 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
509 regval
= (regval
& ~mask
) | ((val
<< offset
) & mask
);
511 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
517 static int eqbr_pinconf_group_get(struct pinctrl_dev
*pctldev
,
518 unsigned int group
, unsigned long *config
)
520 unsigned int i
, npins
, old
= 0;
521 const unsigned int *pins
;
524 ret
= pinctrl_generic_get_group_pins(pctldev
, group
, &pins
, &npins
);
528 for (i
= 0; i
< npins
; i
++) {
529 if (eqbr_pinconf_get(pctldev
, pins
[i
], config
))
532 if (i
&& old
!= *config
)
540 static int eqbr_pinconf_group_set(struct pinctrl_dev
*pctldev
,
541 unsigned int group
, unsigned long *configs
,
542 unsigned int num_configs
)
544 const unsigned int *pins
;
545 unsigned int i
, npins
;
548 ret
= pinctrl_generic_get_group_pins(pctldev
, group
, &pins
, &npins
);
552 for (i
= 0; i
< npins
; i
++) {
553 ret
= eqbr_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
);
560 static const struct pinconf_ops eqbr_pinconf_ops
= {
562 .pin_config_get
= eqbr_pinconf_get
,
563 .pin_config_set
= eqbr_pinconf_set
,
564 .pin_config_group_get
= eqbr_pinconf_group_get
,
565 .pin_config_group_set
= eqbr_pinconf_group_set
,
566 .pin_config_config_dbg_show
= pinconf_generic_dump_config
,
569 static bool is_func_exist(struct pinfunction
*funcs
, const char *name
,
570 unsigned int nr_funcs
, unsigned int *idx
)
577 for (i
= 0; i
< nr_funcs
; i
++) {
578 if (funcs
[i
].name
&& !strcmp(funcs
[i
].name
, name
)) {
587 static int funcs_utils(struct device
*dev
, struct pinfunction
*funcs
,
588 unsigned int *nr_funcs
, funcs_util_ops op
)
590 struct device_node
*node
= dev
->of_node
;
591 struct property
*prop
;
598 for_each_child_of_node_scoped(node
, np
) {
599 prop
= of_find_property(np
, "groups", NULL
);
603 if (of_property_read_string(np
, "function", &fn_name
)) {
604 /* some groups may not have function, it's OK */
605 dev_dbg(dev
, "Group %s: not function binded!\n",
606 (char *)prop
->value
);
611 case OP_COUNT_NR_FUNCS
:
612 if (!is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
613 *nr_funcs
= *nr_funcs
+ 1;
617 if (!is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
618 funcs
[i
].name
= fn_name
;
621 case OP_COUNT_NR_FUNC_GRPS
:
622 if (is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
623 funcs
[fid
].ngroups
++;
626 case OP_ADD_FUNC_GRPS
:
627 if (is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
)) {
628 groups
= (const char **)funcs
[fid
].groups
;
629 for (j
= 0; j
< funcs
[fid
].ngroups
; j
++)
632 groups
[j
] = prop
->value
;
645 static int eqbr_build_functions(struct eqbr_pinctrl_drv_data
*drvdata
)
647 struct device
*dev
= drvdata
->dev
;
648 struct pinfunction
*funcs
= NULL
;
649 unsigned int nr_funcs
= 0;
652 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_COUNT_NR_FUNCS
);
656 funcs
= devm_kcalloc(dev
, nr_funcs
, sizeof(*funcs
), GFP_KERNEL
);
660 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_ADD_FUNCS
);
664 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_COUNT_NR_FUNC_GRPS
);
668 for (i
= 0; i
< nr_funcs
; i
++) {
669 if (!funcs
[i
].ngroups
)
671 funcs
[i
].groups
= devm_kcalloc(dev
, funcs
[i
].ngroups
,
672 sizeof(*(funcs
[i
].groups
)),
674 if (!funcs
[i
].groups
)
678 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_ADD_FUNC_GRPS
);
682 for (i
= 0; i
< nr_funcs
; i
++) {
684 /* Ignore the same function with multiple groups */
685 if (funcs
[i
].name
== NULL
)
688 ret
= pinmux_generic_add_function(drvdata
->pctl_dev
,
694 dev_err(dev
, "Failed to register function %s\n",
703 static int eqbr_build_groups(struct eqbr_pinctrl_drv_data
*drvdata
)
705 struct device
*dev
= drvdata
->dev
;
706 struct device_node
*node
= dev
->of_node
;
707 unsigned int *pins
, *pinmux
, pin_id
, pinmux_id
;
708 struct pingroup group
, *grp
= &group
;
709 struct property
*prop
;
712 for_each_child_of_node_scoped(node
, np
) {
713 prop
= of_find_property(np
, "groups", NULL
);
717 err
= of_property_count_u32_elems(np
, "pins");
719 dev_err(dev
, "No pins in the group: %s\n", prop
->name
);
723 grp
->name
= prop
->value
;
724 pins
= devm_kcalloc(dev
, grp
->npins
, sizeof(*pins
), GFP_KERNEL
);
730 pinmux
= devm_kcalloc(dev
, grp
->npins
, sizeof(*pinmux
), GFP_KERNEL
);
734 for (j
= 0; j
< grp
->npins
; j
++) {
735 if (of_property_read_u32_index(np
, "pins", j
, &pin_id
)) {
736 dev_err(dev
, "Group %s: Read intel pins id failed\n",
740 if (pin_id
>= drvdata
->pctl_desc
.npins
) {
741 dev_err(dev
, "Group %s: Invalid pin ID, idx: %d, pin %u\n",
742 grp
->name
, j
, pin_id
);
746 if (of_property_read_u32_index(np
, "pinmux", j
, &pinmux_id
)) {
747 dev_err(dev
, "Group %s: Read intel pinmux id failed\n",
751 pinmux
[j
] = pinmux_id
;
754 err
= pinctrl_generic_add_group(drvdata
->pctl_dev
,
755 grp
->name
, grp
->pins
, grp
->npins
,
758 dev_err(dev
, "Failed to register group %s\n", grp
->name
);
761 memset(&group
, 0, sizeof(group
));
768 static int pinctrl_reg(struct eqbr_pinctrl_drv_data
*drvdata
)
770 struct pinctrl_desc
*pctl_desc
;
771 struct pinctrl_pin_desc
*pdesc
;
773 unsigned int nr_pins
;
778 pctl_desc
= &drvdata
->pctl_desc
;
779 pctl_desc
->name
= "eqbr-pinctrl";
780 pctl_desc
->owner
= THIS_MODULE
;
781 pctl_desc
->pctlops
= &eqbr_pctl_ops
;
782 pctl_desc
->pmxops
= &eqbr_pinmux_ops
;
783 pctl_desc
->confops
= &eqbr_pinconf_ops
;
784 raw_spin_lock_init(&drvdata
->lock
);
786 for (i
= 0, nr_pins
= 0; i
< drvdata
->nr_banks
; i
++)
787 nr_pins
+= drvdata
->pin_banks
[i
].nr_pins
;
789 pdesc
= devm_kcalloc(dev
, nr_pins
, sizeof(*pdesc
), GFP_KERNEL
);
792 pin_names
= devm_kcalloc(dev
, nr_pins
, PIN_NAME_LEN
, GFP_KERNEL
);
796 for (i
= 0; i
< nr_pins
; i
++) {
797 sprintf(pin_names
, PIN_NAME_FMT
, i
);
799 pdesc
[i
].name
= pin_names
;
800 pin_names
+= PIN_NAME_LEN
;
802 pctl_desc
->pins
= pdesc
;
803 pctl_desc
->npins
= nr_pins
;
804 dev_dbg(dev
, "pinctrl total pin number: %u\n", nr_pins
);
806 ret
= devm_pinctrl_register_and_init(dev
, pctl_desc
, drvdata
,
811 ret
= eqbr_build_groups(drvdata
);
813 dev_err(dev
, "Failed to build groups\n");
817 ret
= eqbr_build_functions(drvdata
);
819 dev_err(dev
, "Failed to build functions\n");
823 return pinctrl_enable(drvdata
->pctl_dev
);
826 static int pinbank_init(struct device_node
*np
,
827 struct eqbr_pinctrl_drv_data
*drvdata
,
828 struct eqbr_pin_bank
*bank
, unsigned int id
)
830 struct device
*dev
= drvdata
->dev
;
831 struct of_phandle_args spec
;
834 bank
->membase
= drvdata
->membase
+ id
* PAD_REG_OFF
;
836 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3, 0, &spec
);
838 dev_err(dev
, "gpio-range not available!\n");
842 bank
->pin_base
= spec
.args
[1];
843 bank
->nr_pins
= spec
.args
[2];
845 bank
->aval_pinmap
= readl(bank
->membase
+ REG_AVAIL
);
848 dev_dbg(dev
, "pinbank id: %d, reg: %px, pinbase: %u, pin number: %u, pinmap: 0x%x\n",
849 id
, bank
->membase
, bank
->pin_base
,
850 bank
->nr_pins
, bank
->aval_pinmap
);
855 static int pinbank_probe(struct eqbr_pinctrl_drv_data
*drvdata
)
857 struct device
*dev
= drvdata
->dev
;
858 struct device_node
*np_gpio
;
859 struct eqbr_gpio_ctrl
*gctrls
;
860 struct eqbr_pin_bank
*banks
;
863 /* Count gpio bank number */
865 for_each_node_by_name(np_gpio
, "gpio") {
866 if (of_device_is_available(np_gpio
))
871 dev_err(dev
, "NO pin bank available!\n");
875 /* Count pin bank number and gpio controller number */
876 banks
= devm_kcalloc(dev
, nr_gpio
, sizeof(*banks
), GFP_KERNEL
);
880 gctrls
= devm_kcalloc(dev
, nr_gpio
, sizeof(*gctrls
), GFP_KERNEL
);
884 dev_dbg(dev
, "found %d gpio controller!\n", nr_gpio
);
886 /* Initialize Pin bank */
888 for_each_node_by_name(np_gpio
, "gpio") {
889 if (!of_device_is_available(np_gpio
))
892 pinbank_init(np_gpio
, drvdata
, banks
+ i
, i
);
894 gctrls
[i
].fwnode
= of_fwnode_handle(np_gpio
);
895 gctrls
[i
].bank
= banks
+ i
;
899 drvdata
->pin_banks
= banks
;
900 drvdata
->nr_banks
= nr_gpio
;
901 drvdata
->gpio_ctrls
= gctrls
;
902 drvdata
->nr_gpio_ctrls
= nr_gpio
;
907 static int eqbr_pinctrl_probe(struct platform_device
*pdev
)
909 struct eqbr_pinctrl_drv_data
*drvdata
;
910 struct device
*dev
= &pdev
->dev
;
913 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
919 drvdata
->membase
= devm_platform_ioremap_resource(pdev
, 0);
920 if (IS_ERR(drvdata
->membase
))
921 return PTR_ERR(drvdata
->membase
);
923 ret
= pinbank_probe(drvdata
);
927 ret
= pinctrl_reg(drvdata
);
931 ret
= gpiolib_reg(drvdata
);
935 platform_set_drvdata(pdev
, drvdata
);
939 static const struct of_device_id eqbr_pinctrl_dt_match
[] = {
940 { .compatible
= "intel,lgm-io" },
943 MODULE_DEVICE_TABLE(of
, eqbr_pinctrl_dt_match
);
945 static struct platform_driver eqbr_pinctrl_driver
= {
946 .probe
= eqbr_pinctrl_probe
,
948 .name
= "eqbr-pinctrl",
949 .of_match_table
= eqbr_pinctrl_dt_match
,
953 module_platform_driver(eqbr_pinctrl_driver
);
955 MODULE_AUTHOR("Zhu Yixin <yixin.zhu@intel.com>, Rahul Tanwar <rahul.tanwar@intel.com>");
956 MODULE_DESCRIPTION("Pinctrl Driver for LGM SoC (Equilibrium)");
957 MODULE_LICENSE("GPL v2");