Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / pinctrl / sirf / pinctrl-sirf.c
blob0df72be60704809dccf7a7bf23c4c993e3c4ea0b
1 /*
2 * pinmux driver for CSR SiRFprimaII
4 * Authors:
5 * Rongjun Ying <rongjun.ying@csr.com>
6 * Yuping Luo <yuping.luo@csr.com>
7 * Barry Song <baohua.song@csr.com>
9 * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
10 * company.
12 * Licensed under GPLv2 or later.
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/io.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of_platform.h>
29 #include <linux/bitops.h>
30 #include <linux/gpio.h>
31 #include <linux/of_gpio.h>
33 #include "pinctrl-sirf.h"
35 #define DRIVER_NAME "pinmux-sirf"
37 struct sirfsoc_gpio_bank {
38 int id;
39 int parent_irq;
40 spinlock_t lock;
43 struct sirfsoc_gpio_chip {
44 struct of_mm_gpio_chip chip;
45 struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS];
46 spinlock_t lock;
49 static struct sirfsoc_pin_group *sirfsoc_pin_groups;
50 static int sirfsoc_pingrp_cnt;
52 static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev)
54 return sirfsoc_pingrp_cnt;
57 static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev,
58 unsigned selector)
60 return sirfsoc_pin_groups[selector].name;
63 static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev,
64 unsigned selector,
65 const unsigned **pins,
66 unsigned *num_pins)
68 *pins = sirfsoc_pin_groups[selector].pins;
69 *num_pins = sirfsoc_pin_groups[selector].num_pins;
70 return 0;
73 static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev,
74 struct seq_file *s, unsigned offset)
76 seq_printf(s, " " DRIVER_NAME);
79 static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev,
80 struct device_node *np_config,
81 struct pinctrl_map **map, unsigned *num_maps)
83 struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev);
84 struct device_node *np;
85 struct property *prop;
86 const char *function, *group;
87 int ret, index = 0, count = 0;
89 /* calculate number of maps required */
90 for_each_child_of_node(np_config, np) {
91 ret = of_property_read_string(np, "sirf,function", &function);
92 if (ret < 0) {
93 of_node_put(np);
94 return ret;
97 ret = of_property_count_strings(np, "sirf,pins");
98 if (ret < 0) {
99 of_node_put(np);
100 return ret;
103 count += ret;
106 if (!count) {
107 dev_err(spmx->dev, "No child nodes passed via DT\n");
108 return -ENODEV;
111 *map = kzalloc(sizeof(**map) * count, GFP_KERNEL);
112 if (!*map)
113 return -ENOMEM;
115 for_each_child_of_node(np_config, np) {
116 of_property_read_string(np, "sirf,function", &function);
117 of_property_for_each_string(np, "sirf,pins", prop, group) {
118 (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
119 (*map)[index].data.mux.group = group;
120 (*map)[index].data.mux.function = function;
121 index++;
125 *num_maps = count;
127 return 0;
130 static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev,
131 struct pinctrl_map *map, unsigned num_maps)
133 kfree(map);
136 static struct pinctrl_ops sirfsoc_pctrl_ops = {
137 .get_groups_count = sirfsoc_get_groups_count,
138 .get_group_name = sirfsoc_get_group_name,
139 .get_group_pins = sirfsoc_get_group_pins,
140 .pin_dbg_show = sirfsoc_pin_dbg_show,
141 .dt_node_to_map = sirfsoc_dt_node_to_map,
142 .dt_free_map = sirfsoc_dt_free_map,
145 static struct sirfsoc_pmx_func *sirfsoc_pmx_functions;
146 static int sirfsoc_pmxfunc_cnt;
148 static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx,
149 unsigned selector, bool enable)
151 int i;
152 const struct sirfsoc_padmux *mux =
153 sirfsoc_pmx_functions[selector].padmux;
154 const struct sirfsoc_muxmask *mask = mux->muxmask;
156 for (i = 0; i < mux->muxmask_counts; i++) {
157 u32 muxval;
158 muxval = readl(spmx->gpio_virtbase +
159 SIRFSOC_GPIO_PAD_EN(mask[i].group));
160 if (enable)
161 muxval = muxval & ~mask[i].mask;
162 else
163 muxval = muxval | mask[i].mask;
164 writel(muxval, spmx->gpio_virtbase +
165 SIRFSOC_GPIO_PAD_EN(mask[i].group));
168 if (mux->funcmask && enable) {
169 u32 func_en_val;
171 func_en_val =
172 readl(spmx->rsc_virtbase + mux->ctrlreg);
173 func_en_val =
174 (func_en_val & ~mux->funcmask) | (mux->funcval);
175 writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg);
179 static int sirfsoc_pinmux_set_mux(struct pinctrl_dev *pmxdev,
180 unsigned selector,
181 unsigned group)
183 struct sirfsoc_pmx *spmx;
185 spmx = pinctrl_dev_get_drvdata(pmxdev);
186 sirfsoc_pinmux_endisable(spmx, selector, true);
188 return 0;
191 static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev)
193 return sirfsoc_pmxfunc_cnt;
196 static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev,
197 unsigned selector)
199 return sirfsoc_pmx_functions[selector].name;
202 static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev,
203 unsigned selector,
204 const char * const **groups,
205 unsigned * const num_groups)
207 *groups = sirfsoc_pmx_functions[selector].groups;
208 *num_groups = sirfsoc_pmx_functions[selector].num_groups;
209 return 0;
212 static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev,
213 struct pinctrl_gpio_range *range, unsigned offset)
215 struct sirfsoc_pmx *spmx;
217 int group = range->id;
219 u32 muxval;
221 spmx = pinctrl_dev_get_drvdata(pmxdev);
223 muxval = readl(spmx->gpio_virtbase +
224 SIRFSOC_GPIO_PAD_EN(group));
225 muxval = muxval | (1 << (offset - range->pin_base));
226 writel(muxval, spmx->gpio_virtbase +
227 SIRFSOC_GPIO_PAD_EN(group));
229 return 0;
232 static struct pinmux_ops sirfsoc_pinmux_ops = {
233 .set_mux = sirfsoc_pinmux_set_mux,
234 .get_functions_count = sirfsoc_pinmux_get_funcs_count,
235 .get_function_name = sirfsoc_pinmux_get_func_name,
236 .get_function_groups = sirfsoc_pinmux_get_groups,
237 .gpio_request_enable = sirfsoc_pinmux_request_gpio,
240 static struct pinctrl_desc sirfsoc_pinmux_desc = {
241 .name = DRIVER_NAME,
242 .pctlops = &sirfsoc_pctrl_ops,
243 .pmxops = &sirfsoc_pinmux_ops,
244 .owner = THIS_MODULE,
247 static void __iomem *sirfsoc_rsc_of_iomap(void)
249 const struct of_device_id rsc_ids[] = {
250 { .compatible = "sirf,prima2-rsc" },
253 struct device_node *np;
255 np = of_find_matching_node(NULL, rsc_ids);
256 if (!np)
257 panic("unable to find compatible rsc node in dtb\n");
259 return of_iomap(np, 0);
262 static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc,
263 const struct of_phandle_args *gpiospec,
264 u32 *flags)
266 if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE)
267 return -EINVAL;
269 if (flags)
270 *flags = gpiospec->args[1];
272 return gpiospec->args[0];
275 static const struct of_device_id pinmux_ids[] = {
276 { .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, },
277 { .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, },
281 static int sirfsoc_pinmux_probe(struct platform_device *pdev)
283 int ret;
284 struct sirfsoc_pmx *spmx;
285 struct device_node *np = pdev->dev.of_node;
286 const struct sirfsoc_pinctrl_data *pdata;
288 /* Create state holders etc for this driver */
289 spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL);
290 if (!spmx)
291 return -ENOMEM;
293 spmx->dev = &pdev->dev;
295 platform_set_drvdata(pdev, spmx);
297 spmx->gpio_virtbase = of_iomap(np, 0);
298 if (!spmx->gpio_virtbase) {
299 dev_err(&pdev->dev, "can't map gpio registers\n");
300 return -ENOMEM;
303 spmx->rsc_virtbase = sirfsoc_rsc_of_iomap();
304 if (!spmx->rsc_virtbase) {
305 ret = -ENOMEM;
306 dev_err(&pdev->dev, "can't map rsc registers\n");
307 goto out_no_rsc_remap;
310 pdata = of_match_node(pinmux_ids, np)->data;
311 sirfsoc_pin_groups = pdata->grps;
312 sirfsoc_pingrp_cnt = pdata->grps_cnt;
313 sirfsoc_pmx_functions = pdata->funcs;
314 sirfsoc_pmxfunc_cnt = pdata->funcs_cnt;
315 sirfsoc_pinmux_desc.pins = pdata->pads;
316 sirfsoc_pinmux_desc.npins = pdata->pads_cnt;
319 /* Now register the pin controller and all pins it handles */
320 spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx);
321 if (IS_ERR(spmx->pmx)) {
322 dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n");
323 ret = PTR_ERR(spmx->pmx);
324 goto out_no_pmx;
327 dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n");
329 return 0;
331 out_no_pmx:
332 iounmap(spmx->rsc_virtbase);
333 out_no_rsc_remap:
334 iounmap(spmx->gpio_virtbase);
335 return ret;
338 #ifdef CONFIG_PM_SLEEP
339 static int sirfsoc_pinmux_suspend_noirq(struct device *dev)
341 int i, j;
342 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
344 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
345 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
346 spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase +
347 SIRFSOC_GPIO_CTRL(i, j));
349 spmx->ints_regs[i] = readl(spmx->gpio_virtbase +
350 SIRFSOC_GPIO_INT_STATUS(i));
351 spmx->paden_regs[i] = readl(spmx->gpio_virtbase +
352 SIRFSOC_GPIO_PAD_EN(i));
354 spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
356 for (i = 0; i < 3; i++)
357 spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i);
359 return 0;
362 static int sirfsoc_pinmux_resume_noirq(struct device *dev)
364 int i, j;
365 struct sirfsoc_pmx *spmx = dev_get_drvdata(dev);
367 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
368 for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) {
369 writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase +
370 SIRFSOC_GPIO_CTRL(i, j));
372 writel(spmx->ints_regs[i], spmx->gpio_virtbase +
373 SIRFSOC_GPIO_INT_STATUS(i));
374 writel(spmx->paden_regs[i], spmx->gpio_virtbase +
375 SIRFSOC_GPIO_PAD_EN(i));
377 writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0);
379 for (i = 0; i < 3; i++)
380 writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i);
382 return 0;
385 static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = {
386 .suspend_noirq = sirfsoc_pinmux_suspend_noirq,
387 .resume_noirq = sirfsoc_pinmux_resume_noirq,
388 .freeze_noirq = sirfsoc_pinmux_suspend_noirq,
389 .restore_noirq = sirfsoc_pinmux_resume_noirq,
391 #endif
393 static struct platform_driver sirfsoc_pinmux_driver = {
394 .driver = {
395 .name = DRIVER_NAME,
396 .of_match_table = pinmux_ids,
397 #ifdef CONFIG_PM_SLEEP
398 .pm = &sirfsoc_pinmux_pm_ops,
399 #endif
401 .probe = sirfsoc_pinmux_probe,
404 static int __init sirfsoc_pinmux_init(void)
406 return platform_driver_register(&sirfsoc_pinmux_driver);
408 arch_initcall(sirfsoc_pinmux_init);
410 static inline struct sirfsoc_gpio_bank *
411 sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset)
413 return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE];
416 static inline int sirfsoc_gpio_to_bankoff(unsigned int offset)
418 return offset % SIRFSOC_GPIO_BANK_SIZE;
421 static void sirfsoc_gpio_irq_ack(struct irq_data *d)
423 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
424 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
425 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
426 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
427 u32 val, offset;
428 unsigned long flags;
430 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
432 spin_lock_irqsave(&sgpio->lock, flags);
434 val = readl(sgpio->chip.regs + offset);
436 writel(val, sgpio->chip.regs + offset);
438 spin_unlock_irqrestore(&sgpio->lock, flags);
441 static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio,
442 struct sirfsoc_gpio_bank *bank,
443 int idx)
445 u32 val, offset;
446 unsigned long flags;
448 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
450 spin_lock_irqsave(&sgpio->lock, flags);
452 val = readl(sgpio->chip.regs + offset);
453 val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
454 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
455 writel(val, sgpio->chip.regs + offset);
457 spin_unlock_irqrestore(&sgpio->lock, flags);
460 static void sirfsoc_gpio_irq_mask(struct irq_data *d)
462 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
463 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
464 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
466 __sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE);
469 static void sirfsoc_gpio_irq_unmask(struct irq_data *d)
471 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
472 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
473 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
474 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
475 u32 val, offset;
476 unsigned long flags;
478 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
480 spin_lock_irqsave(&sgpio->lock, flags);
482 val = readl(sgpio->chip.regs + offset);
483 val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK;
484 val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK;
485 writel(val, sgpio->chip.regs + offset);
487 spin_unlock_irqrestore(&sgpio->lock, flags);
490 static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type)
492 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
493 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
494 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq);
495 int idx = sirfsoc_gpio_to_bankoff(d->hwirq);
496 u32 val, offset;
497 unsigned long flags;
499 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
501 spin_lock_irqsave(&sgpio->lock, flags);
503 val = readl(sgpio->chip.regs + offset);
504 val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK);
506 switch (type) {
507 case IRQ_TYPE_NONE:
508 break;
509 case IRQ_TYPE_EDGE_RISING:
510 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
511 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
512 val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
513 break;
514 case IRQ_TYPE_EDGE_FALLING:
515 val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
516 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
517 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
518 break;
519 case IRQ_TYPE_EDGE_BOTH:
520 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
521 SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
522 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK;
523 break;
524 case IRQ_TYPE_LEVEL_LOW:
525 val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK |
526 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
527 val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK;
528 break;
529 case IRQ_TYPE_LEVEL_HIGH:
530 val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK;
531 val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK |
532 SIRFSOC_GPIO_CTL_INTR_TYPE_MASK);
533 break;
536 writel(val, sgpio->chip.regs + offset);
538 spin_unlock_irqrestore(&sgpio->lock, flags);
540 return 0;
543 static struct irq_chip sirfsoc_irq_chip = {
544 .name = "sirf-gpio-irq",
545 .irq_ack = sirfsoc_gpio_irq_ack,
546 .irq_mask = sirfsoc_gpio_irq_mask,
547 .irq_unmask = sirfsoc_gpio_irq_unmask,
548 .irq_set_type = sirfsoc_gpio_irq_type,
551 static void sirfsoc_gpio_handle_irq(struct irq_desc *desc)
553 unsigned int irq = irq_desc_get_irq(desc);
554 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
555 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc);
556 struct sirfsoc_gpio_bank *bank;
557 u32 status, ctrl;
558 int idx = 0;
559 struct irq_chip *chip = irq_desc_get_chip(desc);
560 int i;
562 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
563 bank = &sgpio->sgpio_bank[i];
564 if (bank->parent_irq == irq)
565 break;
567 BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS);
569 chained_irq_enter(chip, desc);
571 status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id));
572 if (!status) {
573 printk(KERN_WARNING
574 "%s: gpio id %d status %#x no interrupt is flagged\n",
575 __func__, bank->id, status);
576 handle_bad_irq(desc);
577 return;
580 while (status) {
581 ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx));
584 * Here we must check whether the corresponding GPIO's interrupt
585 * has been enabled, otherwise just skip it
587 if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
588 pr_debug("%s: gpio id %d idx %d happens\n",
589 __func__, bank->id, idx);
590 generic_handle_irq(irq_find_mapping(gc->irqdomain, idx +
591 bank->id * SIRFSOC_GPIO_BANK_SIZE));
594 idx++;
595 status = status >> 1;
598 chained_irq_exit(chip, desc);
601 static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio,
602 unsigned ctrl_offset)
604 u32 val;
606 val = readl(sgpio->chip.regs + ctrl_offset);
607 val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK;
608 writel(val, sgpio->chip.regs + ctrl_offset);
611 static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset)
613 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
614 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
615 unsigned long flags;
617 if (pinctrl_request_gpio(chip->base + offset))
618 return -ENODEV;
620 spin_lock_irqsave(&bank->lock, flags);
623 * default status:
624 * set direction as input and mask irq
626 sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
627 __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
629 spin_unlock_irqrestore(&bank->lock, flags);
631 return 0;
634 static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset)
636 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
637 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
638 unsigned long flags;
640 spin_lock_irqsave(&bank->lock, flags);
642 __sirfsoc_gpio_irq_mask(sgpio, bank, offset);
643 sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset));
645 spin_unlock_irqrestore(&bank->lock, flags);
647 pinctrl_free_gpio(chip->base + offset);
650 static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
652 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
653 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
654 int idx = sirfsoc_gpio_to_bankoff(gpio);
655 unsigned long flags;
656 unsigned offset;
658 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
660 spin_lock_irqsave(&bank->lock, flags);
662 sirfsoc_gpio_set_input(sgpio, offset);
664 spin_unlock_irqrestore(&bank->lock, flags);
666 return 0;
669 static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio,
670 struct sirfsoc_gpio_bank *bank,
671 unsigned offset,
672 int value)
674 u32 out_ctrl;
675 unsigned long flags;
677 spin_lock_irqsave(&bank->lock, flags);
679 out_ctrl = readl(sgpio->chip.regs + offset);
680 if (value)
681 out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
682 else
683 out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
685 out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK;
686 out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK;
687 writel(out_ctrl, sgpio->chip.regs + offset);
689 spin_unlock_irqrestore(&bank->lock, flags);
692 static int sirfsoc_gpio_direction_output(struct gpio_chip *chip,
693 unsigned gpio, int value)
695 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
696 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio);
697 int idx = sirfsoc_gpio_to_bankoff(gpio);
698 u32 offset;
699 unsigned long flags;
701 offset = SIRFSOC_GPIO_CTRL(bank->id, idx);
703 spin_lock_irqsave(&sgpio->lock, flags);
705 sirfsoc_gpio_set_output(sgpio, bank, offset, value);
707 spin_unlock_irqrestore(&sgpio->lock, flags);
709 return 0;
712 static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset)
714 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
715 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
716 u32 val;
717 unsigned long flags;
719 spin_lock_irqsave(&bank->lock, flags);
721 val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
723 spin_unlock_irqrestore(&bank->lock, flags);
725 return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK);
728 static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset,
729 int value)
731 struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip);
732 struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset);
733 u32 ctrl;
734 unsigned long flags;
736 spin_lock_irqsave(&bank->lock, flags);
738 ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
739 if (value)
740 ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK;
741 else
742 ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK;
743 writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset));
745 spin_unlock_irqrestore(&bank->lock, flags);
748 static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio,
749 const u32 *pullups)
751 int i, n;
752 const unsigned long *p = (const unsigned long *)pullups;
754 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
755 for_each_set_bit(n, p + i, BITS_PER_LONG) {
756 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
757 u32 val = readl(sgpio->chip.regs + offset);
758 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
759 val |= SIRFSOC_GPIO_CTL_PULL_HIGH;
760 writel(val, sgpio->chip.regs + offset);
765 static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio,
766 const u32 *pulldowns)
768 int i, n;
769 const unsigned long *p = (const unsigned long *)pulldowns;
771 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
772 for_each_set_bit(n, p + i, BITS_PER_LONG) {
773 u32 offset = SIRFSOC_GPIO_CTRL(i, n);
774 u32 val = readl(sgpio->chip.regs + offset);
775 val |= SIRFSOC_GPIO_CTL_PULL_MASK;
776 val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH;
777 writel(val, sgpio->chip.regs + offset);
782 static int sirfsoc_gpio_probe(struct device_node *np)
784 int i, err = 0;
785 static struct sirfsoc_gpio_chip *sgpio;
786 struct sirfsoc_gpio_bank *bank;
787 void __iomem *regs;
788 struct platform_device *pdev;
790 u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS];
792 pdev = of_find_device_by_node(np);
793 if (!pdev)
794 return -ENODEV;
796 sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL);
797 if (!sgpio)
798 return -ENOMEM;
799 spin_lock_init(&sgpio->lock);
801 regs = of_iomap(np, 0);
802 if (!regs)
803 return -ENOMEM;
805 sgpio->chip.gc.request = sirfsoc_gpio_request;
806 sgpio->chip.gc.free = sirfsoc_gpio_free;
807 sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input;
808 sgpio->chip.gc.get = sirfsoc_gpio_get_value;
809 sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output;
810 sgpio->chip.gc.set = sirfsoc_gpio_set_value;
811 sgpio->chip.gc.base = 0;
812 sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS;
813 sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL);
814 sgpio->chip.gc.of_node = np;
815 sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate;
816 sgpio->chip.gc.of_gpio_n_cells = 2;
817 sgpio->chip.gc.parent = &pdev->dev;
818 sgpio->chip.regs = regs;
820 err = gpiochip_add_data(&sgpio->chip.gc, sgpio);
821 if (err) {
822 dev_err(&pdev->dev, "%s: error in probe function with status %d\n",
823 np->full_name, err);
824 goto out;
827 err = gpiochip_irqchip_add(&sgpio->chip.gc,
828 &sirfsoc_irq_chip,
829 0, handle_level_irq,
830 IRQ_TYPE_NONE);
831 if (err) {
832 dev_err(&pdev->dev,
833 "could not connect irqchip to gpiochip\n");
834 goto out_banks;
837 for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) {
838 bank = &sgpio->sgpio_bank[i];
839 spin_lock_init(&bank->lock);
840 bank->parent_irq = platform_get_irq(pdev, i);
841 if (bank->parent_irq < 0) {
842 err = bank->parent_irq;
843 goto out_banks;
846 gpiochip_set_chained_irqchip(&sgpio->chip.gc,
847 &sirfsoc_irq_chip,
848 bank->parent_irq,
849 sirfsoc_gpio_handle_irq);
852 err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev),
853 0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS);
854 if (err) {
855 dev_err(&pdev->dev,
856 "could not add gpiochip pin range\n");
857 goto out_no_range;
860 if (!of_property_read_u32_array(np, "sirf,pullups", pullups,
861 SIRFSOC_GPIO_NO_OF_BANKS))
862 sirfsoc_gpio_set_pullup(sgpio, pullups);
864 if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns,
865 SIRFSOC_GPIO_NO_OF_BANKS))
866 sirfsoc_gpio_set_pulldown(sgpio, pulldowns);
868 return 0;
870 out_no_range:
871 out_banks:
872 gpiochip_remove(&sgpio->chip.gc);
873 out:
874 iounmap(regs);
875 return err;
878 static int __init sirfsoc_gpio_init(void)
881 struct device_node *np;
883 np = of_find_matching_node(NULL, pinmux_ids);
885 if (!np)
886 return -ENODEV;
888 return sirfsoc_gpio_probe(np);
890 subsys_initcall(sirfsoc_gpio_init);