driver.h 18 KB

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