mpc8xxx_gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <linux/slab.h>
  18. #define MPC8XXX_GPIO_PINS 32
  19. #define GPIO_DIR 0x00
  20. #define GPIO_ODR 0x04
  21. #define GPIO_DAT 0x08
  22. #define GPIO_IER 0x0c
  23. #define GPIO_IMR 0x10
  24. #define GPIO_ICR 0x14
  25. struct mpc8xxx_gpio_chip {
  26. struct of_mm_gpio_chip mm_gc;
  27. spinlock_t lock;
  28. /*
  29. * shadowed data register to be able to clear/set output pins in
  30. * open drain mode safely
  31. */
  32. u32 data;
  33. };
  34. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  35. {
  36. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  37. }
  38. static inline struct mpc8xxx_gpio_chip *
  39. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  40. {
  41. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  42. }
  43. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  44. {
  45. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  46. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  47. }
  48. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  49. * defined as output cannot be determined by reading GPDAT register,
  50. * so we use shadow data register instead. The status of input pins
  51. * is determined by reading GPDAT register.
  52. */
  53. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  54. {
  55. u32 val;
  56. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  57. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  58. val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
  59. return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
  60. }
  61. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  62. {
  63. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  64. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  65. }
  66. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  67. {
  68. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  69. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  70. unsigned long flags;
  71. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  72. if (val)
  73. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  74. else
  75. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  76. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  77. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  78. }
  79. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  80. {
  81. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  82. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  83. unsigned long flags;
  84. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  85. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  86. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  87. return 0;
  88. }
  89. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  90. {
  91. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  92. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  93. unsigned long flags;
  94. mpc8xxx_gpio_set(gc, gpio, val);
  95. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  96. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  97. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  98. return 0;
  99. }
  100. static void __init mpc8xxx_add_controller(struct device_node *np)
  101. {
  102. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  103. struct of_mm_gpio_chip *mm_gc;
  104. struct of_gpio_chip *of_gc;
  105. struct gpio_chip *gc;
  106. int ret;
  107. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  108. if (!mpc8xxx_gc) {
  109. ret = -ENOMEM;
  110. goto err;
  111. }
  112. spin_lock_init(&mpc8xxx_gc->lock);
  113. mm_gc = &mpc8xxx_gc->mm_gc;
  114. of_gc = &mm_gc->of_gc;
  115. gc = &of_gc->gc;
  116. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  117. of_gc->gpio_cells = 2;
  118. gc->ngpio = MPC8XXX_GPIO_PINS;
  119. gc->direction_input = mpc8xxx_gpio_dir_in;
  120. gc->direction_output = mpc8xxx_gpio_dir_out;
  121. if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
  122. gc->get = mpc8572_gpio_get;
  123. else
  124. gc->get = mpc8xxx_gpio_get;
  125. gc->set = mpc8xxx_gpio_set;
  126. ret = of_mm_gpiochip_add(np, mm_gc);
  127. if (ret)
  128. goto err;
  129. return;
  130. err:
  131. pr_err("%s: registration failed with status %d\n",
  132. np->full_name, ret);
  133. kfree(mpc8xxx_gc);
  134. return;
  135. }
  136. static int __init mpc8xxx_add_gpiochips(void)
  137. {
  138. struct device_node *np;
  139. for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
  140. mpc8xxx_add_controller(np);
  141. for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
  142. mpc8xxx_add_controller(np);
  143. for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
  144. mpc8xxx_add_controller(np);
  145. return 0;
  146. }
  147. arch_initcall(mpc8xxx_add_gpiochips);