gpio-core.h 4.2 KB

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