driver_gpio.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. int bcma_gpio_init(struct bcma_drv_cc *cc)
  60. {
  61. struct gpio_chip *chip = &cc->gpio;
  62. chip->label = "bcma_gpio";
  63. chip->owner = THIS_MODULE;
  64. chip->request = bcma_gpio_request;
  65. chip->free = bcma_gpio_free;
  66. chip->get = bcma_gpio_get_value;
  67. chip->set = bcma_gpio_set_value;
  68. chip->direction_input = bcma_gpio_direction_input;
  69. chip->direction_output = bcma_gpio_direction_output;
  70. chip->ngpio = 16;
  71. /* There is just one SoC in one device and its GPIO addresses should be
  72. * deterministic to address them more easily. The other buses could get
  73. * a random base number. */
  74. if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
  75. chip->base = 0;
  76. else
  77. chip->base = -1;
  78. return gpiochip_add(chip);
  79. }