gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * linux/arch/arm/mach-w90x900/gpio.c
  3. *
  4. * Generic nuc900 GPIO handling
  5. *
  6. * Wan ZongShun <mcuos.com@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/errno.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <mach/hardware.h>
  24. #define GPIO_BASE (W90X900_VA_GPIO)
  25. #define GPIO_DIR (0x04)
  26. #define GPIO_OUT (0x08)
  27. #define GPIO_IN (0x0C)
  28. #define GROUPINERV (0x10)
  29. #define GPIO_GPIO(Nb) (0x00000001 << (Nb))
  30. #define to_nuc900_gpio_chip(c) container_of(c, struct nuc900_gpio_chip, chip)
  31. #define NUC900_GPIO_CHIP(name, base_gpio, nr_gpio) \
  32. { \
  33. .chip = { \
  34. .label = name, \
  35. .direction_input = nuc900_dir_input, \
  36. .direction_output = nuc900_dir_output, \
  37. .get = nuc900_gpio_get, \
  38. .set = nuc900_gpio_set, \
  39. .base = base_gpio, \
  40. .ngpio = nr_gpio, \
  41. } \
  42. }
  43. struct nuc900_gpio_chip {
  44. struct gpio_chip chip;
  45. void __iomem *regbase; /* Base of group register*/
  46. spinlock_t gpio_lock;
  47. };
  48. static int nuc900_gpio_get(struct gpio_chip *chip, unsigned offset)
  49. {
  50. struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
  51. void __iomem *pio = nuc900_gpio->regbase + GPIO_IN;
  52. unsigned int regval;
  53. regval = __raw_readl(pio);
  54. regval &= GPIO_GPIO(offset);
  55. return (regval != 0);
  56. }
  57. static void nuc900_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  58. {
  59. struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
  60. void __iomem *pio = nuc900_gpio->regbase + GPIO_OUT;
  61. unsigned int regval;
  62. unsigned long flags;
  63. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  64. regval = __raw_readl(pio);
  65. if (val)
  66. regval |= GPIO_GPIO(offset);
  67. else
  68. regval &= ~GPIO_GPIO(offset);
  69. __raw_writel(regval, pio);
  70. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  71. }
  72. static int nuc900_dir_input(struct gpio_chip *chip, unsigned offset)
  73. {
  74. struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
  75. void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
  76. unsigned int regval;
  77. unsigned long flags;
  78. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  79. regval = __raw_readl(pio);
  80. regval &= ~GPIO_GPIO(offset);
  81. __raw_writel(regval, pio);
  82. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  83. return 0;
  84. }
  85. static int nuc900_dir_output(struct gpio_chip *chip, unsigned offset, int val)
  86. {
  87. struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
  88. void __iomem *outreg = nuc900_gpio->regbase + GPIO_OUT;
  89. void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
  90. unsigned int regval;
  91. unsigned long flags;
  92. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  93. regval = __raw_readl(pio);
  94. regval |= GPIO_GPIO(offset);
  95. __raw_writel(regval, pio);
  96. regval = __raw_readl(outreg);
  97. if (val)
  98. regval |= GPIO_GPIO(offset);
  99. else
  100. regval &= ~GPIO_GPIO(offset);
  101. __raw_writel(regval, outreg);
  102. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  103. return 0;
  104. }
  105. static struct nuc900_gpio_chip nuc900_gpio[] = {
  106. NUC900_GPIO_CHIP("GROUPC", 0, 16),
  107. NUC900_GPIO_CHIP("GROUPD", 16, 10),
  108. NUC900_GPIO_CHIP("GROUPE", 26, 14),
  109. NUC900_GPIO_CHIP("GROUPF", 40, 10),
  110. NUC900_GPIO_CHIP("GROUPG", 50, 17),
  111. NUC900_GPIO_CHIP("GROUPH", 67, 8),
  112. NUC900_GPIO_CHIP("GROUPI", 75, 17),
  113. };
  114. void __init nuc900_init_gpio(int nr_group)
  115. {
  116. unsigned i;
  117. struct nuc900_gpio_chip *gpio_chip;
  118. for (i = 0; i < nr_group; i++) {
  119. gpio_chip = &nuc900_gpio[i];
  120. spin_lock_init(&gpio_chip->gpio_lock);
  121. gpio_chip->regbase = GPIO_BASE + i * GROUPINERV;
  122. gpiochip_add(&gpio_chip->chip);
  123. }
  124. }