driver.h 14 KB

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