driver.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * @irq_not_threaded: flag must be set if @can_sleep is set but the
  81. * IRQs don't need to be threaded
  82. * @read_reg: reader function for generic GPIO
  83. * @write_reg: writer function for generic GPIO
  84. * @pin2mask: some generic GPIO controllers work with the big-endian bits
  85. * notation, e.g. in a 8-bits register, GPIO7 is the least significant
  86. * bit. This callback assigns the right bit mask.
  87. * @reg_dat: data (in) register for generic GPIO
  88. * @reg_set: output set register (out=high) for generic GPIO
  89. * @reg_clk: output clear register (out=low) for generic GPIO
  90. * @reg_dir: direction setting register for generic GPIO
  91. * @bgpio_bits: number of register bits used for a generic GPIO i.e.
  92. * <register width> * 8
  93. * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
  94. * shadowed and real data registers writes together.
  95. * @bgpio_data: shadowed data register for generic GPIO to clear/set bits
  96. * safely.
  97. * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
  98. * direction safely.
  99. * @irqchip: GPIO IRQ chip impl, provided by GPIO driver
  100. * @irqdomain: Interrupt translation domain; responsible for mapping
  101. * between GPIO hwirq number and linux irq number
  102. * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated)
  103. * @irq_handler: the irq handler to use (often a predefined irq core function)
  104. * for GPIO IRQs, provided by GPIO driver
  105. * @irq_default_type: default IRQ triggering type applied during GPIO driver
  106. * initialization, provided by GPIO driver
  107. * @irq_parent: GPIO IRQ chip parent/bank linux irq number,
  108. * provided by GPIO driver
  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. bool irq_not_threaded;
  162. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  163. unsigned long (*read_reg)(void __iomem *reg);
  164. void (*write_reg)(void __iomem *reg, unsigned long data);
  165. unsigned long (*pin2mask)(struct gpio_chip *gc, unsigned int pin);
  166. void __iomem *reg_dat;
  167. void __iomem *reg_set;
  168. void __iomem *reg_clr;
  169. void __iomem *reg_dir;
  170. int bgpio_bits;
  171. spinlock_t bgpio_lock;
  172. unsigned long bgpio_data;
  173. unsigned long bgpio_dir;
  174. #endif
  175. #ifdef CONFIG_GPIOLIB_IRQCHIP
  176. /*
  177. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  178. * to handle IRQs for most practical cases.
  179. */
  180. struct irq_chip *irqchip;
  181. struct irq_domain *irqdomain;
  182. unsigned int irq_base;
  183. irq_flow_handler_t irq_handler;
  184. unsigned int irq_default_type;
  185. int irq_parent;
  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. int _gpiochip_irqchip_add(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. struct lock_class_key *lock_key);
  253. #ifdef CONFIG_LOCKDEP
  254. #define gpiochip_irqchip_add(...) \
  255. ( \
  256. ({ \
  257. static struct lock_class_key _key; \
  258. _gpiochip_irqchip_add(__VA_ARGS__, &_key); \
  259. }) \
  260. )
  261. #else
  262. #define gpiochip_irqchip_add(...) \
  263. _gpiochip_irqchip_add(__VA_ARGS__, NULL)
  264. #endif
  265. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  266. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
  267. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
  268. #ifdef CONFIG_PINCTRL
  269. /**
  270. * struct gpio_pin_range - pin range controlled by a gpio chip
  271. * @head: list for maintaining set of pin ranges, used internally
  272. * @pctldev: pinctrl device which handles corresponding pins
  273. * @range: actual range of pins controlled by a gpio controller
  274. */
  275. struct gpio_pin_range {
  276. struct list_head node;
  277. struct pinctrl_dev *pctldev;
  278. struct pinctrl_gpio_range range;
  279. };
  280. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  281. unsigned int gpio_offset, unsigned int pin_offset,
  282. unsigned int npins);
  283. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  284. struct pinctrl_dev *pctldev,
  285. unsigned int gpio_offset, const char *pin_group);
  286. void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
  287. #else
  288. static inline int
  289. gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  290. unsigned int gpio_offset, unsigned int pin_offset,
  291. unsigned int npins)
  292. {
  293. return 0;
  294. }
  295. static inline int
  296. gpiochip_add_pingroup_range(struct gpio_chip *chip,
  297. struct pinctrl_dev *pctldev,
  298. unsigned int gpio_offset, const char *pin_group)
  299. {
  300. return 0;
  301. }
  302. static inline void
  303. gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  304. {
  305. }
  306. #endif /* CONFIG_PINCTRL */
  307. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  308. const char *label);
  309. void gpiochip_free_own_desc(struct gpio_desc *desc);
  310. #else /* CONFIG_GPIOLIB */
  311. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  312. {
  313. /* GPIO can never have been requested */
  314. WARN_ON(1);
  315. return ERR_PTR(-ENODEV);
  316. }
  317. #endif /* CONFIG_GPIOLIB */
  318. #endif