PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / pinctrl / sirf / pinctrl-sirf.c
bloba0d6152701cdf3d3aa5a64bbb435f93526d0a324
1 /*
2 * pinmux driver for CSR SiRFprimaII
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
7 */
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/irq.h>
12 #include <linux/platform_device.h>
13 #include <linux/io.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/irqdomain.h>
17 #include <linux/irqchip/chained_irq.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/of_platform.h>
26 #include <linux/bitops.h>
27 #include <linux/gpio.h>
28 #include <linux/of_gpio.h>
29 #include <asm/mach/irq.h>
31 #include "pinctrl-sirf.h"
33 #define DRIVER_NAME "pinmux-sirf"
35 struct sirfsoc_gpio_bank {
36 struct of_mm_gpio_chip chip;
37 struct irq_domain *domain;
38 int id;
39 int parent_irq;
40 spinlock_t lock;
41 bool is_marco; /* for marco, some registers are different with prima2 */
44 static struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS];
45 static DEFINE_SPINLOCK(sgpio_lock);
47 static struct sirfsoc_pin_group *sirfsoc_pin_groups;
48 static int sirfsoc_pingrp_cnt;
50 static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev)
52 return sirfsoc_pingrp_cnt;
55 static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev,
56 unsigned selector)
58 return sirfsoc_pin_groups[selector].name;
61 static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
62 const unsigned **pins,
63 unsigned *num_pins)
65 *pins = sirfsoc_pin_groups[selector].pins;
66 *num_pins = sirfsoc_pin_groups[selector].num_pins;
67 return 0;
70 static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
71 unsigned offset)
73 seq_printf(s, " " DRIVER_NAME);
76 static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
77 struct device_node *np_config,
78 struct pinctrl_map **map, unsigned *num_maps)
80 struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
81 struct device_node *np;
82 struct property *prop;
83 const char *function, *group;
84 int ret, index = 0, count = 0;
86 /* calculate number of maps required */
87 for_each_child_of_node(np_config, np) {
88 ret = of_property_read_string(np, "sirf,function", &function);
89 if (ret < 0)
90 return ret;
92 ret = of_property_count_strings(np, "sirf,pins");
93 if (ret < 0)
94 return ret;
96 count += ret;
99 if (!count) {
100 dev_err(spmx->dev, "No child nodes passed via DT\n");
101 return -ENODEV;
104 *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
105 if (!*map)
106 return -ENOMEM;
108 for_each_child_of_node(np_config, np) {
109 of_property_read_string(np, "sirf,function", &function);
110 of_property_for_each_string(np, "sirf,pins", prop, group) {
111 (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
112 (*map)[index].data.mux.group = group;
113 (*map)[index].data.mux.function = function;
114 index++;
118 *num_maps = count;
120 return 0;
123 static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
124 struct pinctrl_map *map, unsigned num_maps)
126 kfree(map);
129 static struct pinctrl_ops sirfsoc_pctrl_ops = {
130 .get_groups_count = sirfsoc_get_groups_count,
131 .get_group_name = sirfsoc_get_group_name,
132 .get_group_pins = sirfsoc_get_group_pins,
133 .pin_dbg_show = sirfsoc_pin_dbg_show,
134 .dt_node_to_map = sirfsoc_dt_node_to_map,
135 .dt_free_map = sirfsoc_dt_free_map,
138 static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
139 static int sirfsoc_pmxfunc_cnt;
141 static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx, unsigned selector,
142 bool enable)
144 int i;
145 const struct sirfsoc_padmux *mux = sirfsoc_pmx_functions[selector].padmux;
146 const struct sirfsoc_muxmask *mask = mux->muxmask;
148 for (i = 0; i < mux->muxmask_counts; i++) {
149 u32 muxval;
150 if (!spmx->is_marco) {
151 muxval = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(mask[i].group));
152 if (enable)
153 muxval = muxval & ~mask[i].mask;
154 else
155 muxval = muxval | mask[i].mask;
156 writel(muxval, spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(mask[i].group));
157 } else {
158 if (enable)
159 writel(mask[i].mask, spmx->gpio_virtbase +
160 SIRFSOC_GPIO_PAD_EN_CLR(mask[i].group));
161 else
162 writel(mask[i].mask, spmx->gpio_virtbase +
163 SIRFSOC_GPIO_PAD_EN(mask[i].group));
167 if (mux->funcmask && enable) {
168 u32 func_en_val;
170 func_en_val =
171 readl(spmx->rsc_virtbase + mux->ctrlreg);
172 func_en_val =
173 (func_en_val & ~mux->funcmask) | (mux->funcval);
174 writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
178 static int sirfsoc_pinmux_enable(struct pinctrl_dev *pmxdev, unsigned selector,
179 unsigned group)
181 struct sirfsoc_pmx *spmx;
183 spmx = pinctrl_dev_get_drvdata(pmxdev);
184 sirfsoc_pinmux_endisable(spmx, selector, true);
186 return 0;
189 static void sirfsoc_pinmux_disable(struct pinctrl_dev *pmxdev, unsigned selector,
190 unsigned group)
192 struct sirfsoc_pmx *spmx;
194 spmx = pinctrl_dev_get_drvdata(pmxdev);
195 sirfsoc_pinmux_endisable(spmx, selector, false);
198 static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
200 return sirfsoc_pmxfunc_cnt;
203 static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
204 unsigned selector)
206 return sirfsoc_pmx_functions[selector].name;
209 static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
210 const char * const **groups,
211 unsigned * const num_groups)
213 *groups = sirfsoc_pmx_functions[selector].groups;
214 *num_groups = sirfsoc_pmx_functions[selector].num_groups;
215 return 0;
218 static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
219 struct pinctrl_gpio_range *range, unsigned offset)
221 struct sirfsoc_pmx *spmx;
223 int group = range->id;
225 u32 muxval;
227 spmx = pinctrl_dev_get_drvdata(pmxdev);
229 if (!spmx->is_marco) {
230 muxval = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(group));
231 muxval = muxval | (1 << (offset - range->pin_base));
232 writel(muxval, spmx->gpio_virtbase + SIRFSOC_GPIO_PAD_EN(group));
233 } else {
234 writel(1 << (offset - range->pin_base), spmx->gpio_virtbase +
235 SIRFSOC_GPIO_PAD_EN(group));
238 return 0;
241 static struct pinmux_ops sirfsoc_pinmux_ops = {
242 .enable = sirfsoc_pinmux_enable,
243 .disable = sirfsoc_pinmux_disable,
244 .get_functions_count = sirfsoc_pinmux_get_funcs_count,
245 .get_function_name = sirfsoc_pinmux_get_func_name,
246 .get_function_groups = sirfsoc_pinmux_get_groups,
247 .gpio_request_enable = sirfsoc_pinmux_request_gpio,
250 static struct pinctrl_desc sirfsoc_pinmux_desc = {
251 .name = DRIVER_NAME,
252 .pctlops = &sirfsoc_pctrl_ops,
253 .pmxops = &sirfsoc_pinmux_ops,
254 .owner = THIS_MODULE,
258 * Todo: bind irq_chip to every pinctrl_gpio_range
260 static struct pinctrl_gpio_range sirfsoc_gpio_ranges[] = {
262 .name = "sirfsoc-gpio*",
263 .id = 0,
264 .base = 0,
265 .pin_base = 0,
266 .npins = 32,
267 }, {
268 .name = "sirfsoc-gpio*",
269 .id = 1,
270 .base = 32,
271 .pin_base = 32,
272 .npins = 32,
273 }, {
274 .name = "sirfsoc-gpio*",
275 .id = 2,
276 .base = 64,
277 .pin_base = 64,
278 .npins = 32,
279 }, {
280 .name = "sirfsoc-gpio*",
281 .id = 3,
282 .base = 96,
283 .pin_base = 96,
284 .npins = 19,
288 static void __iomem *sirfsoc_rsc_of_iomap(void)
290 const struct of_device_id rsc_ids[] = {
291 { .compatible = "sirf,prima2-rsc" },
292 { .compatible = "sirf,marco-rsc" },
295 struct device_node *np;
297 np = of_find_matching_node(NULL, rsc_ids);
298 if (!np)
299 panic("unable to find compatible rsc node in dtb\n");
301 return of_iomap(np, 0);
304 static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc,
305 const struct of_phandle_args *gpiospec,
306 u32 *flags)
308 if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE)
309 return -EINVAL;
311 if (gc != &sgpio_bank[gpiospec->args[0] / SIRFSOC_GPIO_BANK_SIZE].chip.gc)
312 return -EINVAL;
314 if (flags)
315 *flags = gpiospec->args[1];
317 return gpiospec->args[0] % SIRFSOC_GPIO_BANK_SIZE;
320 static const struct of_device_id pinmux_ids[] = {
321 { .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, },
322 { .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, },
323 { .compatible = "sirf,marco-pinctrl", .data = &prima2_pinctrl_data, },
327 static int sirfsoc_pinmux_probe(struct platform_device *pdev)
329 int ret;
330 struct sirfsoc_pmx *spmx;
331 struct device_node *np = pdev->dev.of_node;
332 const struct sirfsoc_pinctrl_data *pdata;
333 int i;
335 /* Create state holders etc for this driver */
336 spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL);
337 if (!spmx)
338 return -ENOMEM;
340 spmx->dev = &pdev->dev;
342 platform_set_drvdata(pdev, spmx);
344 spmx->gpio_virtbase = of_iomap(np, 0);
345 if (!spmx->gpio_virtbase) {
346 dev_err(&pdev->dev, "can't map gpio registers\n");
347 return -ENOMEM;
350 spmx->rsc_virtbase = sirfsoc_rsc_of_iomap();
351 if (!spmx->rsc_virtbase) {
352 ret = -ENOMEM;
353 dev_err(&pdev->dev, "can't map rsc registers\n");
354 goto out_no_rsc_remap;
357 if (of_device_is_compatible(np, "sirf,marco-pinctrl"))
358 spmx->is_marco = 1;
360 pdata = of_match_node(pinmux_ids, np)->data;
361 sirfsoc_pin_groups = pdata->grps;
362 sirfsoc_pingrp_cnt = pdata->grps_cnt;
363 sirfsoc_pmx_functions = pdata->funcs;
364 sirfsoc_pmxfunc_cnt = pdata->funcs_cnt;
365 sirfsoc_pinmux_desc.pins = pdata->pads;
366 sirfsoc_pinmux_desc.npins = pdata->pads_cnt;
369 /* Now register the pin controller and all pins it handles */
370 spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx);
371 if (!spmx->pmx) {
372 dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n");
373 ret = -EINVAL;
374 goto out_no_pmx;
377 for (i = 0; i < ARRAY_SIZE(sirfsoc_gpio_ranges); i++) {
378 sirfsoc_gpio_ranges[i].gc = &sgpio_bank[i].chip.gc;
379 pinctrl_add_gpio_range(spmx->pmx, &sirfsoc_gpio_ranges[i]);
382 dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n");
384 return 0;
386 out_no_pmx:
387 iounmap(spmx->rsc_virtbase);
388 out_no_rsc_remap:
389 iounmap(spmx->gpio_virtbase);
390 return ret;
393 #ifdef CONFIG_PM_SLEEP
394 static int sirfsoc_pinmux_suspend_noirq(struct device *dev)
396 int i, j;
397 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
399 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
400 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
401 spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase +
402 SIRFSOC_GPIO_CTRL(i, j));
404 spmx->ints_regs[i] = readl(spmx->gpio_virtbase +
405 SIRFSOC_GPIO_INT_STATUS(i));
406 spmx->paden_regs[i] = readl(spmx->gpio_virtbase +
407 SIRFSOC_GPIO_PAD_EN(i));
409 spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
411 for (i = 0; i < 3; i++)
412 spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i);
414 return 0;
417 static int sirfsoc_pinmux_resume_noirq(struct device *dev)
419 int i, j;
420 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
422 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
423 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
424 writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase +
425 SIRFSOC_GPIO_CTRL(i, j));
427 writel(spmx->ints_regs[i], spmx->gpio_virtbase +
428 SIRFSOC_GPIO_INT_STATUS(i));
429 writel(spmx->paden_regs[i], spmx->gpio_virtbase +
430 SIRFSOC_GPIO_PAD_EN(i));
432 writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
434 for (i = 0; i < 3; i++)
435 writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i);
437 return 0;
440 static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = {
441 .suspend_noirq = sirfsoc_pinmux_suspend_noirq,
442 .resume_noirq = sirfsoc_pinmux_resume_noirq,
443 .freeze_noirq = sirfsoc_pinmux_suspend_noirq,
444 .restore_noirq = sirfsoc_pinmux_resume_noirq,
446 #endif
448 static struct platform_driver sirfsoc_pinmux_driver = {
449 .driver = {
450 .name = DRIVER_NAME,
451 .owner = THIS_MODULE,
452 .of_match_table = pinmux_ids,
453 #ifdef CONFIG_PM_SLEEP
454 .pm = &sirfsoc_pinmux_pm_ops,
455 #endif
457 .probe = sirfsoc_pinmux_probe,
460 static int __init sirfsoc_pinmux_init(void)
462 return platform_driver_register(&sirfsoc_pinmux_driver);
464 arch_initcall(sirfsoc_pinmux_init);
466 static inline int sirfsoc_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
468 struct sirfsoc_gpio_bank *bank = container_of(to_of_mm_gpio_chip(chip),
469 struct sirfsoc_gpio_bank, chip);
471 return irq_create_mapping(bank->domain, offset + bank->id *
472 SIRFSOC_GPIO_BANK_SIZE);
475 static inline int sirfsoc_gpio_to_offset(unsigned int gpio)
477 return gpio % SIRFSOC_GPIO_BANK_SIZE;
480 static inline struct sirfsoc_gpio_bank *sirfsoc_gpio_to_bank(unsigned int gpio)
482 return &sgpio_bank[gpio / SIRFSOC_GPIO_BANK_SIZE];
485 static inline struct sirfsoc_gpio_bank *sirfsoc_irqchip_to_bank(struct gpio_chip *chip)
487 return container_of(to_of_mm_gpio_chip(chip), struct sirfsoc_gpio_bank, chip);
490 static void sirfsoc_gpio_irq_ack(struct irq_data *d)
492 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
493 int idx = d->hwirq % SIRFSOC_GPIO_BANK_SIZE;
494 u32 val, offset;
495 unsigned long flags;
497 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
499 spin_lock_irqsave(&sgpio_lock, flags);
501 val = readl(bank->chip.regs + offset);
503 writel(val, bank->chip.regs + offset);
505 spin_unlock_irqrestore(&sgpio_lock, flags);
508 static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_bank *bank, int idx)
510 u32 val, offset;
511 unsigned long flags;
513 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
515 spin_lock_irqsave(&sgpio_lock, flags);
517 val = readl(bank->chip.regs + offset);
518 val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
519 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
520 writel(val, bank->chip.regs + offset);
522 spin_unlock_irqrestore(&sgpio_lock, flags);
525 static void sirfsoc_gpio_irq_mask(struct irq_data *d)
527 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
529 __sirfsoc_gpio_irq_mask(bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
532 static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
534 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
535 int idx = d->hwirq % SIRFSOC_GPIO_BANK_SIZE;
536 u32 val, offset;
537 unsigned long flags;
539 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
541 spin_lock_irqsave(&sgpio_lock, flags);
543 val = readl(bank->chip.regs + offset);
544 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
545 val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
546 writel(val, bank->chip.regs + offset);
548 spin_unlock_irqrestore(&sgpio_lock, flags);
551 static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
553 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
554 int idx = d->hwirq % SIRFSOC_GPIO_BANK_SIZE;
555 u32 val, offset;
556 unsigned long flags;
558 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
560 spin_lock_irqsave(&sgpio_lock, flags);
562 val = readl(bank->chip.regs + offset);
563 val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
565 switch (type) {
566 case IRQ_TYPE_NONE:
567 break;
568 case IRQ_TYPE_EDGE_RISING:
569 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
570 val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
571 break;
572 case IRQ_TYPE_EDGE_FALLING:
573 val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
574 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
575 break;
576 case IRQ_TYPE_EDGE_BOTH:
577 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
578 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
579 break;
580 case IRQ_TYPE_LEVEL_LOW:
581 val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
582 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
583 break;
584 case IRQ_TYPE_LEVEL_HIGH:
585 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
586 val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
587 break;
590 writel(val, bank->chip.regs + offset);
592 spin_unlock_irqrestore(&sgpio_lock, flags);
594 return 0;
597 static unsigned int sirfsoc_gpio_irq_startup(struct irq_data *d)
599 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
601 if (gpio_lock_as_irq(&bank->chip.gc, d->hwirq))
602 dev_err(bank->chip.gc.dev,
603 "unable to lock HW IRQ %lu for IRQ\n",
604 d->hwirq);
605 sirfsoc_gpio_irq_unmask(d);
606 return 0;
609 static void sirfsoc_gpio_irq_shutdown(struct irq_data *d)
611 struct sirfsoc_gpio_bank *bank = irq_data_get_irq_chip_data(d);
613 sirfsoc_gpio_irq_mask(d);
614 gpio_unlock_as_irq(&bank->chip.gc, d->hwirq);
617 static struct irq_chip sirfsoc_irq_chip = {
618 .name = "sirf-gpio-irq",
619 .irq_ack = sirfsoc_gpio_irq_ack,
620 .irq_mask = sirfsoc_gpio_irq_mask,
621 .irq_unmask = sirfsoc_gpio_irq_unmask,
622 .irq_set_type = sirfsoc_gpio_irq_type,
623 .irq_startup = sirfsoc_gpio_irq_startup,
624 .irq_shutdown = sirfsoc_gpio_irq_shutdown,
627 static void sirfsoc_gpio_handle_irq(unsigned int irq, struct irq_desc *desc)
629 struct sirfsoc_gpio_bank *bank = irq_get_handler_data(irq);
630 u32 status, ctrl;
631 int idx = 0;
632 struct irq_chip *chip = irq_get_chip(irq);
634 chained_irq_enter(chip, desc);
636 status = readl(bank->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
637 if (!status) {
638 printk(KERN_WARNING
639 "%s: gpio id %d status %#x no interrupt is flaged\n",
640 __func__, bank->id, status);
641 handle_bad_irq(irq, desc);
642 return;
645 while (status) {
646 ctrl = readl(bank->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
649 * Here we must check whether the corresponding GPIO's interrupt
650 * has been enabled, otherwise just skip it
652 if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
653 pr_debug("%s: gpio id %d idx %d happens\n",
654 __func__, bank->id, idx);
655 generic_handle_irq(irq_find_mapping(bank->domain, idx +
656 bank->id * SIRFSOC_GPIO_BANK_SIZE));
659 idx++;
660 status = status >> 1;
663 chained_irq_exit(chip, desc);
666 static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_bank *bank, unsigned ctrl_offset)
668 u32 val;
670 val = readl(bank->chip.regs + ctrl_offset);
671 val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
672 writel(val, bank->chip.regs + ctrl_offset);
675 static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
677 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
678 unsigned long flags;
680 if (pinctrl_request_gpio(chip->base + offset))
681 return -ENODEV;
683 spin_lock_irqsave(&bank->lock, flags);
686 * default status:
687 * set direction as input and mask irq
689 sirfsoc_gpio_set_input(bank, SIRFSOC_GPIO_CTRL(bank->id, offset));
690 __sirfsoc_gpio_irq_mask(bank, offset);
692 spin_unlock_irqrestore(&bank->lock, flags);
694 return 0;
697 static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
699 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
700 unsigned long flags;
702 spin_lock_irqsave(&bank->lock, flags);
704 __sirfsoc_gpio_irq_mask(bank, offset);
705 sirfsoc_gpio_set_input(bank, SIRFSOC_GPIO_CTRL(bank->id, offset));
707 spin_unlock_irqrestore(&bank->lock, flags);
709 pinctrl_free_gpio(chip->base + offset);
712 static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
714 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
715 int idx = sirfsoc_gpio_to_offset(gpio);
716 unsigned long flags;
717 unsigned offset;
719 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
721 spin_lock_irqsave(&bank->lock, flags);
723 sirfsoc_gpio_set_input(bank, offset);
725 spin_unlock_irqrestore(&bank->lock, flags);
727 return 0;
730 static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_bank *bank, unsigned offset,
731 int value)
733 u32 out_ctrl;
734 unsigned long flags;
736 spin_lock_irqsave(&bank->lock, flags);
738 out_ctrl = readl(bank->chip.regs + offset);
739 if (value)
740 out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
741 else
742 out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
744 out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
745 out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
746 writel(out_ctrl, bank->chip.regs + offset);
748 spin_unlock_irqrestore(&bank->lock, flags);
751 static int sirfsoc_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value)
753 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
754 int idx = sirfsoc_gpio_to_offset(gpio);
755 u32 offset;
756 unsigned long flags;
758 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
760 spin_lock_irqsave(&sgpio_lock, flags);
762 sirfsoc_gpio_set_output(bank, offset, value);
764 spin_unlock_irqrestore(&sgpio_lock, flags);
766 return 0;
769 static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
771 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
772 u32 val;
773 unsigned long flags;
775 spin_lock_irqsave(&bank->lock, flags);
777 val = readl(bank->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
779 spin_unlock_irqrestore(&bank->lock, flags);
781 return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
784 static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
785 int value)
787 struct sirfsoc_gpio_bank *bank = sirfsoc_irqchip_to_bank(chip);
788 u32 ctrl;
789 unsigned long flags;
791 spin_lock_irqsave(&bank->lock, flags);
793 ctrl = readl(bank->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
794 if (value)
795 ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
796 else
797 ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
798 writel(ctrl, bank->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
800 spin_unlock_irqrestore(&bank->lock, flags);
803 static int sirfsoc_gpio_irq_map(struct irq_domain *d, unsigned int irq,
804 irq_hw_number_t hwirq)
806 struct sirfsoc_gpio_bank *bank = d->host_data;
808 if (!bank)
809 return -EINVAL;
811 irq_set_chip(irq, &sirfsoc_irq_chip);
812 irq_set_handler(irq, handle_level_irq);
813 irq_set_chip_data(irq, bank + hwirq / SIRFSOC_GPIO_BANK_SIZE);
814 set_irq_flags(irq, IRQF_VALID);
816 return 0;
819 static const struct irq_domain_ops sirfsoc_gpio_irq_simple_ops = {
820 .map = sirfsoc_gpio_irq_map,
821 .xlate = irq_domain_xlate_twocell,
824 static void sirfsoc_gpio_set_pullup(const u32 *pullups)
826 int i, n;
827 const unsigned long *p = (const unsigned long *)pullups;
829 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
830 for_each_set_bit(n, p + i, BITS_PER_LONG) {
831 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
832 u32 val = readl(sgpio_bank[i].chip.regs + offset);
833 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
834 val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
835 writel(val, sgpio_bank[i].chip.regs + offset);
840 static void sirfsoc_gpio_set_pulldown(const u32 *pulldowns)
842 int i, n;
843 const unsigned long *p = (const unsigned long *)pulldowns;
845 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
846 for_each_set_bit(n, p + i, BITS_PER_LONG) {
847 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
848 u32 val = readl(sgpio_bank[i].chip.regs + offset);
849 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
850 val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
851 writel(val, sgpio_bank[i].chip.regs + offset);
856 static int sirfsoc_gpio_probe(struct device_node *np)
858 int i, err = 0;
859 struct sirfsoc_gpio_bank *bank;
860 void __iomem *regs;
861 struct platform_device *pdev;
862 struct irq_domain *domain;
863 bool is_marco = false;
865 u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];
867 pdev = of_find_device_by_node(np);
868 if (!pdev)
869 return -ENODEV;
871 regs = of_iomap(np, 0);
872 if (!regs)
873 return -ENOMEM;
875 if (of_device_is_compatible(np, "sirf,marco-pinctrl"))
876 is_marco = 1;
878 domain = irq_domain_add_linear(np, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS,
879 &sirfsoc_gpio_irq_simple_ops, sgpio_bank);
880 if (!domain) {
881 pr_err("%s: Failed to create irqdomain\n", np->full_name);
882 err = -ENOSYS;
883 goto out;
886 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
887 bank = &sgpio_bank[i];
888 spin_lock_init(&bank->lock);
889 bank->chip.gc.request = sirfsoc_gpio_request;
890 bank->chip.gc.free = sirfsoc_gpio_free;
891 bank->chip.gc.direction_input = sirfsoc_gpio_direction_input;
892 bank->chip.gc.get = sirfsoc_gpio_get_value;
893 bank->chip.gc.direction_output = sirfsoc_gpio_direction_output;
894 bank->chip.gc.set = sirfsoc_gpio_set_value;
895 bank->chip.gc.to_irq = sirfsoc_gpio_to_irq;
896 bank->chip.gc.base = i * SIRFSOC_GPIO_BANK_SIZE;
897 bank->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE;
898 bank->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
899 bank->chip.gc.of_node = np;
900 bank->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
901 bank->chip.gc.of_gpio_n_cells = 2;
902 bank->chip.gc.dev = &pdev->dev;
903 bank->chip.regs = regs;
904 bank->id = i;
905 bank->is_marco = is_marco;
906 bank->parent_irq = platform_get_irq(pdev, i);
907 if (bank->parent_irq < 0) {
908 err = bank->parent_irq;
909 goto out;
912 err = gpiochip_add(&bank->chip.gc);
913 if (err) {
914 pr_err("%s: error in probe function with status %d\n",
915 np->full_name, err);
916 goto out;
919 bank->domain = domain;
921 irq_set_chained_handler(bank->parent_irq, sirfsoc_gpio_handle_irq);
922 irq_set_handler_data(bank->parent_irq, bank);
925 if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
926 SIRFSOC_GPIO_NO_OF_BANKS))
927 sirfsoc_gpio_set_pullup(pullups);
929 if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
930 SIRFSOC_GPIO_NO_OF_BANKS))
931 sirfsoc_gpio_set_pulldown(pulldowns);
933 return 0;
935 out:
936 iounmap(regs);
937 return err;
940 static int __init sirfsoc_gpio_init(void)
943 struct device_node *np;
945 np = of_find_matching_node(NULL, pinmux_ids);
947 if (!np)
948 return -ENODEV;
950 return sirfsoc_gpio_probe(np);
952 subsys_initcall(sirfsoc_gpio_init);
954 MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
955 "Yuping Luo <yuping.luo@csr.com>, "
956 "Barry Song <baohua.song@csr.com>");
957 MODULE_DESCRIPTION("SIRFSOC pin control driver");
958 MODULE_LICENSE("GPL");