driver.h 13 KB

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