driver_gpio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Broadcom specific AMBA
  3. * GPIO driver
  4. *
  5. * Copyright 2011, Broadcom Corporation
  6. * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/gpio/driver.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/export.h>
  13. #include <linux/bcma/bcma.h>
  14. #include "bcma_private.h"
  15. #define BCMA_GPIO_MAX_PINS 32
  16. static inline struct bcma_drv_cc *bcma_gpio_get_cc(struct gpio_chip *chip)
  17. {
  18. return container_of(chip, struct bcma_drv_cc, gpio);
  19. }
  20. static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  21. {
  22. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  23. return !!bcma_chipco_gpio_in(cc, 1 << gpio);
  24. }
  25. static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
  26. int value)
  27. {
  28. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  29. bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
  30. }
  31. static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  32. {
  33. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  34. bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
  35. return 0;
  36. }
  37. static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  38. int value)
  39. {
  40. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  41. bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
  42. bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
  43. return 0;
  44. }
  45. static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
  46. {
  47. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  48. bcma_chipco_gpio_control(cc, 1 << gpio, 0);
  49. /* clear pulldown */
  50. bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
  51. /* Set pullup */
  52. bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
  53. return 0;
  54. }
  55. static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
  56. {
  57. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  58. /* clear pullup */
  59. bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
  60. }
  61. #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
  62. static void bcma_gpio_irq_unmask(struct irq_data *d)
  63. {
  64. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  65. struct bcma_drv_cc *cc = bcma_gpio_get_cc(gc);
  66. int gpio = irqd_to_hwirq(d);
  67. u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
  68. bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
  69. bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
  70. }
  71. static void bcma_gpio_irq_mask(struct irq_data *d)
  72. {
  73. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  74. struct bcma_drv_cc *cc = bcma_gpio_get_cc(gc);
  75. int gpio = irqd_to_hwirq(d);
  76. bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
  77. }
  78. static struct irq_chip bcma_gpio_irq_chip = {
  79. .name = "BCMA-GPIO",
  80. .irq_mask = bcma_gpio_irq_mask,
  81. .irq_unmask = bcma_gpio_irq_unmask,
  82. };
  83. static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
  84. {
  85. struct bcma_drv_cc *cc = dev_id;
  86. struct gpio_chip *gc = &cc->gpio;
  87. u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
  88. u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
  89. u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
  90. unsigned long irqs = (val ^ pol) & mask;
  91. int gpio;
  92. if (!irqs)
  93. return IRQ_NONE;
  94. for_each_set_bit(gpio, &irqs, gc->ngpio)
  95. generic_handle_irq(irq_find_mapping(gc->irqdomain, gpio));
  96. bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
  97. return IRQ_HANDLED;
  98. }
  99. static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
  100. {
  101. struct gpio_chip *chip = &cc->gpio;
  102. int hwirq, err;
  103. if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
  104. return 0;
  105. hwirq = bcma_core_irq(cc->core, 0);
  106. err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
  107. cc);
  108. if (err)
  109. return err;
  110. bcma_chipco_gpio_intmask(cc, ~0, 0);
  111. bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
  112. err = gpiochip_irqchip_add(chip,
  113. &bcma_gpio_irq_chip,
  114. 0,
  115. handle_simple_irq,
  116. IRQ_TYPE_NONE);
  117. if (err) {
  118. free_irq(hwirq, cc);
  119. return err;
  120. }
  121. return 0;
  122. }
  123. static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
  124. {
  125. if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
  126. return;
  127. bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
  128. free_irq(bcma_core_irq(cc->core, 0), cc);
  129. }
  130. #else
  131. static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
  132. {
  133. return 0;
  134. }
  135. static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
  136. {
  137. }
  138. #endif
  139. int bcma_gpio_init(struct bcma_drv_cc *cc)
  140. {
  141. struct bcma_bus *bus = cc->core->bus;
  142. struct gpio_chip *chip = &cc->gpio;
  143. int err;
  144. chip->label = "bcma_gpio";
  145. chip->owner = THIS_MODULE;
  146. chip->request = bcma_gpio_request;
  147. chip->free = bcma_gpio_free;
  148. chip->get = bcma_gpio_get_value;
  149. chip->set = bcma_gpio_set_value;
  150. chip->direction_input = bcma_gpio_direction_input;
  151. chip->direction_output = bcma_gpio_direction_output;
  152. chip->owner = THIS_MODULE;
  153. chip->dev = bcma_bus_get_host_dev(bus);
  154. #if IS_BUILTIN(CONFIG_OF)
  155. if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  156. chip->of_node = cc->core->dev.of_node;
  157. #endif
  158. switch (bus->chipinfo.id) {
  159. case BCMA_CHIP_ID_BCM4707:
  160. case BCMA_CHIP_ID_BCM5357:
  161. case BCMA_CHIP_ID_BCM53572:
  162. chip->ngpio = 32;
  163. break;
  164. default:
  165. chip->ngpio = 16;
  166. }
  167. /*
  168. * Register SoC GPIO devices with absolute GPIO pin base.
  169. * On MIPS, we don't have Device Tree and we can't use relative (per chip)
  170. * GPIO numbers.
  171. * On some ARM devices, user space may want to access some system GPIO
  172. * pins directly, which is easier to do with a predictable GPIO base.
  173. */
  174. if (IS_BUILTIN(CONFIG_BCM47XX) ||
  175. cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  176. chip->base = bus->num * BCMA_GPIO_MAX_PINS;
  177. else
  178. chip->base = -1;
  179. err = gpiochip_add(chip);
  180. if (err)
  181. return err;
  182. err = bcma_gpio_irq_init(cc);
  183. if (err) {
  184. gpiochip_remove(chip);
  185. return err;
  186. }
  187. return 0;
  188. }
  189. int bcma_gpio_unregister(struct bcma_drv_cc *cc)
  190. {
  191. bcma_gpio_irq_exit(cc);
  192. gpiochip_remove(&cc->gpio);
  193. return 0;
  194. }