gpio-tnetv107x.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Texas Instruments TNETV107X GPIO Controller
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/gpio.h>
  18. #include <mach/common.h>
  19. #include <mach/tnetv107x.h>
  20. struct tnetv107x_gpio_regs {
  21. u32 idver;
  22. u32 data_in[3];
  23. u32 data_out[3];
  24. u32 direction[3];
  25. u32 enable[3];
  26. };
  27. #define gpio_reg_index(gpio) ((gpio) >> 5)
  28. #define gpio_reg_bit(gpio) BIT((gpio) & 0x1f)
  29. #define gpio_reg_rmw(reg, mask, val) \
  30. __raw_writel((__raw_readl(reg) & ~(mask)) | (val), (reg))
  31. #define gpio_reg_set_bit(reg, gpio) \
  32. gpio_reg_rmw((reg) + gpio_reg_index(gpio), 0, gpio_reg_bit(gpio))
  33. #define gpio_reg_clear_bit(reg, gpio) \
  34. gpio_reg_rmw((reg) + gpio_reg_index(gpio), gpio_reg_bit(gpio), 0)
  35. #define gpio_reg_get_bit(reg, gpio) \
  36. (__raw_readl((reg) + gpio_reg_index(gpio)) & gpio_reg_bit(gpio))
  37. #define chip2controller(chip) \
  38. container_of(chip, struct davinci_gpio_controller, chip)
  39. #define TNETV107X_GPIO_CTLRS DIV_ROUND_UP(TNETV107X_N_GPIO, 32)
  40. static struct davinci_gpio_controller chips[TNETV107X_GPIO_CTLRS];
  41. static int tnetv107x_gpio_request(struct gpio_chip *chip, unsigned offset)
  42. {
  43. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  44. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  45. unsigned gpio = chip->base + offset;
  46. unsigned long flags;
  47. spin_lock_irqsave(&ctlr->lock, flags);
  48. gpio_reg_set_bit(regs->enable, gpio);
  49. spin_unlock_irqrestore(&ctlr->lock, flags);
  50. return 0;
  51. }
  52. static void tnetv107x_gpio_free(struct gpio_chip *chip, unsigned offset)
  53. {
  54. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  55. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  56. unsigned gpio = chip->base + offset;
  57. unsigned long flags;
  58. spin_lock_irqsave(&ctlr->lock, flags);
  59. gpio_reg_clear_bit(regs->enable, gpio);
  60. spin_unlock_irqrestore(&ctlr->lock, flags);
  61. }
  62. static int tnetv107x_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  63. {
  64. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  65. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  66. unsigned gpio = chip->base + offset;
  67. unsigned long flags;
  68. spin_lock_irqsave(&ctlr->lock, flags);
  69. gpio_reg_set_bit(regs->direction, gpio);
  70. spin_unlock_irqrestore(&ctlr->lock, flags);
  71. return 0;
  72. }
  73. static int tnetv107x_gpio_dir_out(struct gpio_chip *chip,
  74. unsigned offset, int value)
  75. {
  76. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  77. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  78. unsigned gpio = chip->base + offset;
  79. unsigned long flags;
  80. spin_lock_irqsave(&ctlr->lock, flags);
  81. if (value)
  82. gpio_reg_set_bit(regs->data_out, gpio);
  83. else
  84. gpio_reg_clear_bit(regs->data_out, gpio);
  85. gpio_reg_clear_bit(regs->direction, gpio);
  86. spin_unlock_irqrestore(&ctlr->lock, flags);
  87. return 0;
  88. }
  89. static int tnetv107x_gpio_get(struct gpio_chip *chip, unsigned offset)
  90. {
  91. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  92. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  93. unsigned gpio = chip->base + offset;
  94. int ret;
  95. ret = gpio_reg_get_bit(regs->data_in, gpio);
  96. return ret ? 1 : 0;
  97. }
  98. static void tnetv107x_gpio_set(struct gpio_chip *chip,
  99. unsigned offset, int value)
  100. {
  101. struct davinci_gpio_controller *ctlr = chip2controller(chip);
  102. struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
  103. unsigned gpio = chip->base + offset;
  104. unsigned long flags;
  105. spin_lock_irqsave(&ctlr->lock, flags);
  106. if (value)
  107. gpio_reg_set_bit(regs->data_out, gpio);
  108. else
  109. gpio_reg_clear_bit(regs->data_out, gpio);
  110. spin_unlock_irqrestore(&ctlr->lock, flags);
  111. }
  112. static int __init tnetv107x_gpio_setup(void)
  113. {
  114. int i, base;
  115. unsigned ngpio;
  116. struct davinci_soc_info *soc_info = &davinci_soc_info;
  117. struct tnetv107x_gpio_regs *regs;
  118. struct davinci_gpio_controller *ctlr;
  119. if (soc_info->gpio_type != GPIO_TYPE_TNETV107X)
  120. return 0;
  121. ngpio = soc_info->gpio_num;
  122. if (ngpio == 0) {
  123. pr_err("GPIO setup: how many GPIOs?\n");
  124. return -EINVAL;
  125. }
  126. if (WARN_ON(TNETV107X_N_GPIO < ngpio))
  127. ngpio = TNETV107X_N_GPIO;
  128. regs = ioremap(soc_info->gpio_base, SZ_4K);
  129. if (WARN_ON(!regs))
  130. return -EINVAL;
  131. for (i = 0, base = 0; base < ngpio; i++, base += 32) {
  132. ctlr = &chips[i];
  133. ctlr->chip.label = "tnetv107x";
  134. ctlr->chip.can_sleep = 0;
  135. ctlr->chip.base = base;
  136. ctlr->chip.ngpio = ngpio - base;
  137. if (ctlr->chip.ngpio > 32)
  138. ctlr->chip.ngpio = 32;
  139. ctlr->chip.request = tnetv107x_gpio_request;
  140. ctlr->chip.free = tnetv107x_gpio_free;
  141. ctlr->chip.direction_input = tnetv107x_gpio_dir_in;
  142. ctlr->chip.get = tnetv107x_gpio_get;
  143. ctlr->chip.direction_output = tnetv107x_gpio_dir_out;
  144. ctlr->chip.set = tnetv107x_gpio_set;
  145. spin_lock_init(&ctlr->lock);
  146. ctlr->regs = regs;
  147. ctlr->set_data = &regs->data_out[i];
  148. ctlr->clr_data = &regs->data_out[i];
  149. ctlr->in_data = &regs->data_in[i];
  150. gpiochip_add(&ctlr->chip);
  151. }
  152. soc_info->gpio_ctlrs = chips;
  153. soc_info->gpio_ctlrs_num = DIV_ROUND_UP(ngpio, 32);
  154. return 0;
  155. }
  156. pure_initcall(tnetv107x_gpio_setup);