driver_gpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.h>
  11. #include <linux/export.h>
  12. #include <linux/bcma/bcma.h>
  13. #include "bcma_private.h"
  14. static inline struct bcma_drv_cc *bcma_gpio_get_cc(struct gpio_chip *chip)
  15. {
  16. return container_of(chip, struct bcma_drv_cc, gpio);
  17. }
  18. static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  19. {
  20. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  21. return !!bcma_chipco_gpio_in(cc, 1 << gpio);
  22. }
  23. static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
  24. int value)
  25. {
  26. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  27. bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
  28. }
  29. static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  30. {
  31. struct bcma_drv_cc *cc = bcma_gpio_get_cc(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 = bcma_gpio_get_cc(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 = bcma_gpio_get_cc(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 = bcma_gpio_get_cc(chip);
  56. /* clear pullup */
  57. bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
  58. }
  59. static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  60. {
  61. struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
  62. if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  63. return bcma_core_irq(cc->core);
  64. else
  65. return -EINVAL;
  66. }
  67. int bcma_gpio_init(struct bcma_drv_cc *cc)
  68. {
  69. struct gpio_chip *chip = &cc->gpio;
  70. chip->label = "bcma_gpio";
  71. chip->owner = THIS_MODULE;
  72. chip->request = bcma_gpio_request;
  73. chip->free = bcma_gpio_free;
  74. chip->get = bcma_gpio_get_value;
  75. chip->set = bcma_gpio_set_value;
  76. chip->direction_input = bcma_gpio_direction_input;
  77. chip->direction_output = bcma_gpio_direction_output;
  78. chip->to_irq = bcma_gpio_to_irq;
  79. chip->ngpio = 16;
  80. /* There is just one SoC in one device and its GPIO addresses should be
  81. * deterministic to address them more easily. The other buses could get
  82. * a random base number. */
  83. if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  84. chip->base = 0;
  85. else
  86. chip->base = -1;
  87. return gpiochip_add(chip);
  88. }
  89. int bcma_gpio_unregister(struct bcma_drv_cc *cc)
  90. {
  91. return gpiochip_remove(&cc->gpio);
  92. }