driver.h 15 KB

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