mpc8xxx_gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * GPIOs on MPC8349/8572/8610 and compatible
  3. *
  4. * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/gpio.h>
  17. #define MPC8XXX_GPIO_PINS 32
  18. #define GPIO_DIR 0x00
  19. #define GPIO_ODR 0x04
  20. #define GPIO_DAT 0x08
  21. #define GPIO_IER 0x0c
  22. #define GPIO_IMR 0x10
  23. #define GPIO_ICR 0x14
  24. struct mpc8xxx_gpio_chip {
  25. struct of_mm_gpio_chip mm_gc;
  26. spinlock_t lock;
  27. /*
  28. * shadowed data register to be able to clear/set output pins in
  29. * open drain mode safely
  30. */
  31. u32 data;
  32. };
  33. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  34. {
  35. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  36. }
  37. static inline struct mpc8xxx_gpio_chip *
  38. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  39. {
  40. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  41. }
  42. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  43. {
  44. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  45. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  46. }
  47. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  48. {
  49. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  50. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  51. }
  52. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  53. {
  54. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  55. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  56. unsigned long flags;
  57. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  58. if (val)
  59. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  60. else
  61. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  62. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  63. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  64. }
  65. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  66. {
  67. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  68. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  69. unsigned long flags;
  70. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  71. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  72. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  73. return 0;
  74. }
  75. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  76. {
  77. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  78. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  79. unsigned long flags;
  80. mpc8xxx_gpio_set(gc, gpio, val);
  81. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  82. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  83. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  84. return 0;
  85. }
  86. static void __init mpc8xxx_add_controller(struct device_node *np)
  87. {
  88. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  89. struct of_mm_gpio_chip *mm_gc;
  90. struct of_gpio_chip *of_gc;
  91. struct gpio_chip *gc;
  92. int ret;
  93. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  94. if (!mpc8xxx_gc) {
  95. ret = -ENOMEM;
  96. goto err;
  97. }
  98. spin_lock_init(&mpc8xxx_gc->lock);
  99. mm_gc = &mpc8xxx_gc->mm_gc;
  100. of_gc = &mm_gc->of_gc;
  101. gc = &of_gc->gc;
  102. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  103. of_gc->gpio_cells = 2;
  104. gc->ngpio = MPC8XXX_GPIO_PINS;
  105. gc->direction_input = mpc8xxx_gpio_dir_in;
  106. gc->direction_output = mpc8xxx_gpio_dir_out;
  107. gc->get = mpc8xxx_gpio_get;
  108. gc->set = mpc8xxx_gpio_set;
  109. ret = of_mm_gpiochip_add(np, mm_gc);
  110. if (ret)
  111. goto err;
  112. return;
  113. err:
  114. pr_err("%s: registration failed with status %d\n",
  115. np->full_name, ret);
  116. kfree(mpc8xxx_gc);
  117. return;
  118. }
  119. static int __init mpc8xxx_add_gpiochips(void)
  120. {
  121. struct device_node *np;
  122. for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
  123. mpc8xxx_add_controller(np);
  124. for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
  125. mpc8xxx_add_controller(np);
  126. for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
  127. mpc8xxx_add_controller(np);
  128. return 0;
  129. }
  130. arch_initcall(mpc8xxx_add_gpiochips);