driver_gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Broadcom specific AMBA
  3. * GPIO driver
  4. *
  5. * Copyright 2011, Broadcom Corporation
  6. * Copyright 2012, Hauke Mehrtens <[email protected]>
  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/property.h>
  14. #include <linux/bcma/bcma.h>
  15. #include "bcma_private.h"
  16. #define BCMA_GPIO_MAX_PINS 32
  17. static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  18. {
  19. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  20. return !!bcma_chipco_gpio_in(cc, 1 << gpio);
  21. }
  22. static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
  23. int value)
  24. {
  25. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  26. bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
  27. }
  28. static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  29. {
  30. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  31. bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
  32. return 0;
  33. }
  34. static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  35. int value)
  36. {
  37. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  38. bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
  39. bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
  40. return 0;
  41. }
  42. static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
  43. {
  44. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  45. bcma_chipco_gpio_control(cc, 1 << gpio, 0);
  46. /* clear pulldown */
  47. bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
  48. /* Set pullup */
  49. bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
  50. return 0;
  51. }
  52. static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
  53. {
  54. struct bcma_drv_cc *cc = gpiochip_get_data(chip);
  55. /* clear pullup */
  56. bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
  57. }
  58. #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
  59. static void bcma_gpio_irq_unmask(struct irq_data *d)
  60. {
  61. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  62. struct bcma_drv_cc *cc = gpiochip_get_data(gc);
  63. int gpio = irqd_to_hwirq(d);
  64. u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
  65. bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
  66. bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
  67. }
  68. static void bcma_gpio_irq_mask(struct irq_data *d)
  69. {
  70. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  71. struct bcma_drv_cc *cc = gpiochip_get_data(gc);
  72. int gpio = irqd_to_hwirq(d);
  73. bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
  74. }
  75. static struct irq_chip bcma_gpio_irq_chip = {
  76. .name = "BCMA-GPIO",
  77. .irq_mask = bcma_gpio_irq_mask,
  78. .irq_unmask = bcma_gpio_irq_unmask,
  79. };
  80. static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
  81. {
  82. struct bcma_drv_cc *cc = dev_id;
  83. struct gpio_chip *gc = &cc->gpio;
  84. u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
  85. u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
  86. u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
  87. unsigned long irqs = (val ^ pol) & mask;
  88. int gpio;
  89. if (!irqs)
  90. return IRQ_NONE;
  91. for_each_set_bit(gpio, &irqs, gc->ngpio)
  92. generic_handle_domain_irq_safe(gc->irq.domain, gpio);
  93. bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
  94. return IRQ_HANDLED;
  95. }
  96. static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
  97. {
  98. struct gpio_chip *chip = &cc->gpio;
  99. struct gpio_irq_chip *girq = &chip->irq;
  100. int hwirq, err;
  101. if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
  102. return 0;
  103. hwirq = bcma_core_irq(cc->core, 0);
  104. err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
  105. cc);
  106. if (err)
  107. return err;
  108. bcma_chipco_gpio_intmask(cc, ~0, 0);
  109. bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
  110. girq->chip = &bcma_gpio_irq_chip;
  111. /* This will let us handle the parent IRQ in the driver */
  112. girq->parent_handler = NULL;
  113. girq->num_parents = 0;
  114. girq->parents = NULL;
  115. girq->default_type = IRQ_TYPE_NONE;
  116. girq->handler = handle_simple_irq;
  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->parent = bus->dev;
  149. chip->fwnode = dev_fwnode(&cc->core->dev);
  150. switch (bus->chipinfo.id) {
  151. case BCMA_CHIP_ID_BCM4707:
  152. case BCMA_CHIP_ID_BCM5357:
  153. case BCMA_CHIP_ID_BCM53572:
  154. case BCMA_CHIP_ID_BCM53573:
  155. case BCMA_CHIP_ID_BCM47094:
  156. chip->ngpio = 32;
  157. break;
  158. default:
  159. chip->ngpio = 16;
  160. }
  161. /*
  162. * Register SoC GPIO devices with absolute GPIO pin base.
  163. * On MIPS, we don't have Device Tree and we can't use relative (per chip)
  164. * GPIO numbers.
  165. * On some ARM devices, user space may want to access some system GPIO
  166. * pins directly, which is easier to do with a predictable GPIO base.
  167. */
  168. if (IS_BUILTIN(CONFIG_BCM47XX) ||
  169. cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  170. chip->base = bus->num * BCMA_GPIO_MAX_PINS;
  171. else
  172. chip->base = -1;
  173. err = bcma_gpio_irq_init(cc);
  174. if (err)
  175. return err;
  176. err = gpiochip_add_data(chip, cc);
  177. if (err) {
  178. bcma_gpio_irq_exit(cc);
  179. return err;
  180. }
  181. return 0;
  182. }
  183. int bcma_gpio_unregister(struct bcma_drv_cc *cc)
  184. {
  185. bcma_gpio_irq_exit(cc);
  186. gpiochip_remove(&cc->gpio);
  187. return 0;
  188. }