gpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/driver.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 NUC900_GPIO_CHIP(name, base_gpio, nr_gpio) \
  31. { \
  32. .chip = { \
  33. .label = name, \
  34. .direction_input = nuc900_dir_input, \
  35. .direction_output = nuc900_dir_output, \
  36. .get = nuc900_gpio_get, \
  37. .set = nuc900_gpio_set, \
  38. .base = base_gpio, \
  39. .ngpio = nr_gpio, \
  40. } \
  41. }
  42. struct nuc900_gpio_chip {
  43. struct gpio_chip chip;
  44. void __iomem *regbase; /* Base of group register*/
  45. spinlock_t gpio_lock;
  46. };
  47. static int nuc900_gpio_get(struct gpio_chip *chip, unsigned offset)
  48. {
  49. struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
  50. void __iomem *pio = nuc900_gpio->regbase + GPIO_IN;
  51. unsigned int regval;
  52. regval = __raw_readl(pio);
  53. regval &= GPIO_GPIO(offset);
  54. return (regval != 0);
  55. }
  56. static void nuc900_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  57. {
  58. struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
  59. void __iomem *pio = nuc900_gpio->regbase + GPIO_OUT;
  60. unsigned int regval;
  61. unsigned long flags;
  62. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  63. regval = __raw_readl(pio);
  64. if (val)
  65. regval |= GPIO_GPIO(offset);
  66. else
  67. regval &= ~GPIO_GPIO(offset);
  68. __raw_writel(regval, pio);
  69. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  70. }
  71. static int nuc900_dir_input(struct gpio_chip *chip, unsigned offset)
  72. {
  73. struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
  74. void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
  75. unsigned int regval;
  76. unsigned long flags;
  77. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  78. regval = __raw_readl(pio);
  79. regval &= ~GPIO_GPIO(offset);
  80. __raw_writel(regval, pio);
  81. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  82. return 0;
  83. }
  84. static int nuc900_dir_output(struct gpio_chip *chip, unsigned offset, int val)
  85. {
  86. struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
  87. void __iomem *outreg = nuc900_gpio->regbase + GPIO_OUT;
  88. void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
  89. unsigned int regval;
  90. unsigned long flags;
  91. spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
  92. regval = __raw_readl(pio);
  93. regval |= GPIO_GPIO(offset);
  94. __raw_writel(regval, pio);
  95. regval = __raw_readl(outreg);
  96. if (val)
  97. regval |= GPIO_GPIO(offset);
  98. else
  99. regval &= ~GPIO_GPIO(offset);
  100. __raw_writel(regval, outreg);
  101. spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
  102. return 0;
  103. }
  104. static struct nuc900_gpio_chip nuc900_gpio[] = {
  105. NUC900_GPIO_CHIP("GROUPC", 0, 16),
  106. NUC900_GPIO_CHIP("GROUPD", 16, 10),
  107. NUC900_GPIO_CHIP("GROUPE", 26, 14),
  108. NUC900_GPIO_CHIP("GROUPF", 40, 10),
  109. NUC900_GPIO_CHIP("GROUPG", 50, 17),
  110. NUC900_GPIO_CHIP("GROUPH", 67, 8),
  111. NUC900_GPIO_CHIP("GROUPI", 75, 17),
  112. };
  113. void __init nuc900_init_gpio(int nr_group)
  114. {
  115. unsigned i;
  116. struct nuc900_gpio_chip *gpio_chip;
  117. for (i = 0; i < nr_group; i++) {
  118. gpio_chip = &nuc900_gpio[i];
  119. spin_lock_init(&gpio_chip->gpio_lock);
  120. gpio_chip->regbase = GPIO_BASE + i * GROUPINERV;
  121. gpiochip_add_data(&gpio_chip->chip, gpio_chip);
  122. }
  123. }