driver_gpio.c 5.7 KB

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