gpio-core.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2008 Simtec Electronics
  4. * http://armlinux.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * S3C Platform - GPIO core
  8. */
  9. #ifndef __PLAT_SAMSUNG_GPIO_CORE_H
  10. #define __PLAT_SAMSUNG_GPIO_CORE_H
  11. /* Bring in machine-local definitions, especially S3C_GPIO_END */
  12. #include <mach/gpio-samsung.h>
  13. #define GPIOCON_OFF (0x00)
  14. #define GPIODAT_OFF (0x04)
  15. #define con_4bit_shift(__off) ((__off) * 4)
  16. /* Define the core gpiolib support functions that the s3c platforms may
  17. * need to extend or change depending on the hardware and the s3c chip
  18. * selected at build or found at run time.
  19. *
  20. * These definitions are not intended for driver inclusion, there is
  21. * nothing here that should not live outside the platform and core
  22. * specific code.
  23. */
  24. struct samsung_gpio_chip;
  25. /**
  26. * struct samsung_gpio_pm - power management (suspend/resume) information
  27. * @save: Routine to save the state of the GPIO block
  28. * @resume: Routine to resume the GPIO block.
  29. */
  30. struct samsung_gpio_pm {
  31. void (*save)(struct samsung_gpio_chip *chip);
  32. void (*resume)(struct samsung_gpio_chip *chip);
  33. };
  34. struct samsung_gpio_cfg;
  35. /**
  36. * struct samsung_gpio_chip - wrapper for specific implementation of gpio
  37. * @chip: The chip structure to be exported via gpiolib.
  38. * @base: The base pointer to the gpio configuration registers.
  39. * @group: The group register number for gpio interrupt support.
  40. * @irq_base: The base irq number.
  41. * @config: special function and pull-resistor control information.
  42. * @lock: Lock for exclusive access to this gpio bank.
  43. * @pm_save: Save information for suspend/resume support.
  44. * @bitmap_gpio_int: Bitmap for representing GPIO interrupt or not.
  45. *
  46. * This wrapper provides the necessary information for the Samsung
  47. * specific gpios being registered with gpiolib.
  48. *
  49. * The lock protects each gpio bank from multiple access of the shared
  50. * configuration registers, or from reading of data whilst another thread
  51. * is writing to the register set.
  52. *
  53. * Each chip has its own lock to avoid any contention between different
  54. * CPU cores trying to get one lock for different GPIO banks, where each
  55. * bank of GPIO has its own register space and configuration registers.
  56. */
  57. struct samsung_gpio_chip {
  58. struct gpio_chip chip;
  59. struct samsung_gpio_cfg *config;
  60. struct samsung_gpio_pm *pm;
  61. void __iomem *base;
  62. int irq_base;
  63. int group;
  64. spinlock_t lock;
  65. #ifdef CONFIG_PM
  66. u32 pm_save[4];
  67. #endif
  68. u32 bitmap_gpio_int;
  69. };
  70. static inline struct samsung_gpio_chip *to_samsung_gpio(struct gpio_chip *gpc)
  71. {
  72. return container_of(gpc, struct samsung_gpio_chip, chip);
  73. }
  74. /**
  75. * samsung_gpiolib_to_irq - convert gpio pin to irq number
  76. * @chip: The gpio chip that the pin belongs to.
  77. * @offset: The offset of the pin in the chip.
  78. *
  79. * This helper returns the irq number calculated from the chip->irq_base and
  80. * the provided offset.
  81. */
  82. extern int samsung_gpiolib_to_irq(struct gpio_chip *chip, unsigned int offset);
  83. /* exported for core SoC support to change */
  84. extern struct samsung_gpio_cfg s3c24xx_gpiocfg_default;
  85. #ifdef CONFIG_S3C_GPIO_TRACK
  86. extern struct samsung_gpio_chip *s3c_gpios[S3C_GPIO_END];
  87. static inline struct samsung_gpio_chip *samsung_gpiolib_getchip(unsigned int chip)
  88. {
  89. return (chip < S3C_GPIO_END) ? s3c_gpios[chip] : NULL;
  90. }
  91. #else
  92. /* machine specific code should provide samsung_gpiolib_getchip */
  93. extern struct samsung_gpio_chip s3c24xx_gpios[];
  94. static inline struct samsung_gpio_chip *samsung_gpiolib_getchip(unsigned int pin)
  95. {
  96. struct samsung_gpio_chip *chip;
  97. if (pin > S3C_GPIO_END)
  98. return NULL;
  99. chip = &s3c24xx_gpios[pin/32];
  100. return ((pin - chip->chip.base) < chip->chip.ngpio) ? chip : NULL;
  101. }
  102. static inline void s3c_gpiolib_track(struct samsung_gpio_chip *chip) { }
  103. #endif
  104. #ifdef CONFIG_PM
  105. extern struct samsung_gpio_pm samsung_gpio_pm_1bit;
  106. extern struct samsung_gpio_pm samsung_gpio_pm_2bit;
  107. extern struct samsung_gpio_pm samsung_gpio_pm_4bit;
  108. #define __gpio_pm(x) x
  109. #else
  110. #define samsung_gpio_pm_1bit NULL
  111. #define samsung_gpio_pm_2bit NULL
  112. #define samsung_gpio_pm_4bit NULL
  113. #define __gpio_pm(x) NULL
  114. #endif /* CONFIG_PM */
  115. /* locking wrappers to deal with multiple access to the same gpio bank */
  116. #define samsung_gpio_lock(_oc, _fl) spin_lock_irqsave(&(_oc)->lock, _fl)
  117. #define samsung_gpio_unlock(_oc, _fl) spin_unlock_irqrestore(&(_oc)->lock, _fl)
  118. #endif /* __PLAT_SAMSUNG_GPIO_CORE_H */