gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Miscellaneous functions for IDT EB434 board
  3. *
  4. * Copyright 2004 IDT Inc. ([email protected])
  5. * Copyright 2006 Phil Sutter <[email protected]>
  6. * Copyright 2007 Florian Fainelli <[email protected]>
  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. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  16. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/export.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/gpio/driver.h>
  35. #include <asm/mach-rc32434/rb.h>
  36. #include <asm/mach-rc32434/gpio.h>
  37. #define GPIOBASE 0x050000
  38. /* Offsets relative to GPIOBASE */
  39. #define GPIOFUNC 0x00
  40. #define GPIOCFG 0x04
  41. #define GPIOD 0x08
  42. #define GPIOILEVEL 0x0C
  43. #define GPIOISTAT 0x10
  44. #define GPIONMIEN 0x14
  45. #define IMASK6 0x38
  46. struct rb532_gpio_chip {
  47. struct gpio_chip chip;
  48. void __iomem *regbase;
  49. };
  50. static struct resource rb532_gpio_reg0_res[] = {
  51. {
  52. .name = "gpio_reg0",
  53. .start = REGBASE + GPIOBASE,
  54. .end = REGBASE + GPIOBASE + sizeof(struct rb532_gpio_reg) - 1,
  55. .flags = IORESOURCE_MEM,
  56. }
  57. };
  58. /* rb532_set_bit - sanely set a bit
  59. *
  60. * bitval: new value for the bit
  61. * offset: bit index in the 4 byte address range
  62. * ioaddr: 4 byte aligned address being altered
  63. */
  64. static inline void rb532_set_bit(unsigned bitval,
  65. unsigned offset, void __iomem *ioaddr)
  66. {
  67. unsigned long flags;
  68. u32 val;
  69. local_irq_save(flags);
  70. val = readl(ioaddr);
  71. val &= ~(!bitval << offset); /* unset bit if bitval == 0 */
  72. val |= (!!bitval << offset); /* set bit if bitval == 1 */
  73. writel(val, ioaddr);
  74. local_irq_restore(flags);
  75. }
  76. /* rb532_get_bit - read a bit
  77. *
  78. * returns the boolean state of the bit, which may be > 1
  79. */
  80. static inline int rb532_get_bit(unsigned offset, void __iomem *ioaddr)
  81. {
  82. return readl(ioaddr) & (1 << offset);
  83. }
  84. /*
  85. * Return GPIO level */
  86. static int rb532_gpio_get(struct gpio_chip *chip, unsigned offset)
  87. {
  88. struct rb532_gpio_chip *gpch;
  89. gpch = gpiochip_get_data(chip);
  90. return !!rb532_get_bit(offset, gpch->regbase + GPIOD);
  91. }
  92. /*
  93. * Set output GPIO level
  94. */
  95. static void rb532_gpio_set(struct gpio_chip *chip,
  96. unsigned offset, int value)
  97. {
  98. struct rb532_gpio_chip *gpch;
  99. gpch = gpiochip_get_data(chip);
  100. rb532_set_bit(value, offset, gpch->regbase + GPIOD);
  101. }
  102. /*
  103. * Set GPIO direction to input
  104. */
  105. static int rb532_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  106. {
  107. struct rb532_gpio_chip *gpch;
  108. gpch = gpiochip_get_data(chip);
  109. /* disable alternate function in case it's set */
  110. rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC);
  111. rb532_set_bit(0, offset, gpch->regbase + GPIOCFG);
  112. return 0;
  113. }
  114. /*
  115. * Set GPIO direction to output
  116. */
  117. static int rb532_gpio_direction_output(struct gpio_chip *chip,
  118. unsigned offset, int value)
  119. {
  120. struct rb532_gpio_chip *gpch;
  121. gpch = gpiochip_get_data(chip);
  122. /* disable alternate function in case it's set */
  123. rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC);
  124. /* set the initial output value */
  125. rb532_set_bit(value, offset, gpch->regbase + GPIOD);
  126. rb532_set_bit(1, offset, gpch->regbase + GPIOCFG);
  127. return 0;
  128. }
  129. static int rb532_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  130. {
  131. return 8 + 4 * 32 + gpio;
  132. }
  133. static struct rb532_gpio_chip rb532_gpio_chip[] = {
  134. [0] = {
  135. .chip = {
  136. .label = "gpio0",
  137. .direction_input = rb532_gpio_direction_input,
  138. .direction_output = rb532_gpio_direction_output,
  139. .get = rb532_gpio_get,
  140. .set = rb532_gpio_set,
  141. .to_irq = rb532_gpio_to_irq,
  142. .base = 0,
  143. .ngpio = 32,
  144. },
  145. },
  146. };
  147. /*
  148. * Set GPIO interrupt level
  149. */
  150. void rb532_gpio_set_ilevel(int bit, unsigned gpio)
  151. {
  152. rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOILEVEL);
  153. }
  154. EXPORT_SYMBOL(rb532_gpio_set_ilevel);
  155. /*
  156. * Set GPIO interrupt status
  157. */
  158. void rb532_gpio_set_istat(int bit, unsigned gpio)
  159. {
  160. rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOISTAT);
  161. }
  162. EXPORT_SYMBOL(rb532_gpio_set_istat);
  163. /*
  164. * Configure GPIO alternate function
  165. */
  166. void rb532_gpio_set_func(unsigned gpio)
  167. {
  168. rb532_set_bit(1, gpio, rb532_gpio_chip->regbase + GPIOFUNC);
  169. }
  170. EXPORT_SYMBOL(rb532_gpio_set_func);
  171. int __init rb532_gpio_init(void)
  172. {
  173. struct resource *r;
  174. r = rb532_gpio_reg0_res;
  175. rb532_gpio_chip->regbase = ioremap(r->start, resource_size(r));
  176. if (!rb532_gpio_chip->regbase) {
  177. printk(KERN_ERR "rb532: cannot remap GPIO register 0\n");
  178. return -ENXIO;
  179. }
  180. /* Register our GPIO chip */
  181. gpiochip_add_data(&rb532_gpio_chip->chip, rb532_gpio_chip);
  182. return 0;
  183. }
  184. arch_initcall(rb532_gpio_init);