driver.h 17 KB

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