driver_gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/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. gpiochip_enable_irq(gc, gpio);
  66. bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
  67. bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
  68. }
  69. static void bcma_gpio_irq_mask(struct irq_data *d)
  70. {
  71. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  72. struct bcma_drv_cc *cc = gpiochip_get_data(gc);
  73. int gpio = irqd_to_hwirq(d);
  74. bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
  75. gpiochip_disable_irq(gc, gpio);
  76. }
  77. static const struct irq_chip bcma_gpio_irq_chip = {
  78. .name = "BCMA-GPIO",
  79. .irq_mask = bcma_gpio_irq_mask,
  80. .irq_unmask = bcma_gpio_irq_unmask,
  81. .flags = IRQCHIP_IMMUTABLE,
  82. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  83. };
  84. static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
  85. {
  86. struct bcma_drv_cc *cc = dev_id;
  87. struct gpio_chip *gc = &cc->gpio;
  88. u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
  89. u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
  90. u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
  91. unsigned long irqs = (val ^ pol) & mask;
  92. int gpio;
  93. if (!irqs)
  94. return IRQ_NONE;
  95. for_each_set_bit(gpio, &irqs, gc->ngpio)
  96. generic_handle_domain_irq_safe(gc->irq.domain, gpio);
  97. bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
  98. return IRQ_HANDLED;
  99. }
  100. static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
  101. {
  102. struct gpio_chip *chip = &cc->gpio;
  103. struct gpio_irq_chip *girq = &chip->irq;
  104. int hwirq, err;
  105. if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
  106. return 0;
  107. hwirq = bcma_core_irq(cc->core, 0);
  108. err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
  109. cc);
  110. if (err)
  111. return err;
  112. bcma_chipco_gpio_intmask(cc, ~0, 0);
  113. bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
  114. gpio_irq_chip_set_chip(girq, &bcma_gpio_irq_chip);
  115. /* This will let us handle the parent IRQ in the driver */
  116. girq->parent_handler = NULL;
  117. girq->num_parents = 0;
  118. girq->parents = NULL;
  119. girq->default_type = IRQ_TYPE_NONE;
  120. girq->handler = handle_simple_irq;
  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->parent = bus->dev;
  153. chip->fwnode = dev_fwnode(&cc->core->dev);
  154. switch (bus->chipinfo.id) {
  155. case BCMA_CHIP_ID_BCM4707:
  156. case BCMA_CHIP_ID_BCM5357:
  157. case BCMA_CHIP_ID_BCM53572:
  158. case BCMA_CHIP_ID_BCM53573:
  159. case BCMA_CHIP_ID_BCM47094:
  160. chip->ngpio = 32;
  161. break;
  162. default:
  163. chip->ngpio = 16;
  164. }
  165. /*
  166. * Register SoC GPIO devices with absolute GPIO pin base.
  167. * On MIPS, we don't have Device Tree and we can't use relative (per chip)
  168. * GPIO numbers.
  169. * On some ARM devices, user space may want to access some system GPIO
  170. * pins directly, which is easier to do with a predictable GPIO base.
  171. */
  172. if (IS_BUILTIN(CONFIG_BCM47XX) ||
  173. cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  174. chip->base = bus->num * BCMA_GPIO_MAX_PINS;
  175. else
  176. chip->base = -1;
  177. err = bcma_gpio_irq_init(cc);
  178. if (err)
  179. return err;
  180. err = gpiochip_add_data(chip, cc);
  181. if (err) {
  182. bcma_gpio_irq_exit(cc);
  183. return err;
  184. }
  185. return 0;
  186. }
  187. int bcma_gpio_unregister(struct bcma_drv_cc *cc)
  188. {
  189. bcma_gpio_irq_exit(cc);
  190. gpiochip_remove(&cc->gpio);
  191. return 0;
  192. }