driver_gpio.c 5.5 KB

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