driver.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #ifndef __LINUX_GPIO_DRIVER_H
  2. #define __LINUX_GPIO_DRIVER_H
  3. #include <linux/device.h>
  4. #include <linux/types.h>
  5. #include <linux/module.h>
  6. #include <linux/irq.h>
  7. #include <linux/irqchip/chained_irq.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/lockdep.h>
  10. #include <linux/pinctrl/pinctrl.h>
  11. #include <linux/kconfig.h>
  12. struct gpio_desc;
  13. struct of_phandle_args;
  14. struct device_node;
  15. struct seq_file;
  16. struct gpio_device;
  17. #ifdef CONFIG_GPIOLIB
  18. /**
  19. * enum single_ended_mode - mode for single ended operation
  20. * @LINE_MODE_PUSH_PULL: normal mode for a GPIO line, drive actively high/low
  21. * @LINE_MODE_OPEN_DRAIN: set line to be open drain
  22. * @LINE_MODE_OPEN_SOURCE: set line to be open source
  23. */
  24. enum single_ended_mode {
  25. LINE_MODE_PUSH_PULL,
  26. LINE_MODE_OPEN_DRAIN,
  27. LINE_MODE_OPEN_SOURCE,
  28. };
  29. /**
  30. * struct gpio_chip - abstract a GPIO controller
  31. * @label: a functional name for the GPIO device, such as a part
  32. * number or the name of the SoC IP-block implementing it.
  33. * @gpiodev: the internal state holder, opaque struct
  34. * @parent: optional parent device providing the GPIOs
  35. * @owner: helps prevent removal of modules exporting active GPIOs
  36. * @request: optional hook for chip-specific activation, such as
  37. * enabling module power and clock; may sleep
  38. * @free: optional hook for chip-specific deactivation, such as
  39. * disabling module power and clock; may sleep
  40. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  41. * (same as GPIOF_DIR_XXX), or negative error
  42. * @direction_input: configures signal "offset" as input, or returns error
  43. * @direction_output: configures signal "offset" as output, or returns error
  44. * @get: returns value for signal "offset", 0=low, 1=high, or negative error
  45. * @set: assigns output value for signal "offset"
  46. * @set_multiple: assigns output values for multiple signals defined by "mask"
  47. * @set_debounce: optional hook for setting debounce time for specified gpio in
  48. * interrupt triggered gpio chips
  49. * @set_single_ended: optional hook for setting a line as open drain, open
  50. * source, or non-single ended (restore from open drain/source to normal
  51. * push-pull mode) this should be implemented if the hardware supports
  52. * open drain or open source settings. The GPIOlib will otherwise try
  53. * to emulate open drain/source by not actively driving lines high/low
  54. * if a consumer request this. The driver may return -ENOTSUPP if e.g.
  55. * it supports just open drain but not open source and is called
  56. * with LINE_MODE_OPEN_SOURCE as mode argument.
  57. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  58. * implementation may not sleep
  59. * @dbg_show: optional routine to show contents in debugfs; default code
  60. * will be used when this is omitted, but custom code can show extra
  61. * state (such as pullup/pulldown configuration).
  62. * @base: identifies the first GPIO number handled by this chip;
  63. * or, if negative during registration, requests dynamic ID allocation.
  64. * DEPRECATION: providing anything non-negative and nailing the base
  65. * offset of GPIO chips is deprecated. Please pass -1 as base to
  66. * let gpiolib select the chip base in all possible cases. We want to
  67. * get rid of the static GPIO number space in the long run.
  68. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  69. * handled is (base + ngpio - 1).
  70. * @names: if set, must be an array of strings to use as alternative
  71. * names for the GPIOs in this chip. Any entry in the array
  72. * may be NULL if there is no alias for the GPIO, however the
  73. * array must be @ngpio entries long. A name can include a single printk
  74. * format specifier for an unsigned int. It is substituted by the actual
  75. * number of the gpio.
  76. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  77. * must while accessing GPIO expander chips over I2C or SPI. This
  78. * implies that if the chip supports IRQs, these IRQs need to be threaded
  79. * as the chip access may sleep when e.g. reading out the IRQ status
  80. * registers.
  81. * @irq_not_threaded: flag must be set if @can_sleep is set but the
  82. * IRQs don't need to be threaded
  83. * @read_reg: reader function for generic GPIO
  84. * @write_reg: writer function for generic GPIO
  85. * @pin2mask: some generic GPIO controllers work with the big-endian bits
  86. * notation, e.g. in a 8-bits register, GPIO7 is the least significant
  87. * bit. This callback assigns the right bit mask.
  88. * @reg_dat: data (in) register for generic GPIO
  89. * @reg_set: output set register (out=high) for generic GPIO
  90. * @reg_clk: output clear register (out=low) for generic GPIO
  91. * @reg_dir: direction setting register for generic GPIO
  92. * @bgpio_bits: number of register bits used for a generic GPIO i.e.
  93. * <register width> * 8
  94. * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
  95. * shadowed and real data registers writes together.
  96. * @bgpio_data: shadowed data register for generic GPIO to clear/set bits
  97. * safely.
  98. * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
  99. * direction safely.
  100. * @irqchip: GPIO IRQ chip impl, provided by GPIO driver
  101. * @irqdomain: Interrupt translation domain; responsible for mapping
  102. * between GPIO hwirq number and linux irq number
  103. * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated)
  104. * @irq_handler: the irq handler to use (often a predefined irq core function)
  105. * for GPIO IRQs, provided by GPIO driver
  106. * @irq_default_type: default IRQ triggering type applied during GPIO driver
  107. * initialization, provided by GPIO driver
  108. * @irq_parent: GPIO IRQ chip parent/bank linux irq number,
  109. * provided by GPIO driver
  110. * @lock_key: per GPIO IRQ chip lockdep class
  111. *
  112. * A gpio_chip can help platforms abstract various sources of GPIOs so
  113. * they can all be accessed through a common programing interface.
  114. * Example sources would be SOC controllers, FPGAs, multifunction
  115. * chips, dedicated GPIO expanders, and so on.
  116. *
  117. * Each chip controls a number of signals, identified in method calls
  118. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  119. * are referenced through calls like gpio_get_value(gpio), the offset
  120. * is calculated by subtracting @base from the gpio number.
  121. */
  122. struct gpio_chip {
  123. const char *label;
  124. struct gpio_device *gpiodev;
  125. struct device *parent;
  126. struct module *owner;
  127. int (*request)(struct gpio_chip *chip,
  128. unsigned offset);
  129. void (*free)(struct gpio_chip *chip,
  130. unsigned offset);
  131. int (*get_direction)(struct gpio_chip *chip,
  132. unsigned offset);
  133. int (*direction_input)(struct gpio_chip *chip,
  134. unsigned offset);
  135. int (*direction_output)(struct gpio_chip *chip,
  136. unsigned offset, int value);
  137. int (*get)(struct gpio_chip *chip,
  138. unsigned offset);
  139. void (*set)(struct gpio_chip *chip,
  140. unsigned offset, int value);
  141. void (*set_multiple)(struct gpio_chip *chip,
  142. unsigned long *mask,
  143. unsigned long *bits);
  144. int (*set_debounce)(struct gpio_chip *chip,
  145. unsigned offset,
  146. unsigned debounce);
  147. int (*set_single_ended)(struct gpio_chip *chip,
  148. unsigned offset,
  149. enum single_ended_mode mode);
  150. int (*to_irq)(struct gpio_chip *chip,
  151. unsigned offset);
  152. void (*dbg_show)(struct seq_file *s,
  153. struct gpio_chip *chip);
  154. int base;
  155. u16 ngpio;
  156. const char *const *names;
  157. bool can_sleep;
  158. bool irq_not_threaded;
  159. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  160. unsigned long (*read_reg)(void __iomem *reg);
  161. void (*write_reg)(void __iomem *reg, unsigned long data);
  162. unsigned long (*pin2mask)(struct gpio_chip *gc, unsigned int pin);
  163. void __iomem *reg_dat;
  164. void __iomem *reg_set;
  165. void __iomem *reg_clr;
  166. void __iomem *reg_dir;
  167. int bgpio_bits;
  168. spinlock_t bgpio_lock;
  169. unsigned long bgpio_data;
  170. unsigned long bgpio_dir;
  171. #endif
  172. #ifdef CONFIG_GPIOLIB_IRQCHIP
  173. /*
  174. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  175. * to handle IRQs for most practical cases.
  176. */
  177. struct irq_chip *irqchip;
  178. struct irq_domain *irqdomain;
  179. unsigned int irq_base;
  180. irq_flow_handler_t irq_handler;
  181. unsigned int irq_default_type;
  182. int irq_parent;
  183. struct lock_class_key *lock_key;
  184. #endif
  185. #if defined(CONFIG_OF_GPIO)
  186. /*
  187. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  188. * device tree automatically may have an OF translation
  189. */
  190. struct device_node *of_node;
  191. int of_gpio_n_cells;
  192. int (*of_xlate)(struct gpio_chip *gc,
  193. const struct of_phandle_args *gpiospec, u32 *flags);
  194. #endif
  195. };
  196. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  197. unsigned offset);
  198. /* add/remove chips */
  199. extern int gpiochip_add_data(struct gpio_chip *chip, void *data);
  200. static inline int gpiochip_add(struct gpio_chip *chip)
  201. {
  202. return gpiochip_add_data(chip, NULL);
  203. }
  204. extern void gpiochip_remove(struct gpio_chip *chip);
  205. extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  206. void *data);
  207. extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
  208. extern struct gpio_chip *gpiochip_find(void *data,
  209. int (*match)(struct gpio_chip *chip, void *data));
  210. /* lock/unlock as IRQ */
  211. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
  212. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
  213. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
  214. /* Line status inquiry for drivers */
  215. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
  216. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
  217. /* get driver data */
  218. void *gpiochip_get_data(struct gpio_chip *chip);
  219. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
  220. struct bgpio_pdata {
  221. const char *label;
  222. int base;
  223. int ngpio;
  224. };
  225. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  226. int bgpio_init(struct gpio_chip *gc, struct device *dev,
  227. unsigned long sz, void __iomem *dat, void __iomem *set,
  228. void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
  229. unsigned long flags);
  230. #define BGPIOF_BIG_ENDIAN BIT(0)
  231. #define BGPIOF_UNREADABLE_REG_SET BIT(1) /* reg_set is unreadable */
  232. #define BGPIOF_UNREADABLE_REG_DIR BIT(2) /* reg_dir is unreadable */
  233. #define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
  234. #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */
  235. #define BGPIOF_NO_OUTPUT BIT(5) /* only input */
  236. #endif
  237. #ifdef CONFIG_GPIOLIB_IRQCHIP
  238. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  239. struct irq_chip *irqchip,
  240. int parent_irq,
  241. irq_flow_handler_t parent_handler);
  242. int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  243. struct irq_chip *irqchip,
  244. unsigned int first_irq,
  245. irq_flow_handler_t handler,
  246. unsigned int type,
  247. struct lock_class_key *lock_key);
  248. #ifdef CONFIG_LOCKDEP
  249. #define gpiochip_irqchip_add(...) \
  250. ( \
  251. ({ \
  252. static struct lock_class_key _key; \
  253. _gpiochip_irqchip_add(__VA_ARGS__, &_key); \
  254. }) \
  255. )
  256. #else
  257. #define gpiochip_irqchip_add(...) \
  258. _gpiochip_irqchip_add(__VA_ARGS__, NULL)
  259. #endif
  260. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  261. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
  262. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
  263. #ifdef CONFIG_PINCTRL
  264. /**
  265. * struct gpio_pin_range - pin range controlled by a gpio chip
  266. * @head: list for maintaining set of pin ranges, used internally
  267. * @pctldev: pinctrl device which handles corresponding pins
  268. * @range: actual range of pins controlled by a gpio controller
  269. */
  270. struct gpio_pin_range {
  271. struct list_head node;
  272. struct pinctrl_dev *pctldev;
  273. struct pinctrl_gpio_range range;
  274. };
  275. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  276. unsigned int gpio_offset, unsigned int pin_offset,
  277. unsigned int npins);
  278. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  279. struct pinctrl_dev *pctldev,
  280. unsigned int gpio_offset, const char *pin_group);
  281. void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
  282. #else
  283. static inline int
  284. gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  285. unsigned int gpio_offset, unsigned int pin_offset,
  286. unsigned int npins)
  287. {
  288. return 0;
  289. }
  290. static inline int
  291. gpiochip_add_pingroup_range(struct gpio_chip *chip,
  292. struct pinctrl_dev *pctldev,
  293. unsigned int gpio_offset, const char *pin_group)
  294. {
  295. return 0;
  296. }
  297. static inline void
  298. gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  299. {
  300. }
  301. #endif /* CONFIG_PINCTRL */
  302. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  303. const char *label);
  304. void gpiochip_free_own_desc(struct gpio_desc *desc);
  305. #else /* CONFIG_GPIOLIB */
  306. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  307. {
  308. /* GPIO can never have been requested */
  309. WARN_ON(1);
  310. return ERR_PTR(-ENODEV);
  311. }
  312. #endif /* CONFIG_GPIOLIB */
  313. #endif