1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pinmux driver for CSR SiRFprimaII
6 * Rongjun Ying <rongjun.ying@csr.com>
7 * Yuping Luo <yuping.luo@csr.com>
8 * Barry Song <baohua.song@csr.com>
10 * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
14 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pinctrl/machine.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/of_platform.h>
28 #include <linux/bitops.h>
29 #include <linux/gpio/driver.h>
30 #include <linux/of_gpio.h>
32 #include "pinctrl-sirf.h"
34 #define DRIVER_NAME "pinmux-sirf"
36 struct sirfsoc_gpio_bank
{
42 struct sirfsoc_gpio_chip
{
43 struct of_mm_gpio_chip chip
;
44 struct sirfsoc_gpio_bank sgpio_bank
[SIRFSOC_GPIO_NO_OF_BANKS
];
48 static struct sirfsoc_pin_group
*sirfsoc_pin_groups
;
49 static int sirfsoc_pingrp_cnt
;
51 static int sirfsoc_get_groups_count(struct pinctrl_dev
*pctldev
)
53 return sirfsoc_pingrp_cnt
;
56 static const char *sirfsoc_get_group_name(struct pinctrl_dev
*pctldev
,
59 return sirfsoc_pin_groups
[selector
].name
;
62 static int sirfsoc_get_group_pins(struct pinctrl_dev
*pctldev
,
64 const unsigned **pins
,
67 *pins
= sirfsoc_pin_groups
[selector
].pins
;
68 *num_pins
= sirfsoc_pin_groups
[selector
].num_pins
;
72 static void sirfsoc_pin_dbg_show(struct pinctrl_dev
*pctldev
,
73 struct seq_file
*s
, unsigned offset
)
75 seq_printf(s
, " " DRIVER_NAME
);
78 static int sirfsoc_dt_node_to_map(struct pinctrl_dev
*pctldev
,
79 struct device_node
*np_config
,
80 struct pinctrl_map
**map
, unsigned *num_maps
)
82 struct sirfsoc_pmx
*spmx
= pinctrl_dev_get_drvdata(pctldev
);
83 struct device_node
*np
;
84 struct property
*prop
;
85 const char *function
, *group
;
86 int ret
, index
= 0, count
= 0;
88 /* calculate number of maps required */
89 for_each_child_of_node(np_config
, np
) {
90 ret
= of_property_read_string(np
, "sirf,function", &function
);
96 ret
= of_property_count_strings(np
, "sirf,pins");
106 dev_err(spmx
->dev
, "No child nodes passed via DT\n");
110 *map
= kcalloc(count
, sizeof(**map
), GFP_KERNEL
);
114 for_each_child_of_node(np_config
, np
) {
115 of_property_read_string(np
, "sirf,function", &function
);
116 of_property_for_each_string(np
, "sirf,pins", prop
, group
) {
117 (*map
)[index
].type
= PIN_MAP_TYPE_MUX_GROUP
;
118 (*map
)[index
].data
.mux
.group
= group
;
119 (*map
)[index
].data
.mux
.function
= function
;
129 static void sirfsoc_dt_free_map(struct pinctrl_dev
*pctldev
,
130 struct pinctrl_map
*map
, unsigned num_maps
)
135 static const struct pinctrl_ops sirfsoc_pctrl_ops
= {
136 .get_groups_count
= sirfsoc_get_groups_count
,
137 .get_group_name
= sirfsoc_get_group_name
,
138 .get_group_pins
= sirfsoc_get_group_pins
,
139 .pin_dbg_show
= sirfsoc_pin_dbg_show
,
140 .dt_node_to_map
= sirfsoc_dt_node_to_map
,
141 .dt_free_map
= sirfsoc_dt_free_map
,
144 static struct sirfsoc_pmx_func
*sirfsoc_pmx_functions
;
145 static int sirfsoc_pmxfunc_cnt
;
147 static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx
*spmx
,
148 unsigned selector
, bool enable
)
151 const struct sirfsoc_padmux
*mux
=
152 sirfsoc_pmx_functions
[selector
].padmux
;
153 const struct sirfsoc_muxmask
*mask
= mux
->muxmask
;
155 for (i
= 0; i
< mux
->muxmask_counts
; i
++) {
157 muxval
= readl(spmx
->gpio_virtbase
+
158 SIRFSOC_GPIO_PAD_EN(mask
[i
].group
));
160 muxval
= muxval
& ~mask
[i
].mask
;
162 muxval
= muxval
| mask
[i
].mask
;
163 writel(muxval
, spmx
->gpio_virtbase
+
164 SIRFSOC_GPIO_PAD_EN(mask
[i
].group
));
167 if (mux
->funcmask
&& enable
) {
171 readl(spmx
->rsc_virtbase
+ mux
->ctrlreg
);
173 (func_en_val
& ~mux
->funcmask
) | (mux
->funcval
);
174 writel(func_en_val
, spmx
->rsc_virtbase
+ mux
->ctrlreg
);
178 static int sirfsoc_pinmux_set_mux(struct pinctrl_dev
*pmxdev
,
182 struct sirfsoc_pmx
*spmx
;
184 spmx
= pinctrl_dev_get_drvdata(pmxdev
);
185 sirfsoc_pinmux_endisable(spmx
, selector
, true);
190 static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev
*pmxdev
)
192 return sirfsoc_pmxfunc_cnt
;
195 static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev
*pctldev
,
198 return sirfsoc_pmx_functions
[selector
].name
;
201 static int sirfsoc_pinmux_get_groups(struct pinctrl_dev
*pctldev
,
203 const char * const **groups
,
204 unsigned * const num_groups
)
206 *groups
= sirfsoc_pmx_functions
[selector
].groups
;
207 *num_groups
= sirfsoc_pmx_functions
[selector
].num_groups
;
211 static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev
*pmxdev
,
212 struct pinctrl_gpio_range
*range
, unsigned offset
)
214 struct sirfsoc_pmx
*spmx
;
216 int group
= range
->id
;
220 spmx
= pinctrl_dev_get_drvdata(pmxdev
);
222 muxval
= readl(spmx
->gpio_virtbase
+
223 SIRFSOC_GPIO_PAD_EN(group
));
224 muxval
= muxval
| (1 << (offset
- range
->pin_base
));
225 writel(muxval
, spmx
->gpio_virtbase
+
226 SIRFSOC_GPIO_PAD_EN(group
));
231 static const struct pinmux_ops sirfsoc_pinmux_ops
= {
232 .set_mux
= sirfsoc_pinmux_set_mux
,
233 .get_functions_count
= sirfsoc_pinmux_get_funcs_count
,
234 .get_function_name
= sirfsoc_pinmux_get_func_name
,
235 .get_function_groups
= sirfsoc_pinmux_get_groups
,
236 .gpio_request_enable
= sirfsoc_pinmux_request_gpio
,
239 static struct pinctrl_desc sirfsoc_pinmux_desc
= {
241 .pctlops
= &sirfsoc_pctrl_ops
,
242 .pmxops
= &sirfsoc_pinmux_ops
,
243 .owner
= THIS_MODULE
,
246 static void __iomem
*sirfsoc_rsc_of_iomap(void)
248 const struct of_device_id rsc_ids
[] = {
249 { .compatible
= "sirf,prima2-rsc" },
252 struct device_node
*np
;
254 np
= of_find_matching_node(NULL
, rsc_ids
);
256 panic("unable to find compatible rsc node in dtb\n");
258 return of_iomap(np
, 0);
261 static int sirfsoc_gpio_of_xlate(struct gpio_chip
*gc
,
262 const struct of_phandle_args
*gpiospec
,
265 if (gpiospec
->args
[0] > SIRFSOC_GPIO_NO_OF_BANKS
* SIRFSOC_GPIO_BANK_SIZE
)
269 *flags
= gpiospec
->args
[1];
271 return gpiospec
->args
[0];
274 static const struct of_device_id pinmux_ids
[] = {
275 { .compatible
= "sirf,prima2-pinctrl", .data
= &prima2_pinctrl_data
, },
276 { .compatible
= "sirf,atlas6-pinctrl", .data
= &atlas6_pinctrl_data
, },
280 static int sirfsoc_pinmux_probe(struct platform_device
*pdev
)
283 struct sirfsoc_pmx
*spmx
;
284 struct device_node
*np
= pdev
->dev
.of_node
;
285 const struct sirfsoc_pinctrl_data
*pdata
;
287 /* Create state holders etc for this driver */
288 spmx
= devm_kzalloc(&pdev
->dev
, sizeof(*spmx
), GFP_KERNEL
);
292 spmx
->dev
= &pdev
->dev
;
294 platform_set_drvdata(pdev
, spmx
);
296 spmx
->gpio_virtbase
= of_iomap(np
, 0);
297 if (!spmx
->gpio_virtbase
) {
298 dev_err(&pdev
->dev
, "can't map gpio registers\n");
302 spmx
->rsc_virtbase
= sirfsoc_rsc_of_iomap();
303 if (!spmx
->rsc_virtbase
) {
305 dev_err(&pdev
->dev
, "can't map rsc registers\n");
306 goto out_no_rsc_remap
;
309 pdata
= of_match_node(pinmux_ids
, np
)->data
;
310 sirfsoc_pin_groups
= pdata
->grps
;
311 sirfsoc_pingrp_cnt
= pdata
->grps_cnt
;
312 sirfsoc_pmx_functions
= pdata
->funcs
;
313 sirfsoc_pmxfunc_cnt
= pdata
->funcs_cnt
;
314 sirfsoc_pinmux_desc
.pins
= pdata
->pads
;
315 sirfsoc_pinmux_desc
.npins
= pdata
->pads_cnt
;
318 /* Now register the pin controller and all pins it handles */
319 spmx
->pmx
= pinctrl_register(&sirfsoc_pinmux_desc
, &pdev
->dev
, spmx
);
320 if (IS_ERR(spmx
->pmx
)) {
321 dev_err(&pdev
->dev
, "could not register SIRFSOC pinmux driver\n");
322 ret
= PTR_ERR(spmx
->pmx
);
326 dev_info(&pdev
->dev
, "initialized SIRFSOC pinmux driver\n");
331 iounmap(spmx
->rsc_virtbase
);
333 iounmap(spmx
->gpio_virtbase
);
337 #ifdef CONFIG_PM_SLEEP
338 static int sirfsoc_pinmux_suspend_noirq(struct device
*dev
)
341 struct sirfsoc_pmx
*spmx
= dev_get_drvdata(dev
);
343 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
344 for (j
= 0; j
< SIRFSOC_GPIO_BANK_SIZE
; j
++) {
345 spmx
->gpio_regs
[i
][j
] = readl(spmx
->gpio_virtbase
+
346 SIRFSOC_GPIO_CTRL(i
, j
));
348 spmx
->ints_regs
[i
] = readl(spmx
->gpio_virtbase
+
349 SIRFSOC_GPIO_INT_STATUS(i
));
350 spmx
->paden_regs
[i
] = readl(spmx
->gpio_virtbase
+
351 SIRFSOC_GPIO_PAD_EN(i
));
353 spmx
->dspen_regs
= readl(spmx
->gpio_virtbase
+ SIRFSOC_GPIO_DSP_EN0
);
355 for (i
= 0; i
< 3; i
++)
356 spmx
->rsc_regs
[i
] = readl(spmx
->rsc_virtbase
+ 4 * i
);
361 static int sirfsoc_pinmux_resume_noirq(struct device
*dev
)
364 struct sirfsoc_pmx
*spmx
= dev_get_drvdata(dev
);
366 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
367 for (j
= 0; j
< SIRFSOC_GPIO_BANK_SIZE
; j
++) {
368 writel(spmx
->gpio_regs
[i
][j
], spmx
->gpio_virtbase
+
369 SIRFSOC_GPIO_CTRL(i
, j
));
371 writel(spmx
->ints_regs
[i
], spmx
->gpio_virtbase
+
372 SIRFSOC_GPIO_INT_STATUS(i
));
373 writel(spmx
->paden_regs
[i
], spmx
->gpio_virtbase
+
374 SIRFSOC_GPIO_PAD_EN(i
));
376 writel(spmx
->dspen_regs
, spmx
->gpio_virtbase
+ SIRFSOC_GPIO_DSP_EN0
);
378 for (i
= 0; i
< 3; i
++)
379 writel(spmx
->rsc_regs
[i
], spmx
->rsc_virtbase
+ 4 * i
);
384 static const struct dev_pm_ops sirfsoc_pinmux_pm_ops
= {
385 .suspend_noirq
= sirfsoc_pinmux_suspend_noirq
,
386 .resume_noirq
= sirfsoc_pinmux_resume_noirq
,
387 .freeze_noirq
= sirfsoc_pinmux_suspend_noirq
,
388 .restore_noirq
= sirfsoc_pinmux_resume_noirq
,
392 static struct platform_driver sirfsoc_pinmux_driver
= {
395 .of_match_table
= pinmux_ids
,
396 #ifdef CONFIG_PM_SLEEP
397 .pm
= &sirfsoc_pinmux_pm_ops
,
400 .probe
= sirfsoc_pinmux_probe
,
403 static int __init
sirfsoc_pinmux_init(void)
405 return platform_driver_register(&sirfsoc_pinmux_driver
);
407 arch_initcall(sirfsoc_pinmux_init
);
409 static inline struct sirfsoc_gpio_bank
*
410 sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip
*sgpio
, unsigned int offset
)
412 return &sgpio
->sgpio_bank
[offset
/ SIRFSOC_GPIO_BANK_SIZE
];
415 static inline int sirfsoc_gpio_to_bankoff(unsigned int offset
)
417 return offset
% SIRFSOC_GPIO_BANK_SIZE
;
420 static void sirfsoc_gpio_irq_ack(struct irq_data
*d
)
422 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
423 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(gc
);
424 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, d
->hwirq
);
425 int idx
= sirfsoc_gpio_to_bankoff(d
->hwirq
);
429 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
431 spin_lock_irqsave(&sgpio
->lock
, flags
);
433 val
= readl(sgpio
->chip
.regs
+ offset
);
435 writel(val
, sgpio
->chip
.regs
+ offset
);
437 spin_unlock_irqrestore(&sgpio
->lock
, flags
);
440 static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip
*sgpio
,
441 struct sirfsoc_gpio_bank
*bank
,
447 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
449 spin_lock_irqsave(&sgpio
->lock
, flags
);
451 val
= readl(sgpio
->chip
.regs
+ offset
);
452 val
&= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK
;
453 val
&= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK
;
454 writel(val
, sgpio
->chip
.regs
+ offset
);
456 spin_unlock_irqrestore(&sgpio
->lock
, flags
);
459 static void sirfsoc_gpio_irq_mask(struct irq_data
*d
)
461 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
462 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(gc
);
463 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, d
->hwirq
);
465 __sirfsoc_gpio_irq_mask(sgpio
, bank
, d
->hwirq
% SIRFSOC_GPIO_BANK_SIZE
);
468 static void sirfsoc_gpio_irq_unmask(struct irq_data
*d
)
470 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
471 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(gc
);
472 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, d
->hwirq
);
473 int idx
= sirfsoc_gpio_to_bankoff(d
->hwirq
);
477 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
479 spin_lock_irqsave(&sgpio
->lock
, flags
);
481 val
= readl(sgpio
->chip
.regs
+ offset
);
482 val
&= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK
;
483 val
|= SIRFSOC_GPIO_CTL_INTR_EN_MASK
;
484 writel(val
, sgpio
->chip
.regs
+ offset
);
486 spin_unlock_irqrestore(&sgpio
->lock
, flags
);
489 static int sirfsoc_gpio_irq_type(struct irq_data
*d
, unsigned type
)
491 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
492 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(gc
);
493 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, d
->hwirq
);
494 int idx
= sirfsoc_gpio_to_bankoff(d
->hwirq
);
498 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
500 spin_lock_irqsave(&sgpio
->lock
, flags
);
502 val
= readl(sgpio
->chip
.regs
+ offset
);
503 val
&= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK
| SIRFSOC_GPIO_CTL_OUT_EN_MASK
);
508 case IRQ_TYPE_EDGE_RISING
:
509 val
|= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK
|
510 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK
;
511 val
&= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK
;
513 case IRQ_TYPE_EDGE_FALLING
:
514 val
&= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK
;
515 val
|= SIRFSOC_GPIO_CTL_INTR_LOW_MASK
|
516 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK
;
518 case IRQ_TYPE_EDGE_BOTH
:
519 val
|= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK
|
520 SIRFSOC_GPIO_CTL_INTR_LOW_MASK
|
521 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK
;
523 case IRQ_TYPE_LEVEL_LOW
:
524 val
&= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK
|
525 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK
);
526 val
|= SIRFSOC_GPIO_CTL_INTR_LOW_MASK
;
528 case IRQ_TYPE_LEVEL_HIGH
:
529 val
|= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK
;
530 val
&= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK
|
531 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK
);
535 writel(val
, sgpio
->chip
.regs
+ offset
);
537 spin_unlock_irqrestore(&sgpio
->lock
, flags
);
542 static struct irq_chip sirfsoc_irq_chip
= {
543 .name
= "sirf-gpio-irq",
544 .irq_ack
= sirfsoc_gpio_irq_ack
,
545 .irq_mask
= sirfsoc_gpio_irq_mask
,
546 .irq_unmask
= sirfsoc_gpio_irq_unmask
,
547 .irq_set_type
= sirfsoc_gpio_irq_type
,
550 static void sirfsoc_gpio_handle_irq(struct irq_desc
*desc
)
552 unsigned int irq
= irq_desc_get_irq(desc
);
553 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
554 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(gc
);
555 struct sirfsoc_gpio_bank
*bank
;
558 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
561 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
562 bank
= &sgpio
->sgpio_bank
[i
];
563 if (bank
->parent_irq
== irq
)
566 BUG_ON(i
== SIRFSOC_GPIO_NO_OF_BANKS
);
568 chained_irq_enter(chip
, desc
);
570 status
= readl(sgpio
->chip
.regs
+ SIRFSOC_GPIO_INT_STATUS(bank
->id
));
573 "%s: gpio id %d status %#x no interrupt is flagged\n",
574 __func__
, bank
->id
, status
);
575 handle_bad_irq(desc
);
580 ctrl
= readl(sgpio
->chip
.regs
+ SIRFSOC_GPIO_CTRL(bank
->id
, idx
));
583 * Here we must check whether the corresponding GPIO's interrupt
584 * has been enabled, otherwise just skip it
586 if ((status
& 0x1) && (ctrl
& SIRFSOC_GPIO_CTL_INTR_EN_MASK
)) {
587 pr_debug("%s: gpio id %d idx %d happens\n",
588 __func__
, bank
->id
, idx
);
589 generic_handle_irq(irq_find_mapping(gc
->irq
.domain
, idx
+
590 bank
->id
* SIRFSOC_GPIO_BANK_SIZE
));
594 status
= status
>> 1;
597 chained_irq_exit(chip
, desc
);
600 static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip
*sgpio
,
601 unsigned ctrl_offset
)
605 val
= readl(sgpio
->chip
.regs
+ ctrl_offset
);
606 val
&= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK
;
607 writel(val
, sgpio
->chip
.regs
+ ctrl_offset
);
610 static int sirfsoc_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
612 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
613 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, offset
);
616 if (pinctrl_gpio_request(chip
->base
+ offset
))
619 spin_lock_irqsave(&bank
->lock
, flags
);
623 * set direction as input and mask irq
625 sirfsoc_gpio_set_input(sgpio
, SIRFSOC_GPIO_CTRL(bank
->id
, offset
));
626 __sirfsoc_gpio_irq_mask(sgpio
, bank
, offset
);
628 spin_unlock_irqrestore(&bank
->lock
, flags
);
633 static void sirfsoc_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
635 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
636 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, offset
);
639 spin_lock_irqsave(&bank
->lock
, flags
);
641 __sirfsoc_gpio_irq_mask(sgpio
, bank
, offset
);
642 sirfsoc_gpio_set_input(sgpio
, SIRFSOC_GPIO_CTRL(bank
->id
, offset
));
644 spin_unlock_irqrestore(&bank
->lock
, flags
);
646 pinctrl_gpio_free(chip
->base
+ offset
);
649 static int sirfsoc_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
651 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
652 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, gpio
);
653 int idx
= sirfsoc_gpio_to_bankoff(gpio
);
657 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
659 spin_lock_irqsave(&bank
->lock
, flags
);
661 sirfsoc_gpio_set_input(sgpio
, offset
);
663 spin_unlock_irqrestore(&bank
->lock
, flags
);
668 static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip
*sgpio
,
669 struct sirfsoc_gpio_bank
*bank
,
676 spin_lock_irqsave(&bank
->lock
, flags
);
678 out_ctrl
= readl(sgpio
->chip
.regs
+ offset
);
680 out_ctrl
|= SIRFSOC_GPIO_CTL_DATAOUT_MASK
;
682 out_ctrl
&= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK
;
684 out_ctrl
&= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK
;
685 out_ctrl
|= SIRFSOC_GPIO_CTL_OUT_EN_MASK
;
686 writel(out_ctrl
, sgpio
->chip
.regs
+ offset
);
688 spin_unlock_irqrestore(&bank
->lock
, flags
);
691 static int sirfsoc_gpio_direction_output(struct gpio_chip
*chip
,
692 unsigned gpio
, int value
)
694 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
695 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, gpio
);
696 int idx
= sirfsoc_gpio_to_bankoff(gpio
);
700 offset
= SIRFSOC_GPIO_CTRL(bank
->id
, idx
);
702 spin_lock_irqsave(&sgpio
->lock
, flags
);
704 sirfsoc_gpio_set_output(sgpio
, bank
, offset
, value
);
706 spin_unlock_irqrestore(&sgpio
->lock
, flags
);
711 static int sirfsoc_gpio_get_value(struct gpio_chip
*chip
, unsigned offset
)
713 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
714 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, offset
);
718 spin_lock_irqsave(&bank
->lock
, flags
);
720 val
= readl(sgpio
->chip
.regs
+ SIRFSOC_GPIO_CTRL(bank
->id
, offset
));
722 spin_unlock_irqrestore(&bank
->lock
, flags
);
724 return !!(val
& SIRFSOC_GPIO_CTL_DATAIN_MASK
);
727 static void sirfsoc_gpio_set_value(struct gpio_chip
*chip
, unsigned offset
,
730 struct sirfsoc_gpio_chip
*sgpio
= gpiochip_get_data(chip
);
731 struct sirfsoc_gpio_bank
*bank
= sirfsoc_gpio_to_bank(sgpio
, offset
);
735 spin_lock_irqsave(&bank
->lock
, flags
);
737 ctrl
= readl(sgpio
->chip
.regs
+ SIRFSOC_GPIO_CTRL(bank
->id
, offset
));
739 ctrl
|= SIRFSOC_GPIO_CTL_DATAOUT_MASK
;
741 ctrl
&= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK
;
742 writel(ctrl
, sgpio
->chip
.regs
+ SIRFSOC_GPIO_CTRL(bank
->id
, offset
));
744 spin_unlock_irqrestore(&bank
->lock
, flags
);
747 static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip
*sgpio
,
751 const unsigned long *p
= (const unsigned long *)pullups
;
753 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
754 for_each_set_bit(n
, p
+ i
, BITS_PER_LONG
) {
755 u32 offset
= SIRFSOC_GPIO_CTRL(i
, n
);
756 u32 val
= readl(sgpio
->chip
.regs
+ offset
);
757 val
|= SIRFSOC_GPIO_CTL_PULL_MASK
;
758 val
|= SIRFSOC_GPIO_CTL_PULL_HIGH
;
759 writel(val
, sgpio
->chip
.regs
+ offset
);
764 static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip
*sgpio
,
765 const u32
*pulldowns
)
768 const unsigned long *p
= (const unsigned long *)pulldowns
;
770 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
771 for_each_set_bit(n
, p
+ i
, BITS_PER_LONG
) {
772 u32 offset
= SIRFSOC_GPIO_CTRL(i
, n
);
773 u32 val
= readl(sgpio
->chip
.regs
+ offset
);
774 val
|= SIRFSOC_GPIO_CTL_PULL_MASK
;
775 val
&= ~SIRFSOC_GPIO_CTL_PULL_HIGH
;
776 writel(val
, sgpio
->chip
.regs
+ offset
);
781 static int sirfsoc_gpio_probe(struct device_node
*np
)
784 struct sirfsoc_gpio_chip
*sgpio
;
785 struct sirfsoc_gpio_bank
*bank
;
787 struct platform_device
*pdev
;
788 struct gpio_irq_chip
*girq
;
790 u32 pullups
[SIRFSOC_GPIO_NO_OF_BANKS
], pulldowns
[SIRFSOC_GPIO_NO_OF_BANKS
];
792 pdev
= of_find_device_by_node(np
);
796 sgpio
= devm_kzalloc(&pdev
->dev
, sizeof(*sgpio
), GFP_KERNEL
);
801 spin_lock_init(&sgpio
->lock
);
803 regs
= of_iomap(np
, 0);
809 sgpio
->chip
.gc
.request
= sirfsoc_gpio_request
;
810 sgpio
->chip
.gc
.free
= sirfsoc_gpio_free
;
811 sgpio
->chip
.gc
.direction_input
= sirfsoc_gpio_direction_input
;
812 sgpio
->chip
.gc
.get
= sirfsoc_gpio_get_value
;
813 sgpio
->chip
.gc
.direction_output
= sirfsoc_gpio_direction_output
;
814 sgpio
->chip
.gc
.set
= sirfsoc_gpio_set_value
;
815 sgpio
->chip
.gc
.base
= 0;
816 sgpio
->chip
.gc
.ngpio
= SIRFSOC_GPIO_BANK_SIZE
* SIRFSOC_GPIO_NO_OF_BANKS
;
817 sgpio
->chip
.gc
.label
= kasprintf(GFP_KERNEL
, "%pOF", np
);
818 sgpio
->chip
.gc
.of_node
= np
;
819 sgpio
->chip
.gc
.of_xlate
= sirfsoc_gpio_of_xlate
;
820 sgpio
->chip
.gc
.of_gpio_n_cells
= 2;
821 sgpio
->chip
.gc
.parent
= &pdev
->dev
;
822 sgpio
->chip
.regs
= regs
;
824 girq
= &sgpio
->chip
.gc
.irq
;
825 girq
->chip
= &sirfsoc_irq_chip
;
826 girq
->parent_handler
= sirfsoc_gpio_handle_irq
;
827 girq
->num_parents
= SIRFSOC_GPIO_NO_OF_BANKS
;
828 girq
->parents
= devm_kcalloc(&pdev
->dev
, SIRFSOC_GPIO_NO_OF_BANKS
,
829 sizeof(*girq
->parents
),
831 if (!girq
->parents
) {
835 for (i
= 0; i
< SIRFSOC_GPIO_NO_OF_BANKS
; i
++) {
836 bank
= &sgpio
->sgpio_bank
[i
];
837 spin_lock_init(&bank
->lock
);
838 bank
->parent_irq
= platform_get_irq(pdev
, i
);
839 if (bank
->parent_irq
< 0) {
840 err
= bank
->parent_irq
;
843 girq
->parents
[i
] = bank
->parent_irq
;
845 girq
->default_type
= IRQ_TYPE_NONE
;
846 girq
->handler
= handle_level_irq
;
848 err
= gpiochip_add_data(&sgpio
->chip
.gc
, sgpio
);
850 dev_err(&pdev
->dev
, "%pOF: error in probe function with status %d\n",
855 err
= gpiochip_add_pin_range(&sgpio
->chip
.gc
, dev_name(&pdev
->dev
),
856 0, 0, SIRFSOC_GPIO_BANK_SIZE
* SIRFSOC_GPIO_NO_OF_BANKS
);
859 "could not add gpiochip pin range\n");
863 if (!of_property_read_u32_array(np
, "sirf,pullups", pullups
,
864 SIRFSOC_GPIO_NO_OF_BANKS
))
865 sirfsoc_gpio_set_pullup(sgpio
, pullups
);
867 if (!of_property_read_u32_array(np
, "sirf,pulldowns", pulldowns
,
868 SIRFSOC_GPIO_NO_OF_BANKS
))
869 sirfsoc_gpio_set_pulldown(sgpio
, pulldowns
);
874 gpiochip_remove(&sgpio
->chip
.gc
);
878 put_device(&pdev
->dev
);
882 static int __init
sirfsoc_gpio_init(void)
885 struct device_node
*np
;
887 np
= of_find_matching_node(NULL
, pinmux_ids
);
892 return sirfsoc_gpio_probe(np
);
894 subsys_initcall(sirfsoc_gpio_init
);