mpc8xxx_gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  48. * defined as output cannot be determined by reading GPDAT register,
  49. * so we use shadow data register instead. The status of input pins
  50. * is determined by reading GPDAT register.
  51. */
  52. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  53. {
  54. u32 val;
  55. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  56. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  57. val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
  58. return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
  59. }
  60. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  61. {
  62. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  63. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  64. }
  65. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  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. if (val)
  72. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  73. else
  74. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  75. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  76. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  77. }
  78. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  79. {
  80. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  81. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  82. unsigned long flags;
  83. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  84. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  85. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  86. return 0;
  87. }
  88. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  89. {
  90. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  91. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  92. unsigned long flags;
  93. mpc8xxx_gpio_set(gc, gpio, val);
  94. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  95. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  96. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  97. return 0;
  98. }
  99. static void __init mpc8xxx_add_controller(struct device_node *np)
  100. {
  101. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  102. struct of_mm_gpio_chip *mm_gc;
  103. struct of_gpio_chip *of_gc;
  104. struct gpio_chip *gc;
  105. int ret;
  106. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  107. if (!mpc8xxx_gc) {
  108. ret = -ENOMEM;
  109. goto err;
  110. }
  111. spin_lock_init(&mpc8xxx_gc->lock);
  112. mm_gc = &mpc8xxx_gc->mm_gc;
  113. of_gc = &mm_gc->of_gc;
  114. gc = &of_gc->gc;
  115. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  116. of_gc->gpio_cells = 2;
  117. gc->ngpio = MPC8XXX_GPIO_PINS;
  118. gc->direction_input = mpc8xxx_gpio_dir_in;
  119. gc->direction_output = mpc8xxx_gpio_dir_out;
  120. if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
  121. gc->get = mpc8572_gpio_get;
  122. else
  123. gc->get = mpc8xxx_gpio_get;
  124. gc->set = mpc8xxx_gpio_set;
  125. ret = of_mm_gpiochip_add(np, mm_gc);
  126. if (ret)
  127. goto err;
  128. return;
  129. err:
  130. pr_err("%s: registration failed with status %d\n",
  131. np->full_name, ret);
  132. kfree(mpc8xxx_gc);
  133. return;
  134. }
  135. static int __init mpc8xxx_add_gpiochips(void)
  136. {
  137. struct device_node *np;
  138. for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
  139. mpc8xxx_add_controller(np);
  140. for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
  141. mpc8xxx_add_controller(np);
  142. for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
  143. mpc8xxx_add_controller(np);
  144. return 0;
  145. }
  146. arch_initcall(mpc8xxx_add_gpiochips);