gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * QUICC Engine GPIOs
  3. *
  4. * Copyright (c) MontaVista Software, Inc. 2008.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/gpio.h>
  20. #include <asm/qe.h>
  21. struct qe_gpio_chip {
  22. struct of_mm_gpio_chip mm_gc;
  23. spinlock_t lock;
  24. /* shadowed data register to clear/set bits safely */
  25. u32 cpdata;
  26. };
  27. static inline struct qe_gpio_chip *
  28. to_qe_gpio_chip(struct of_mm_gpio_chip *mm_gc)
  29. {
  30. return container_of(mm_gc, struct qe_gpio_chip, mm_gc);
  31. }
  32. static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  33. {
  34. struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
  35. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  36. qe_gc->cpdata = in_be32(&regs->cpdata);
  37. }
  38. static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  39. {
  40. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  41. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  42. u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
  43. return in_be32(&regs->cpdata) & pin_mask;
  44. }
  45. static void qe_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  46. {
  47. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  48. struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
  49. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  50. unsigned long flags;
  51. u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
  52. spin_lock_irqsave(&qe_gc->lock, flags);
  53. if (val)
  54. qe_gc->cpdata |= pin_mask;
  55. else
  56. qe_gc->cpdata &= ~pin_mask;
  57. out_be32(&regs->cpdata, qe_gc->cpdata);
  58. spin_unlock_irqrestore(&qe_gc->lock, flags);
  59. }
  60. static int qe_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  61. {
  62. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  63. struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
  64. unsigned long flags;
  65. spin_lock_irqsave(&qe_gc->lock, flags);
  66. __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_IN, 0, 0, 0);
  67. spin_unlock_irqrestore(&qe_gc->lock, flags);
  68. return 0;
  69. }
  70. static int qe_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  71. {
  72. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  73. struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
  74. unsigned long flags;
  75. spin_lock_irqsave(&qe_gc->lock, flags);
  76. __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_OUT, 0, 0, 0);
  77. spin_unlock_irqrestore(&qe_gc->lock, flags);
  78. qe_gpio_set(gc, gpio, val);
  79. return 0;
  80. }
  81. static int __init qe_add_gpiochips(void)
  82. {
  83. struct device_node *np;
  84. for_each_compatible_node(np, NULL, "fsl,mpc8323-qe-pario-bank") {
  85. int ret;
  86. struct qe_gpio_chip *qe_gc;
  87. struct of_mm_gpio_chip *mm_gc;
  88. struct of_gpio_chip *of_gc;
  89. struct gpio_chip *gc;
  90. qe_gc = kzalloc(sizeof(*qe_gc), GFP_KERNEL);
  91. if (!qe_gc) {
  92. ret = -ENOMEM;
  93. goto err;
  94. }
  95. spin_lock_init(&qe_gc->lock);
  96. mm_gc = &qe_gc->mm_gc;
  97. of_gc = &mm_gc->of_gc;
  98. gc = &of_gc->gc;
  99. mm_gc->save_regs = qe_gpio_save_regs;
  100. of_gc->gpio_cells = 2;
  101. gc->ngpio = QE_PIO_PINS;
  102. gc->direction_input = qe_gpio_dir_in;
  103. gc->direction_output = qe_gpio_dir_out;
  104. gc->get = qe_gpio_get;
  105. gc->set = qe_gpio_set;
  106. ret = of_mm_gpiochip_add(np, mm_gc);
  107. if (ret)
  108. goto err;
  109. continue;
  110. err:
  111. pr_err("%s: registration failed with status %d\n",
  112. np->full_name, ret);
  113. kfree(qe_gc);
  114. /* try others anyway */
  115. }
  116. return 0;
  117. }
  118. arch_initcall(qe_add_gpiochips);