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>
18 #include "pinctrl-equilibrium.h"
20 #define PIN_NAME_FMT "io-%d"
21 #define PIN_NAME_LEN 10
22 #define PAD_REG_OFF 0x100
24 static void eqbr_gpio_disable_irq(struct irq_data
*d
)
26 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
27 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
28 unsigned int offset
= irqd_to_hwirq(d
);
31 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
32 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNENCLR
);
33 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
36 static void eqbr_gpio_enable_irq(struct irq_data
*d
)
38 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
39 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
40 unsigned int offset
= irqd_to_hwirq(d
);
43 gc
->direction_input(gc
, offset
);
44 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
45 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNRNSET
);
46 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
49 static void eqbr_gpio_ack_irq(struct irq_data
*d
)
51 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
52 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
53 unsigned int offset
= irqd_to_hwirq(d
);
56 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
57 writel(BIT(offset
), gctrl
->membase
+ GPIO_IRNCR
);
58 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
61 static void eqbr_gpio_mask_ack_irq(struct irq_data
*d
)
63 eqbr_gpio_disable_irq(d
);
67 static inline void eqbr_cfg_bit(void __iomem
*addr
,
68 unsigned int offset
, unsigned int set
)
71 writel(readl(addr
) | BIT(offset
), addr
);
73 writel(readl(addr
) & ~BIT(offset
), addr
);
76 static int eqbr_irq_type_cfg(struct gpio_irq_type
*type
,
77 struct eqbr_gpio_ctrl
*gctrl
,
82 raw_spin_lock_irqsave(&gctrl
->lock
, flags
);
83 eqbr_cfg_bit(gctrl
->membase
+ GPIO_IRNCFG
, offset
, type
->trig_type
);
84 eqbr_cfg_bit(gctrl
->membase
+ GPIO_EXINTCR1
, offset
, type
->trig_type
);
85 eqbr_cfg_bit(gctrl
->membase
+ GPIO_EXINTCR0
, offset
, type
->logic_type
);
86 raw_spin_unlock_irqrestore(&gctrl
->lock
, flags
);
91 static int eqbr_gpio_set_irq_type(struct irq_data
*d
, unsigned int type
)
93 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
94 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
95 unsigned int offset
= irqd_to_hwirq(d
);
96 struct gpio_irq_type it
;
98 memset(&it
, 0, sizeof(it
));
100 if ((type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_NONE
)
104 case IRQ_TYPE_EDGE_RISING
:
105 it
.trig_type
= GPIO_EDGE_TRIG
;
106 it
.edge_type
= GPIO_SINGLE_EDGE
;
107 it
.logic_type
= GPIO_POSITIVE_TRIG
;
110 case IRQ_TYPE_EDGE_FALLING
:
111 it
.trig_type
= GPIO_EDGE_TRIG
;
112 it
.edge_type
= GPIO_SINGLE_EDGE
;
113 it
.logic_type
= GPIO_NEGATIVE_TRIG
;
116 case IRQ_TYPE_EDGE_BOTH
:
117 it
.trig_type
= GPIO_EDGE_TRIG
;
118 it
.edge_type
= GPIO_BOTH_EDGE
;
119 it
.logic_type
= GPIO_POSITIVE_TRIG
;
122 case IRQ_TYPE_LEVEL_HIGH
:
123 it
.trig_type
= GPIO_LEVEL_TRIG
;
124 it
.edge_type
= GPIO_SINGLE_EDGE
;
125 it
.logic_type
= GPIO_POSITIVE_TRIG
;
128 case IRQ_TYPE_LEVEL_LOW
:
129 it
.trig_type
= GPIO_LEVEL_TRIG
;
130 it
.edge_type
= GPIO_SINGLE_EDGE
;
131 it
.logic_type
= GPIO_NEGATIVE_TRIG
;
138 eqbr_irq_type_cfg(&it
, gctrl
, offset
);
139 if (it
.trig_type
== GPIO_EDGE_TRIG
)
140 irq_set_handler_locked(d
, handle_edge_irq
);
142 irq_set_handler_locked(d
, handle_level_irq
);
147 static void eqbr_irq_handler(struct irq_desc
*desc
)
149 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
150 struct eqbr_gpio_ctrl
*gctrl
= gpiochip_get_data(gc
);
151 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
152 unsigned long pins
, offset
;
154 chained_irq_enter(ic
, desc
);
155 pins
= readl(gctrl
->membase
+ GPIO_IRNCR
);
157 for_each_set_bit(offset
, &pins
, gc
->ngpio
)
158 generic_handle_irq(irq_find_mapping(gc
->irq
.domain
, offset
));
160 chained_irq_exit(ic
, desc
);
163 static int gpiochip_setup(struct device
*dev
, struct eqbr_gpio_ctrl
*gctrl
)
165 struct gpio_irq_chip
*girq
;
166 struct gpio_chip
*gc
;
169 gc
->label
= gctrl
->name
;
170 #if defined(CONFIG_OF_GPIO)
171 gc
->of_node
= gctrl
->node
;
174 if (!of_property_read_bool(gctrl
->node
, "interrupt-controller")) {
175 dev_dbg(dev
, "gc %s: doesn't act as interrupt controller!\n",
180 gctrl
->ic
.name
= "gpio_irq";
181 gctrl
->ic
.irq_mask
= eqbr_gpio_disable_irq
;
182 gctrl
->ic
.irq_unmask
= eqbr_gpio_enable_irq
;
183 gctrl
->ic
.irq_ack
= eqbr_gpio_ack_irq
;
184 gctrl
->ic
.irq_mask_ack
= eqbr_gpio_mask_ack_irq
;
185 gctrl
->ic
.irq_set_type
= eqbr_gpio_set_irq_type
;
187 girq
= &gctrl
->chip
.irq
;
188 girq
->chip
= &gctrl
->ic
;
189 girq
->parent_handler
= eqbr_irq_handler
;
190 girq
->num_parents
= 1;
191 girq
->parents
= devm_kcalloc(dev
, 1, sizeof(*girq
->parents
), GFP_KERNEL
);
195 girq
->default_type
= IRQ_TYPE_NONE
;
196 girq
->handler
= handle_bad_irq
;
197 girq
->parents
[0] = gctrl
->virq
;
202 static int gpiolib_reg(struct eqbr_pinctrl_drv_data
*drvdata
)
204 struct device
*dev
= drvdata
->dev
;
205 struct eqbr_gpio_ctrl
*gctrl
;
206 struct device_node
*np
;
210 for (i
= 0; i
< drvdata
->nr_gpio_ctrls
; i
++) {
211 gctrl
= drvdata
->gpio_ctrls
+ i
;
214 gctrl
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "gpiochip%d", i
);
218 if (of_address_to_resource(np
, 0, &res
)) {
219 dev_err(dev
, "Failed to get GPIO register address\n");
223 gctrl
->membase
= devm_ioremap_resource(dev
, &res
);
224 if (IS_ERR(gctrl
->membase
))
225 return PTR_ERR(gctrl
->membase
);
227 gctrl
->virq
= irq_of_parse_and_map(np
, 0);
229 dev_err(dev
, "%s: failed to parse and map irq\n",
233 raw_spin_lock_init(&gctrl
->lock
);
235 ret
= bgpio_init(&gctrl
->chip
, dev
, gctrl
->bank
->nr_pins
/ 8,
236 gctrl
->membase
+ GPIO_IN
,
237 gctrl
->membase
+ GPIO_OUTSET
,
238 gctrl
->membase
+ GPIO_OUTCLR
,
239 gctrl
->membase
+ GPIO_DIR
,
242 dev_err(dev
, "unable to init generic GPIO\n");
246 ret
= gpiochip_setup(dev
, gctrl
);
250 ret
= devm_gpiochip_add_data(dev
, &gctrl
->chip
, gctrl
);
258 static inline struct eqbr_pin_bank
259 *find_pinbank_via_pin(struct eqbr_pinctrl_drv_data
*pctl
, unsigned int pin
)
261 struct eqbr_pin_bank
*bank
;
264 for (i
= 0; i
< pctl
->nr_banks
; i
++) {
265 bank
= &pctl
->pin_banks
[i
];
266 if (pin
>= bank
->pin_base
&&
267 (pin
- bank
->pin_base
) < bank
->nr_pins
)
274 static const struct pinctrl_ops eqbr_pctl_ops
= {
275 .get_groups_count
= pinctrl_generic_get_group_count
,
276 .get_group_name
= pinctrl_generic_get_group_name
,
277 .get_group_pins
= pinctrl_generic_get_group_pins
,
278 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
279 .dt_free_map
= pinconf_generic_dt_free_map
,
282 static int eqbr_set_pin_mux(struct eqbr_pinctrl_drv_data
*pctl
,
283 unsigned int pmx
, unsigned int pin
)
285 struct eqbr_pin_bank
*bank
;
290 bank
= find_pinbank_via_pin(pctl
, pin
);
292 dev_err(pctl
->dev
, "Couldn't find pin bank for pin %u\n", pin
);
296 offset
= pin
- bank
->pin_base
;
298 if (!(bank
->aval_pinmap
& BIT(offset
))) {
300 "PIN: %u is not valid, pinbase: %u, bitmap: %u\n",
301 pin
, bank
->pin_base
, bank
->aval_pinmap
);
305 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
306 writel(pmx
, mem
+ (offset
* 4));
307 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
311 static int eqbr_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
312 unsigned int selector
, unsigned int group
)
314 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
315 struct function_desc
*func
;
316 struct group_desc
*grp
;
317 unsigned int *pinmux
;
320 func
= pinmux_generic_get_function(pctldev
, selector
);
324 grp
= pinctrl_generic_get_group(pctldev
, group
);
329 for (i
= 0; i
< grp
->num_pins
; i
++)
330 eqbr_set_pin_mux(pctl
, pinmux
[i
], grp
->pins
[i
]);
335 static int eqbr_pinmux_gpio_request(struct pinctrl_dev
*pctldev
,
336 struct pinctrl_gpio_range
*range
,
339 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
341 return eqbr_set_pin_mux(pctl
, EQBR_GPIO_MODE
, pin
);
344 static const struct pinmux_ops eqbr_pinmux_ops
= {
345 .get_functions_count
= pinmux_generic_get_function_count
,
346 .get_function_name
= pinmux_generic_get_function_name
,
347 .get_function_groups
= pinmux_generic_get_function_groups
,
348 .set_mux
= eqbr_pinmux_set_mux
,
349 .gpio_request_enable
= eqbr_pinmux_gpio_request
,
353 static int get_drv_cur(void __iomem
*mem
, unsigned int offset
)
355 unsigned int idx
= offset
/ DRV_CUR_PINS
; /* 0-15, 16-31 per register*/
356 unsigned int pin_offset
= offset
% DRV_CUR_PINS
;
358 return PARSE_DRV_CURRENT(readl(mem
+ REG_DRCC(idx
)), pin_offset
);
361 static struct eqbr_gpio_ctrl
362 *get_gpio_ctrls_via_bank(struct eqbr_pinctrl_drv_data
*pctl
,
363 struct eqbr_pin_bank
*bank
)
367 for (i
= 0; i
< pctl
->nr_gpio_ctrls
; i
++) {
368 if (pctl
->gpio_ctrls
[i
].bank
== bank
)
369 return &pctl
->gpio_ctrls
[i
];
375 static int eqbr_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
376 unsigned long *config
)
378 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
379 enum pin_config_param param
= pinconf_to_config_param(*config
);
380 struct eqbr_gpio_ctrl
*gctrl
;
381 struct eqbr_pin_bank
*bank
;
387 bank
= find_pinbank_via_pin(pctl
, pin
);
389 dev_err(pctl
->dev
, "Couldn't find pin bank for pin %u\n", pin
);
393 offset
= pin
- bank
->pin_base
;
395 if (!(bank
->aval_pinmap
& BIT(offset
))) {
397 "PIN: %u is not valid, pinbase: %u, bitmap: %u\n",
398 pin
, bank
->pin_base
, bank
->aval_pinmap
);
402 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
404 case PIN_CONFIG_BIAS_PULL_UP
:
405 val
= !!(readl(mem
+ REG_PUEN
) & BIT(offset
));
407 case PIN_CONFIG_BIAS_PULL_DOWN
:
408 val
= !!(readl(mem
+ REG_PDEN
) & BIT(offset
));
410 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
411 val
= !!(readl(mem
+ REG_OD
) & BIT(offset
));
413 case PIN_CONFIG_DRIVE_STRENGTH
:
414 val
= get_drv_cur(mem
, offset
);
416 case PIN_CONFIG_SLEW_RATE
:
417 val
= !!(readl(mem
+ REG_SRC
) & BIT(offset
));
419 case PIN_CONFIG_OUTPUT_ENABLE
:
420 gctrl
= get_gpio_ctrls_via_bank(pctl
, bank
);
422 dev_err(pctl
->dev
, "Failed to find gpio via bank pinbase: %u, pin: %u\n",
423 bank
->pin_base
, pin
);
424 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
427 val
= !!(readl(gctrl
->membase
+ GPIO_DIR
) & BIT(offset
));
430 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
433 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
434 *config
= pinconf_to_config_packed(param
, val
);
439 static int eqbr_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
440 unsigned long *configs
, unsigned int num_configs
)
442 struct eqbr_pinctrl_drv_data
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
443 struct eqbr_gpio_ctrl
*gctrl
;
444 enum pin_config_param param
;
445 struct eqbr_pin_bank
*bank
;
446 unsigned int val
, offset
;
447 struct gpio_chip
*gc
;
453 for (i
= 0; i
< num_configs
; i
++) {
454 param
= pinconf_to_config_param(configs
[i
]);
455 val
= pinconf_to_config_argument(configs
[i
]);
457 bank
= find_pinbank_via_pin(pctl
, pin
);
460 "Couldn't find pin bank for pin %u\n", pin
);
464 offset
= pin
- bank
->pin_base
;
467 case PIN_CONFIG_BIAS_PULL_UP
:
471 case PIN_CONFIG_BIAS_PULL_DOWN
:
475 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
479 case PIN_CONFIG_DRIVE_STRENGTH
:
480 mem
+= REG_DRCC(offset
/ DRV_CUR_PINS
);
481 offset
= (offset
% DRV_CUR_PINS
) * 2;
482 mask
= GENMASK(1, 0) << offset
;
484 case PIN_CONFIG_SLEW_RATE
:
488 case PIN_CONFIG_OUTPUT_ENABLE
:
489 gctrl
= get_gpio_ctrls_via_bank(pctl
, bank
);
491 dev_err(pctl
->dev
, "Failed to find gpio via bank pinbase: %u, pin: %u\n",
492 bank
->pin_base
, pin
);
496 gc
->direction_output(gc
, offset
, 0);
502 raw_spin_lock_irqsave(&pctl
->lock
, flags
);
504 regval
= (regval
& ~mask
) | ((val
<< offset
) & mask
);
506 raw_spin_unlock_irqrestore(&pctl
->lock
, flags
);
512 static int eqbr_pinconf_group_get(struct pinctrl_dev
*pctldev
,
513 unsigned int group
, unsigned long *config
)
515 unsigned int i
, npins
, old
= 0;
516 const unsigned int *pins
;
519 ret
= pinctrl_generic_get_group_pins(pctldev
, group
, &pins
, &npins
);
523 for (i
= 0; i
< npins
; i
++) {
524 if (eqbr_pinconf_get(pctldev
, pins
[i
], config
))
527 if (i
&& old
!= *config
)
535 static int eqbr_pinconf_group_set(struct pinctrl_dev
*pctldev
,
536 unsigned int group
, unsigned long *configs
,
537 unsigned int num_configs
)
539 const unsigned int *pins
;
540 unsigned int i
, npins
;
543 ret
= pinctrl_generic_get_group_pins(pctldev
, group
, &pins
, &npins
);
547 for (i
= 0; i
< npins
; i
++) {
548 ret
= eqbr_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
);
555 static const struct pinconf_ops eqbr_pinconf_ops
= {
557 .pin_config_get
= eqbr_pinconf_get
,
558 .pin_config_set
= eqbr_pinconf_set
,
559 .pin_config_group_get
= eqbr_pinconf_group_get
,
560 .pin_config_group_set
= eqbr_pinconf_group_set
,
561 .pin_config_config_dbg_show
= pinconf_generic_dump_config
,
564 static bool is_func_exist(struct eqbr_pmx_func
*funcs
, const char *name
,
565 unsigned int nr_funcs
, unsigned int *idx
)
572 for (i
= 0; i
< nr_funcs
; i
++) {
573 if (funcs
[i
].name
&& !strcmp(funcs
[i
].name
, name
)) {
582 static int funcs_utils(struct device
*dev
, struct eqbr_pmx_func
*funcs
,
583 unsigned int *nr_funcs
, funcs_util_ops op
)
585 struct device_node
*node
= dev
->of_node
;
586 struct device_node
*np
;
587 struct property
*prop
;
593 for_each_child_of_node(node
, np
) {
594 prop
= of_find_property(np
, "groups", NULL
);
598 if (of_property_read_string(np
, "function", &fn_name
)) {
599 /* some groups may not have function, it's OK */
600 dev_dbg(dev
, "Group %s: not function binded!\n",
601 (char *)prop
->value
);
606 case OP_COUNT_NR_FUNCS
:
607 if (!is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
608 *nr_funcs
= *nr_funcs
+ 1;
612 if (!is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
613 funcs
[i
].name
= fn_name
;
616 case OP_COUNT_NR_FUNC_GRPS
:
617 if (is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
))
618 funcs
[fid
].nr_groups
++;
621 case OP_ADD_FUNC_GRPS
:
622 if (is_func_exist(funcs
, fn_name
, *nr_funcs
, &fid
)) {
623 for (j
= 0; j
< funcs
[fid
].nr_groups
; j
++)
624 if (!funcs
[fid
].groups
[j
])
626 funcs
[fid
].groups
[j
] = prop
->value
;
639 static int eqbr_build_functions(struct eqbr_pinctrl_drv_data
*drvdata
)
641 struct device
*dev
= drvdata
->dev
;
642 struct eqbr_pmx_func
*funcs
= NULL
;
643 unsigned int nr_funcs
= 0;
646 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_COUNT_NR_FUNCS
);
650 funcs
= devm_kcalloc(dev
, nr_funcs
, sizeof(*funcs
), GFP_KERNEL
);
654 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_ADD_FUNCS
);
658 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_COUNT_NR_FUNC_GRPS
);
662 for (i
= 0; i
< nr_funcs
; i
++) {
663 if (!funcs
[i
].nr_groups
)
665 funcs
[i
].groups
= devm_kcalloc(dev
, funcs
[i
].nr_groups
,
666 sizeof(*(funcs
[i
].groups
)),
668 if (!funcs
[i
].groups
)
672 ret
= funcs_utils(dev
, funcs
, &nr_funcs
, OP_ADD_FUNC_GRPS
);
676 for (i
= 0; i
< nr_funcs
; i
++) {
677 ret
= pinmux_generic_add_function(drvdata
->pctl_dev
,
683 dev_err(dev
, "Failed to register function %s\n",
692 static int eqbr_build_groups(struct eqbr_pinctrl_drv_data
*drvdata
)
694 struct device
*dev
= drvdata
->dev
;
695 struct device_node
*node
= dev
->of_node
;
696 unsigned int *pinmux
, pin_id
, pinmux_id
;
697 struct group_desc group
;
698 struct device_node
*np
;
699 struct property
*prop
;
702 for_each_child_of_node(node
, np
) {
703 prop
= of_find_property(np
, "groups", NULL
);
707 group
.num_pins
= of_property_count_u32_elems(np
, "pins");
708 if (group
.num_pins
< 0) {
709 dev_err(dev
, "No pins in the group: %s\n", prop
->name
);
712 group
.name
= prop
->value
;
713 group
.pins
= devm_kcalloc(dev
, group
.num_pins
,
714 sizeof(*(group
.pins
)), GFP_KERNEL
);
718 pinmux
= devm_kcalloc(dev
, group
.num_pins
, sizeof(*pinmux
),
723 for (j
= 0; j
< group
.num_pins
; j
++) {
724 if (of_property_read_u32_index(np
, "pins", j
, &pin_id
)) {
725 dev_err(dev
, "Group %s: Read intel pins id failed\n",
729 if (pin_id
>= drvdata
->pctl_desc
.npins
) {
730 dev_err(dev
, "Group %s: Invalid pin ID, idx: %d, pin %u\n",
731 group
.name
, j
, pin_id
);
734 group
.pins
[j
] = pin_id
;
735 if (of_property_read_u32_index(np
, "pinmux", j
, &pinmux_id
)) {
736 dev_err(dev
, "Group %s: Read intel pinmux id failed\n",
740 pinmux
[j
] = pinmux_id
;
743 err
= pinctrl_generic_add_group(drvdata
->pctl_dev
, group
.name
,
744 group
.pins
, group
.num_pins
,
747 dev_err(dev
, "Failed to register group %s\n", group
.name
);
750 memset(&group
, 0, sizeof(group
));
757 static int pinctrl_reg(struct eqbr_pinctrl_drv_data
*drvdata
)
759 struct pinctrl_desc
*pctl_desc
;
760 struct pinctrl_pin_desc
*pdesc
;
762 unsigned int nr_pins
;
767 pctl_desc
= &drvdata
->pctl_desc
;
768 pctl_desc
->name
= "eqbr-pinctrl";
769 pctl_desc
->owner
= THIS_MODULE
;
770 pctl_desc
->pctlops
= &eqbr_pctl_ops
;
771 pctl_desc
->pmxops
= &eqbr_pinmux_ops
;
772 pctl_desc
->confops
= &eqbr_pinconf_ops
;
773 raw_spin_lock_init(&drvdata
->lock
);
775 for (i
= 0, nr_pins
= 0; i
< drvdata
->nr_banks
; i
++)
776 nr_pins
+= drvdata
->pin_banks
[i
].nr_pins
;
778 pdesc
= devm_kcalloc(dev
, nr_pins
, sizeof(*pdesc
), GFP_KERNEL
);
781 pin_names
= devm_kcalloc(dev
, nr_pins
, PIN_NAME_LEN
, GFP_KERNEL
);
785 for (i
= 0; i
< nr_pins
; i
++) {
786 sprintf(pin_names
, PIN_NAME_FMT
, i
);
788 pdesc
[i
].name
= pin_names
;
789 pin_names
+= PIN_NAME_LEN
;
791 pctl_desc
->pins
= pdesc
;
792 pctl_desc
->npins
= nr_pins
;
793 dev_dbg(dev
, "pinctrl total pin number: %u\n", nr_pins
);
795 ret
= devm_pinctrl_register_and_init(dev
, pctl_desc
, drvdata
,
800 ret
= eqbr_build_groups(drvdata
);
802 dev_err(dev
, "Failed to build groups\n");
806 ret
= eqbr_build_functions(drvdata
);
808 dev_err(dev
, "Failed to build groups\n");
812 return pinctrl_enable(drvdata
->pctl_dev
);
815 static int pinbank_init(struct device_node
*np
,
816 struct eqbr_pinctrl_drv_data
*drvdata
,
817 struct eqbr_pin_bank
*bank
, unsigned int id
)
819 struct device
*dev
= drvdata
->dev
;
820 struct of_phandle_args spec
;
823 bank
->membase
= drvdata
->membase
+ id
* PAD_REG_OFF
;
825 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3, 0, &spec
);
827 dev_err(dev
, "gpio-range not available!\n");
831 bank
->pin_base
= spec
.args
[1];
832 bank
->nr_pins
= spec
.args
[2];
834 bank
->aval_pinmap
= readl(bank
->membase
+ REG_AVAIL
);
837 dev_dbg(dev
, "pinbank id: %d, reg: %px, pinbase: %u, pin number: %u, pinmap: 0x%x\n",
838 id
, bank
->membase
, bank
->pin_base
,
839 bank
->nr_pins
, bank
->aval_pinmap
);
844 static int pinbank_probe(struct eqbr_pinctrl_drv_data
*drvdata
)
846 struct device
*dev
= drvdata
->dev
;
847 struct device_node
*np_gpio
;
848 struct eqbr_gpio_ctrl
*gctrls
;
849 struct eqbr_pin_bank
*banks
;
852 /* Count gpio bank number */
854 for_each_node_by_name(np_gpio
, "gpio") {
855 if (of_device_is_available(np_gpio
))
860 dev_err(dev
, "NO pin bank available!\n");
864 /* Count pin bank number and gpio controller number */
865 banks
= devm_kcalloc(dev
, nr_gpio
, sizeof(*banks
), GFP_KERNEL
);
869 gctrls
= devm_kcalloc(dev
, nr_gpio
, sizeof(*gctrls
), GFP_KERNEL
);
873 dev_dbg(dev
, "found %d gpio controller!\n", nr_gpio
);
875 /* Initialize Pin bank */
877 for_each_node_by_name(np_gpio
, "gpio") {
878 if (!of_device_is_available(np_gpio
))
881 pinbank_init(np_gpio
, drvdata
, banks
+ i
, i
);
883 gctrls
[i
].node
= np_gpio
;
884 gctrls
[i
].bank
= banks
+ i
;
888 drvdata
->pin_banks
= banks
;
889 drvdata
->nr_banks
= nr_gpio
;
890 drvdata
->gpio_ctrls
= gctrls
;
891 drvdata
->nr_gpio_ctrls
= nr_gpio
;
896 static int eqbr_pinctrl_probe(struct platform_device
*pdev
)
898 struct eqbr_pinctrl_drv_data
*drvdata
;
899 struct device
*dev
= &pdev
->dev
;
902 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
908 drvdata
->membase
= devm_platform_ioremap_resource(pdev
, 0);
909 if (IS_ERR(drvdata
->membase
))
910 return PTR_ERR(drvdata
->membase
);
912 ret
= pinbank_probe(drvdata
);
916 ret
= pinctrl_reg(drvdata
);
920 ret
= gpiolib_reg(drvdata
);
924 platform_set_drvdata(pdev
, drvdata
);
928 static const struct of_device_id eqbr_pinctrl_dt_match
[] = {
929 { .compatible
= "intel,lgm-io" },
933 static struct platform_driver eqbr_pinctrl_driver
= {
934 .probe
= eqbr_pinctrl_probe
,
936 .name
= "eqbr-pinctrl",
937 .of_match_table
= eqbr_pinctrl_dt_match
,
941 module_platform_driver(eqbr_pinctrl_driver
);
943 MODULE_AUTHOR("Zhu Yixin <yixin.zhu@intel.com>, Rahul Tanwar <rahul.tanwar@intel.com>");
944 MODULE_DESCRIPTION("Pinctrl Driver for LGM SoC (Equilibrium)");
945 MODULE_LICENSE("GPL v2");