driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/irq.h>
  6. #include <linux/irqchip/chained_irq.h>
  7. #include <linux/irqdomain.h>
  8. #include <linux/lockdep.h>
  9. #include <linux/pinctrl/pinctrl.h>
  10. #include <linux/pinctrl/pinconf-generic.h>
  11. struct gpio_desc;
  12. struct of_phandle_args;
  13. struct device_node;
  14. struct seq_file;
  15. struct gpio_device;
  16. struct module;
  17. #ifdef CONFIG_GPIOLIB
  18. #ifdef CONFIG_GPIOLIB_IRQCHIP
  19. /**
  20. * struct gpio_irq_chip - GPIO interrupt controller
  21. */
  22. struct gpio_irq_chip {
  23. /**
  24. * @chip:
  25. *
  26. * GPIO IRQ chip implementation, provided by GPIO driver.
  27. */
  28. struct irq_chip *chip;
  29. /**
  30. * @domain:
  31. *
  32. * Interrupt translation domain; responsible for mapping between GPIO
  33. * hwirq number and Linux IRQ number.
  34. */
  35. struct irq_domain *domain;
  36. /**
  37. * @domain_ops:
  38. *
  39. * Table of interrupt domain operations for this IRQ chip.
  40. */
  41. const struct irq_domain_ops *domain_ops;
  42. /**
  43. * @parent_handler:
  44. *
  45. * The interrupt handler for the GPIO chip's parent interrupts, may be
  46. * NULL if the parent interrupts are nested rather than cascaded.
  47. */
  48. irq_flow_handler_t parent_handler;
  49. /**
  50. * @parent_handler_data:
  51. *
  52. * Data associated, and passed to, the handler for the parent
  53. * interrupt.
  54. */
  55. void *parent_handler_data;
  56. };
  57. static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip)
  58. {
  59. return container_of(chip, struct gpio_irq_chip, chip);
  60. }
  61. #endif
  62. /**
  63. * struct gpio_chip - abstract a GPIO controller
  64. * @label: a functional name for the GPIO device, such as a part
  65. * number or the name of the SoC IP-block implementing it.
  66. * @gpiodev: the internal state holder, opaque struct
  67. * @parent: optional parent device providing the GPIOs
  68. * @owner: helps prevent removal of modules exporting active GPIOs
  69. * @request: optional hook for chip-specific activation, such as
  70. * enabling module power and clock; may sleep
  71. * @free: optional hook for chip-specific deactivation, such as
  72. * disabling module power and clock; may sleep
  73. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  74. * (same as GPIOF_DIR_XXX), or negative error
  75. * @direction_input: configures signal "offset" as input, or returns error
  76. * @direction_output: configures signal "offset" as output, or returns error
  77. * @get: returns value for signal "offset", 0=low, 1=high, or negative error
  78. * @get_multiple: reads values for multiple signals defined by "mask" and
  79. * stores them in "bits", returns 0 on success or negative error
  80. * @set: assigns output value for signal "offset"
  81. * @set_multiple: assigns output values for multiple signals defined by "mask"
  82. * @set_config: optional hook for all kinds of settings. Uses the same
  83. * packed config format as generic pinconf.
  84. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  85. * implementation may not sleep
  86. * @dbg_show: optional routine to show contents in debugfs; default code
  87. * will be used when this is omitted, but custom code can show extra
  88. * state (such as pullup/pulldown configuration).
  89. * @base: identifies the first GPIO number handled by this chip;
  90. * or, if negative during registration, requests dynamic ID allocation.
  91. * DEPRECATION: providing anything non-negative and nailing the base
  92. * offset of GPIO chips is deprecated. Please pass -1 as base to
  93. * let gpiolib select the chip base in all possible cases. We want to
  94. * get rid of the static GPIO number space in the long run.
  95. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  96. * handled is (base + ngpio - 1).
  97. * @names: if set, must be an array of strings to use as alternative
  98. * names for the GPIOs in this chip. Any entry in the array
  99. * may be NULL if there is no alias for the GPIO, however the
  100. * array must be @ngpio entries long. A name can include a single printk
  101. * format specifier for an unsigned int. It is substituted by the actual
  102. * number of the gpio.
  103. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  104. * must while accessing GPIO expander chips over I2C or SPI. This
  105. * implies that if the chip supports IRQs, these IRQs need to be threaded
  106. * as the chip access may sleep when e.g. reading out the IRQ status
  107. * registers.
  108. * @read_reg: reader function for generic GPIO
  109. * @write_reg: writer function for generic GPIO
  110. * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
  111. * line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
  112. * generic GPIO core. It is for internal housekeeping only.
  113. * @reg_dat: data (in) register for generic GPIO
  114. * @reg_set: output set register (out=high) for generic GPIO
  115. * @reg_clr: output clear register (out=low) for generic GPIO
  116. * @reg_dir: direction setting register for generic GPIO
  117. * @bgpio_bits: number of register bits used for a generic GPIO i.e.
  118. * <register width> * 8
  119. * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
  120. * shadowed and real data registers writes together.
  121. * @bgpio_data: shadowed data register for generic GPIO to clear/set bits
  122. * safely.
  123. * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
  124. * direction safely.
  125. * @irq_handler: the irq handler to use (often a predefined irq core function)
  126. * for GPIO IRQs, provided by GPIO driver
  127. * @irq_default_type: default IRQ triggering type applied during GPIO driver
  128. * initialization, provided by GPIO driver
  129. * @irq_chained_parent: GPIO IRQ chip parent/bank linux irq number,
  130. * provided by GPIO driver for chained interrupt (not for nested
  131. * interrupts).
  132. * @irq_nested: True if set the interrupt handling is nested.
  133. * @irq_need_valid_mask: If set core allocates @irq_valid_mask with all
  134. * bits set to one
  135. * @irq_valid_mask: If not %NULL holds bitmask of GPIOs which are valid to
  136. * be included in IRQ domain of the chip
  137. * @lock_key: per GPIO IRQ chip lockdep class
  138. *
  139. * A gpio_chip can help platforms abstract various sources of GPIOs so
  140. * they can all be accessed through a common programing interface.
  141. * Example sources would be SOC controllers, FPGAs, multifunction
  142. * chips, dedicated GPIO expanders, and so on.
  143. *
  144. * Each chip controls a number of signals, identified in method calls
  145. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  146. * are referenced through calls like gpio_get_value(gpio), the offset
  147. * is calculated by subtracting @base from the gpio number.
  148. */
  149. struct gpio_chip {
  150. const char *label;
  151. struct gpio_device *gpiodev;
  152. struct device *parent;
  153. struct module *owner;
  154. int (*request)(struct gpio_chip *chip,
  155. unsigned offset);
  156. void (*free)(struct gpio_chip *chip,
  157. unsigned offset);
  158. int (*get_direction)(struct gpio_chip *chip,
  159. unsigned offset);
  160. int (*direction_input)(struct gpio_chip *chip,
  161. unsigned offset);
  162. int (*direction_output)(struct gpio_chip *chip,
  163. unsigned offset, int value);
  164. int (*get)(struct gpio_chip *chip,
  165. unsigned offset);
  166. int (*get_multiple)(struct gpio_chip *chip,
  167. unsigned long *mask,
  168. unsigned long *bits);
  169. void (*set)(struct gpio_chip *chip,
  170. unsigned offset, int value);
  171. void (*set_multiple)(struct gpio_chip *chip,
  172. unsigned long *mask,
  173. unsigned long *bits);
  174. int (*set_config)(struct gpio_chip *chip,
  175. unsigned offset,
  176. unsigned long config);
  177. int (*to_irq)(struct gpio_chip *chip,
  178. unsigned offset);
  179. void (*dbg_show)(struct seq_file *s,
  180. struct gpio_chip *chip);
  181. int base;
  182. u16 ngpio;
  183. const char *const *names;
  184. bool can_sleep;
  185. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  186. unsigned long (*read_reg)(void __iomem *reg);
  187. void (*write_reg)(void __iomem *reg, unsigned long data);
  188. bool be_bits;
  189. void __iomem *reg_dat;
  190. void __iomem *reg_set;
  191. void __iomem *reg_clr;
  192. void __iomem *reg_dir;
  193. int bgpio_bits;
  194. spinlock_t bgpio_lock;
  195. unsigned long bgpio_data;
  196. unsigned long bgpio_dir;
  197. #endif
  198. #ifdef CONFIG_GPIOLIB_IRQCHIP
  199. /*
  200. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  201. * to handle IRQs for most practical cases.
  202. */
  203. irq_flow_handler_t irq_handler;
  204. unsigned int irq_default_type;
  205. unsigned int irq_chained_parent;
  206. bool irq_nested;
  207. bool irq_need_valid_mask;
  208. unsigned long *irq_valid_mask;
  209. struct lock_class_key *lock_key;
  210. /**
  211. * @irq:
  212. *
  213. * Integrates interrupt chip functionality with the GPIO chip. Can be
  214. * used to handle IRQs for most practical cases.
  215. */
  216. struct gpio_irq_chip irq;
  217. #endif
  218. #if defined(CONFIG_OF_GPIO)
  219. /*
  220. * If CONFIG_OF is enabled, then all GPIO controllers described in the
  221. * device tree automatically may have an OF translation
  222. */
  223. /**
  224. * @of_node:
  225. *
  226. * Pointer to a device tree node representing this GPIO controller.
  227. */
  228. struct device_node *of_node;
  229. /**
  230. * @of_gpio_n_cells:
  231. *
  232. * Number of cells used to form the GPIO specifier.
  233. */
  234. unsigned int of_gpio_n_cells;
  235. /**
  236. * @of_xlate:
  237. *
  238. * Callback to translate a device tree GPIO specifier into a chip-
  239. * relative GPIO number and flags.
  240. */
  241. int (*of_xlate)(struct gpio_chip *gc,
  242. const struct of_phandle_args *gpiospec, u32 *flags);
  243. #endif
  244. };
  245. extern const char *gpiochip_is_requested(struct gpio_chip *chip,
  246. unsigned offset);
  247. /* add/remove chips */
  248. extern int gpiochip_add_data(struct gpio_chip *chip, void *data);
  249. static inline int gpiochip_add(struct gpio_chip *chip)
  250. {
  251. return gpiochip_add_data(chip, NULL);
  252. }
  253. extern void gpiochip_remove(struct gpio_chip *chip);
  254. extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  255. void *data);
  256. extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
  257. extern struct gpio_chip *gpiochip_find(void *data,
  258. int (*match)(struct gpio_chip *chip, void *data));
  259. /* lock/unlock as IRQ */
  260. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
  261. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
  262. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
  263. /* Line status inquiry for drivers */
  264. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);
  265. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset);
  266. /* Sleep persistence inquiry for drivers */
  267. bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset);
  268. /* get driver data */
  269. void *gpiochip_get_data(struct gpio_chip *chip);
  270. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
  271. struct bgpio_pdata {
  272. const char *label;
  273. int base;
  274. int ngpio;
  275. };
  276. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  277. int bgpio_init(struct gpio_chip *gc, struct device *dev,
  278. unsigned long sz, void __iomem *dat, void __iomem *set,
  279. void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
  280. unsigned long flags);
  281. #define BGPIOF_BIG_ENDIAN BIT(0)
  282. #define BGPIOF_UNREADABLE_REG_SET BIT(1) /* reg_set is unreadable */
  283. #define BGPIOF_UNREADABLE_REG_DIR BIT(2) /* reg_dir is unreadable */
  284. #define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
  285. #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */
  286. #define BGPIOF_NO_OUTPUT BIT(5) /* only input */
  287. #endif
  288. #ifdef CONFIG_GPIOLIB_IRQCHIP
  289. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  290. struct irq_chip *irqchip,
  291. unsigned int parent_irq,
  292. irq_flow_handler_t parent_handler);
  293. void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
  294. struct irq_chip *irqchip,
  295. unsigned int parent_irq);
  296. int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
  297. struct irq_chip *irqchip,
  298. unsigned int first_irq,
  299. irq_flow_handler_t handler,
  300. unsigned int type,
  301. bool nested,
  302. struct lock_class_key *lock_key);
  303. #ifdef CONFIG_LOCKDEP
  304. /*
  305. * Lockdep requires that each irqchip instance be created with a
  306. * unique key so as to avoid unnecessary warnings. This upfront
  307. * boilerplate static inlines provides such a key for each
  308. * unique instance.
  309. */
  310. static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  311. struct irq_chip *irqchip,
  312. unsigned int first_irq,
  313. irq_flow_handler_t handler,
  314. unsigned int type)
  315. {
  316. static struct lock_class_key key;
  317. return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
  318. handler, type, false, &key);
  319. }
  320. static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
  321. struct irq_chip *irqchip,
  322. unsigned int first_irq,
  323. irq_flow_handler_t handler,
  324. unsigned int type)
  325. {
  326. static struct lock_class_key key;
  327. return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
  328. handler, type, true, &key);
  329. }
  330. #else
  331. static inline int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  332. struct irq_chip *irqchip,
  333. unsigned int first_irq,
  334. irq_flow_handler_t handler,
  335. unsigned int type)
  336. {
  337. return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
  338. handler, type, false, NULL);
  339. }
  340. static inline int gpiochip_irqchip_add_nested(struct gpio_chip *gpiochip,
  341. struct irq_chip *irqchip,
  342. unsigned int first_irq,
  343. irq_flow_handler_t handler,
  344. unsigned int type)
  345. {
  346. return gpiochip_irqchip_add_key(gpiochip, irqchip, first_irq,
  347. handler, type, true, NULL);
  348. }
  349. #endif /* CONFIG_LOCKDEP */
  350. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  351. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
  352. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
  353. int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
  354. unsigned long config);
  355. #ifdef CONFIG_PINCTRL
  356. /**
  357. * struct gpio_pin_range - pin range controlled by a gpio chip
  358. * @node: list for maintaining set of pin ranges, used internally
  359. * @pctldev: pinctrl device which handles corresponding pins
  360. * @range: actual range of pins controlled by a gpio controller
  361. */
  362. struct gpio_pin_range {
  363. struct list_head node;
  364. struct pinctrl_dev *pctldev;
  365. struct pinctrl_gpio_range range;
  366. };
  367. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  368. unsigned int gpio_offset, unsigned int pin_offset,
  369. unsigned int npins);
  370. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  371. struct pinctrl_dev *pctldev,
  372. unsigned int gpio_offset, const char *pin_group);
  373. void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
  374. #else
  375. static inline int
  376. gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  377. unsigned int gpio_offset, unsigned int pin_offset,
  378. unsigned int npins)
  379. {
  380. return 0;
  381. }
  382. static inline int
  383. gpiochip_add_pingroup_range(struct gpio_chip *chip,
  384. struct pinctrl_dev *pctldev,
  385. unsigned int gpio_offset, const char *pin_group)
  386. {
  387. return 0;
  388. }
  389. static inline void
  390. gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  391. {
  392. }
  393. #endif /* CONFIG_PINCTRL */
  394. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  395. const char *label);
  396. void gpiochip_free_own_desc(struct gpio_desc *desc);
  397. #else /* CONFIG_GPIOLIB */
  398. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  399. {
  400. /* GPIO can never have been requested */
  401. WARN_ON(1);
  402. return ERR_PTR(-ENODEV);
  403. }
  404. #endif /* CONFIG_GPIOLIB */
  405. #endif