driver.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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/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. #include <linux/property.h>
  12. #include <linux/types.h>
  13. #include <linux/android_kabi.h>
  14. #include <asm/msi.h>
  15. struct gpio_desc;
  16. struct of_phandle_args;
  17. struct device_node;
  18. struct seq_file;
  19. struct gpio_device;
  20. struct module;
  21. enum gpiod_flags;
  22. enum gpio_lookup_flags;
  23. struct gpio_chip;
  24. union gpio_irq_fwspec {
  25. struct irq_fwspec fwspec;
  26. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  27. msi_alloc_info_t msiinfo;
  28. #endif
  29. };
  30. #define GPIO_LINE_DIRECTION_IN 1
  31. #define GPIO_LINE_DIRECTION_OUT 0
  32. /**
  33. * struct gpio_irq_chip - GPIO interrupt controller
  34. */
  35. struct gpio_irq_chip {
  36. /**
  37. * @chip:
  38. *
  39. * GPIO IRQ chip implementation, provided by GPIO driver.
  40. */
  41. struct irq_chip *chip;
  42. /**
  43. * @domain:
  44. *
  45. * Interrupt translation domain; responsible for mapping between GPIO
  46. * hwirq number and Linux IRQ number.
  47. */
  48. struct irq_domain *domain;
  49. /**
  50. * @domain_ops:
  51. *
  52. * Table of interrupt domain operations for this IRQ chip.
  53. */
  54. const struct irq_domain_ops *domain_ops;
  55. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  56. /**
  57. * @fwnode:
  58. *
  59. * Firmware node corresponding to this gpiochip/irqchip, necessary
  60. * for hierarchical irqdomain support.
  61. */
  62. struct fwnode_handle *fwnode;
  63. /**
  64. * @parent_domain:
  65. *
  66. * If non-NULL, will be set as the parent of this GPIO interrupt
  67. * controller's IRQ domain to establish a hierarchical interrupt
  68. * domain. The presence of this will activate the hierarchical
  69. * interrupt support.
  70. */
  71. struct irq_domain *parent_domain;
  72. /**
  73. * @child_to_parent_hwirq:
  74. *
  75. * This callback translates a child hardware IRQ offset to a parent
  76. * hardware IRQ offset on a hierarchical interrupt chip. The child
  77. * hardware IRQs correspond to the GPIO index 0..ngpio-1 (see the
  78. * ngpio field of struct gpio_chip) and the corresponding parent
  79. * hardware IRQ and type (such as IRQ_TYPE_*) shall be returned by
  80. * the driver. The driver can calculate this from an offset or using
  81. * a lookup table or whatever method is best for this chip. Return
  82. * 0 on successful translation in the driver.
  83. *
  84. * If some ranges of hardware IRQs do not have a corresponding parent
  85. * HWIRQ, return -EINVAL, but also make sure to fill in @valid_mask and
  86. * @need_valid_mask to make these GPIO lines unavailable for
  87. * translation.
  88. */
  89. int (*child_to_parent_hwirq)(struct gpio_chip *gc,
  90. unsigned int child_hwirq,
  91. unsigned int child_type,
  92. unsigned int *parent_hwirq,
  93. unsigned int *parent_type);
  94. /**
  95. * @populate_parent_alloc_arg :
  96. *
  97. * This optional callback allocates and populates the specific struct
  98. * for the parent's IRQ domain. If this is not specified, then
  99. * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell
  100. * variant named &gpiochip_populate_parent_fwspec_fourcell is also
  101. * available.
  102. */
  103. int (*populate_parent_alloc_arg)(struct gpio_chip *gc,
  104. union gpio_irq_fwspec *fwspec,
  105. unsigned int parent_hwirq,
  106. unsigned int parent_type);
  107. /**
  108. * @child_offset_to_irq:
  109. *
  110. * This optional callback is used to translate the child's GPIO line
  111. * offset on the GPIO chip to an IRQ number for the GPIO to_irq()
  112. * callback. If this is not specified, then a default callback will be
  113. * provided that returns the line offset.
  114. */
  115. unsigned int (*child_offset_to_irq)(struct gpio_chip *gc,
  116. unsigned int pin);
  117. /**
  118. * @child_irq_domain_ops:
  119. *
  120. * The IRQ domain operations that will be used for this GPIO IRQ
  121. * chip. If no operations are provided, then default callbacks will
  122. * be populated to setup the IRQ hierarchy. Some drivers need to
  123. * supply their own translate function.
  124. */
  125. struct irq_domain_ops child_irq_domain_ops;
  126. #endif
  127. /**
  128. * @handler:
  129. *
  130. * The IRQ handler to use (often a predefined IRQ core function) for
  131. * GPIO IRQs, provided by GPIO driver.
  132. */
  133. irq_flow_handler_t handler;
  134. /**
  135. * @default_type:
  136. *
  137. * Default IRQ triggering type applied during GPIO driver
  138. * initialization, provided by GPIO driver.
  139. */
  140. unsigned int default_type;
  141. /**
  142. * @lock_key:
  143. *
  144. * Per GPIO IRQ chip lockdep class for IRQ lock.
  145. */
  146. struct lock_class_key *lock_key;
  147. /**
  148. * @request_key:
  149. *
  150. * Per GPIO IRQ chip lockdep class for IRQ request.
  151. */
  152. struct lock_class_key *request_key;
  153. /**
  154. * @parent_handler:
  155. *
  156. * The interrupt handler for the GPIO chip's parent interrupts, may be
  157. * NULL if the parent interrupts are nested rather than cascaded.
  158. */
  159. irq_flow_handler_t parent_handler;
  160. union {
  161. /**
  162. * @parent_handler_data:
  163. *
  164. * If @per_parent_data is false, @parent_handler_data is a
  165. * single pointer used as the data associated with every
  166. * parent interrupt.
  167. */
  168. void *parent_handler_data;
  169. /**
  170. * @parent_handler_data_array:
  171. *
  172. * If @per_parent_data is true, @parent_handler_data_array is
  173. * an array of @num_parents pointers, and is used to associate
  174. * different data for each parent. This cannot be NULL if
  175. * @per_parent_data is true.
  176. */
  177. void **parent_handler_data_array;
  178. };
  179. /**
  180. * @num_parents:
  181. *
  182. * The number of interrupt parents of a GPIO chip.
  183. */
  184. unsigned int num_parents;
  185. /**
  186. * @parents:
  187. *
  188. * A list of interrupt parents of a GPIO chip. This is owned by the
  189. * driver, so the core will only reference this list, not modify it.
  190. */
  191. unsigned int *parents;
  192. /**
  193. * @map:
  194. *
  195. * A list of interrupt parents for each line of a GPIO chip.
  196. */
  197. unsigned int *map;
  198. /**
  199. * @threaded:
  200. *
  201. * True if set the interrupt handling uses nested threads.
  202. */
  203. bool threaded;
  204. /**
  205. * @per_parent_data:
  206. *
  207. * True if parent_handler_data_array describes a @num_parents
  208. * sized array to be used as parent data.
  209. */
  210. bool per_parent_data;
  211. /**
  212. * @initialized:
  213. *
  214. * Flag to track GPIO chip irq member's initialization.
  215. * This flag will make sure GPIO chip irq members are not used
  216. * before they are initialized.
  217. */
  218. bool initialized;
  219. /**
  220. * @init_hw: optional routine to initialize hardware before
  221. * an IRQ chip will be added. This is quite useful when
  222. * a particular driver wants to clear IRQ related registers
  223. * in order to avoid undesired events.
  224. */
  225. int (*init_hw)(struct gpio_chip *gc);
  226. /**
  227. * @init_valid_mask: optional routine to initialize @valid_mask, to be
  228. * used if not all GPIO lines are valid interrupts. Sometimes some
  229. * lines just cannot fire interrupts, and this routine, when defined,
  230. * is passed a bitmap in "valid_mask" and it will have ngpios
  231. * bits from 0..(ngpios-1) set to "1" as in valid. The callback can
  232. * then directly set some bits to "0" if they cannot be used for
  233. * interrupts.
  234. */
  235. void (*init_valid_mask)(struct gpio_chip *gc,
  236. unsigned long *valid_mask,
  237. unsigned int ngpios);
  238. /**
  239. * @valid_mask:
  240. *
  241. * If not %NULL, holds bitmask of GPIOs which are valid to be included
  242. * in IRQ domain of the chip.
  243. */
  244. unsigned long *valid_mask;
  245. /**
  246. * @first:
  247. *
  248. * Required for static IRQ allocation. If set, irq_domain_add_simple()
  249. * will allocate and map all IRQs during initialization.
  250. */
  251. unsigned int first;
  252. /**
  253. * @irq_enable:
  254. *
  255. * Store old irq_chip irq_enable callback
  256. */
  257. void (*irq_enable)(struct irq_data *data);
  258. /**
  259. * @irq_disable:
  260. *
  261. * Store old irq_chip irq_disable callback
  262. */
  263. void (*irq_disable)(struct irq_data *data);
  264. /**
  265. * @irq_unmask:
  266. *
  267. * Store old irq_chip irq_unmask callback
  268. */
  269. void (*irq_unmask)(struct irq_data *data);
  270. /**
  271. * @irq_mask:
  272. *
  273. * Store old irq_chip irq_mask callback
  274. */
  275. void (*irq_mask)(struct irq_data *data);
  276. ANDROID_KABI_RESERVE(1);
  277. ANDROID_KABI_RESERVE(2);
  278. };
  279. /**
  280. * struct gpio_chip - abstract a GPIO controller
  281. * @label: a functional name for the GPIO device, such as a part
  282. * number or the name of the SoC IP-block implementing it.
  283. * @gpiodev: the internal state holder, opaque struct
  284. * @parent: optional parent device providing the GPIOs
  285. * @fwnode: optional fwnode providing this controller's properties
  286. * @owner: helps prevent removal of modules exporting active GPIOs
  287. * @request: optional hook for chip-specific activation, such as
  288. * enabling module power and clock; may sleep
  289. * @free: optional hook for chip-specific deactivation, such as
  290. * disabling module power and clock; may sleep
  291. * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  292. * (same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN),
  293. * or negative error. It is recommended to always implement this
  294. * function, even on input-only or output-only gpio chips.
  295. * @direction_input: configures signal "offset" as input, or returns error
  296. * This can be omitted on input-only or output-only gpio chips.
  297. * @direction_output: configures signal "offset" as output, or returns error
  298. * This can be omitted on input-only or output-only gpio chips.
  299. * @get: returns value for signal "offset", 0=low, 1=high, or negative error
  300. * @get_multiple: reads values for multiple signals defined by "mask" and
  301. * stores them in "bits", returns 0 on success or negative error
  302. * @set: assigns output value for signal "offset"
  303. * @set_multiple: assigns output values for multiple signals defined by "mask"
  304. * @set_config: optional hook for all kinds of settings. Uses the same
  305. * packed config format as generic pinconf.
  306. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  307. * implementation may not sleep
  308. * @dbg_show: optional routine to show contents in debugfs; default code
  309. * will be used when this is omitted, but custom code can show extra
  310. * state (such as pullup/pulldown configuration).
  311. * @init_valid_mask: optional routine to initialize @valid_mask, to be used if
  312. * not all GPIOs are valid.
  313. * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
  314. * requires special mapping of the pins that provides GPIO functionality.
  315. * It is called after adding GPIO chip and before adding IRQ chip.
  316. * @en_hw_timestamp: Dependent on GPIO chip, an optional routine to
  317. * enable hardware timestamp.
  318. * @dis_hw_timestamp: Dependent on GPIO chip, an optional routine to
  319. * disable hardware timestamp.
  320. * @base: identifies the first GPIO number handled by this chip;
  321. * or, if negative during registration, requests dynamic ID allocation.
  322. * DEPRECATION: providing anything non-negative and nailing the base
  323. * offset of GPIO chips is deprecated. Please pass -1 as base to
  324. * let gpiolib select the chip base in all possible cases. We want to
  325. * get rid of the static GPIO number space in the long run.
  326. * @ngpio: the number of GPIOs handled by this controller; the last GPIO
  327. * handled is (base + ngpio - 1).
  328. * @offset: when multiple gpio chips belong to the same device this
  329. * can be used as offset within the device so friendly names can
  330. * be properly assigned.
  331. * @names: if set, must be an array of strings to use as alternative
  332. * names for the GPIOs in this chip. Any entry in the array
  333. * may be NULL if there is no alias for the GPIO, however the
  334. * array must be @ngpio entries long. A name can include a single printk
  335. * format specifier for an unsigned int. It is substituted by the actual
  336. * number of the gpio.
  337. * @can_sleep: flag must be set iff get()/set() methods sleep, as they
  338. * must while accessing GPIO expander chips over I2C or SPI. This
  339. * implies that if the chip supports IRQs, these IRQs need to be threaded
  340. * as the chip access may sleep when e.g. reading out the IRQ status
  341. * registers.
  342. * @read_reg: reader function for generic GPIO
  343. * @write_reg: writer function for generic GPIO
  344. * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
  345. * line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
  346. * generic GPIO core. It is for internal housekeeping only.
  347. * @reg_dat: data (in) register for generic GPIO
  348. * @reg_set: output set register (out=high) for generic GPIO
  349. * @reg_clr: output clear register (out=low) for generic GPIO
  350. * @reg_dir_out: direction out setting register for generic GPIO
  351. * @reg_dir_in: direction in setting register for generic GPIO
  352. * @bgpio_dir_unreadable: indicates that the direction register(s) cannot
  353. * be read and we need to rely on out internal state tracking.
  354. * @bgpio_bits: number of register bits used for a generic GPIO i.e.
  355. * <register width> * 8
  356. * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
  357. * shadowed and real data registers writes together.
  358. * @bgpio_data: shadowed data register for generic GPIO to clear/set bits
  359. * safely.
  360. * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
  361. * direction safely. A "1" in this word means the line is set as
  362. * output.
  363. *
  364. * A gpio_chip can help platforms abstract various sources of GPIOs so
  365. * they can all be accessed through a common programming interface.
  366. * Example sources would be SOC controllers, FPGAs, multifunction
  367. * chips, dedicated GPIO expanders, and so on.
  368. *
  369. * Each chip controls a number of signals, identified in method calls
  370. * by "offset" values in the range 0..(@ngpio - 1). When those signals
  371. * are referenced through calls like gpio_get_value(gpio), the offset
  372. * is calculated by subtracting @base from the gpio number.
  373. */
  374. struct gpio_chip {
  375. const char *label;
  376. struct gpio_device *gpiodev;
  377. struct device *parent;
  378. struct fwnode_handle *fwnode;
  379. struct module *owner;
  380. int (*request)(struct gpio_chip *gc,
  381. unsigned int offset);
  382. void (*free)(struct gpio_chip *gc,
  383. unsigned int offset);
  384. int (*get_direction)(struct gpio_chip *gc,
  385. unsigned int offset);
  386. int (*direction_input)(struct gpio_chip *gc,
  387. unsigned int offset);
  388. int (*direction_output)(struct gpio_chip *gc,
  389. unsigned int offset, int value);
  390. int (*get)(struct gpio_chip *gc,
  391. unsigned int offset);
  392. int (*get_multiple)(struct gpio_chip *gc,
  393. unsigned long *mask,
  394. unsigned long *bits);
  395. void (*set)(struct gpio_chip *gc,
  396. unsigned int offset, int value);
  397. void (*set_multiple)(struct gpio_chip *gc,
  398. unsigned long *mask,
  399. unsigned long *bits);
  400. int (*set_config)(struct gpio_chip *gc,
  401. unsigned int offset,
  402. unsigned long config);
  403. int (*to_irq)(struct gpio_chip *gc,
  404. unsigned int offset);
  405. void (*dbg_show)(struct seq_file *s,
  406. struct gpio_chip *gc);
  407. int (*init_valid_mask)(struct gpio_chip *gc,
  408. unsigned long *valid_mask,
  409. unsigned int ngpios);
  410. int (*add_pin_ranges)(struct gpio_chip *gc);
  411. int (*en_hw_timestamp)(struct gpio_chip *gc,
  412. u32 offset,
  413. unsigned long flags);
  414. int (*dis_hw_timestamp)(struct gpio_chip *gc,
  415. u32 offset,
  416. unsigned long flags);
  417. int base;
  418. u16 ngpio;
  419. u16 offset;
  420. const char *const *names;
  421. bool can_sleep;
  422. #if IS_ENABLED(CONFIG_GPIO_GENERIC)
  423. unsigned long (*read_reg)(void __iomem *reg);
  424. void (*write_reg)(void __iomem *reg, unsigned long data);
  425. bool be_bits;
  426. void __iomem *reg_dat;
  427. void __iomem *reg_set;
  428. void __iomem *reg_clr;
  429. void __iomem *reg_dir_out;
  430. void __iomem *reg_dir_in;
  431. bool bgpio_dir_unreadable;
  432. int bgpio_bits;
  433. raw_spinlock_t bgpio_lock;
  434. unsigned long bgpio_data;
  435. unsigned long bgpio_dir;
  436. #endif /* CONFIG_GPIO_GENERIC */
  437. #ifdef CONFIG_GPIOLIB_IRQCHIP
  438. /*
  439. * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
  440. * to handle IRQs for most practical cases.
  441. */
  442. /**
  443. * @irq:
  444. *
  445. * Integrates interrupt chip functionality with the GPIO chip. Can be
  446. * used to handle IRQs for most practical cases.
  447. */
  448. struct gpio_irq_chip irq;
  449. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  450. /**
  451. * @valid_mask:
  452. *
  453. * If not %NULL, holds bitmask of GPIOs which are valid to be used
  454. * from the chip.
  455. */
  456. unsigned long *valid_mask;
  457. #if defined(CONFIG_OF_GPIO)
  458. /*
  459. * If CONFIG_OF_GPIO is enabled, then all GPIO controllers described in
  460. * the device tree automatically may have an OF translation
  461. */
  462. /**
  463. * @of_node:
  464. *
  465. * Pointer to a device tree node representing this GPIO controller.
  466. */
  467. struct device_node *of_node;
  468. /**
  469. * @of_gpio_n_cells:
  470. *
  471. * Number of cells used to form the GPIO specifier.
  472. */
  473. unsigned int of_gpio_n_cells;
  474. /**
  475. * @of_xlate:
  476. *
  477. * Callback to translate a device tree GPIO specifier into a chip-
  478. * relative GPIO number and flags.
  479. */
  480. int (*of_xlate)(struct gpio_chip *gc,
  481. const struct of_phandle_args *gpiospec, u32 *flags);
  482. /**
  483. * @of_gpio_ranges_fallback:
  484. *
  485. * Optional hook for the case that no gpio-ranges property is defined
  486. * within the device tree node "np" (usually DT before introduction
  487. * of gpio-ranges). So this callback is helpful to provide the
  488. * necessary backward compatibility for the pin ranges.
  489. */
  490. int (*of_gpio_ranges_fallback)(struct gpio_chip *gc,
  491. struct device_node *np);
  492. #endif /* CONFIG_OF_GPIO */
  493. ANDROID_KABI_RESERVE(1);
  494. ANDROID_KABI_RESERVE(2);
  495. };
  496. extern const char *gpiochip_is_requested(struct gpio_chip *gc,
  497. unsigned int offset);
  498. /**
  499. * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
  500. * @chip: the chip to query
  501. * @i: loop variable
  502. * @base: first GPIO in the range
  503. * @size: amount of GPIOs to check starting from @base
  504. * @label: label of current GPIO
  505. */
  506. #define for_each_requested_gpio_in_range(chip, i, base, size, label) \
  507. for (i = 0; i < size; i++) \
  508. if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
  509. /* Iterates over all requested GPIO of the given @chip */
  510. #define for_each_requested_gpio(chip, i, label) \
  511. for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label)
  512. /* add/remove chips */
  513. extern int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
  514. struct lock_class_key *lock_key,
  515. struct lock_class_key *request_key);
  516. /**
  517. * gpiochip_add_data() - register a gpio_chip
  518. * @gc: the chip to register, with gc->base initialized
  519. * @data: driver-private data associated with this chip
  520. *
  521. * Context: potentially before irqs will work
  522. *
  523. * When gpiochip_add_data() is called very early during boot, so that GPIOs
  524. * can be freely used, the gc->parent device must be registered before
  525. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  526. * for GPIOs will fail rudely.
  527. *
  528. * gpiochip_add_data() must only be called after gpiolib initialization,
  529. * i.e. after core_initcall().
  530. *
  531. * If gc->base is negative, this requests dynamic assignment of
  532. * a range of valid GPIOs.
  533. *
  534. * Returns:
  535. * A negative errno if the chip can't be registered, such as because the
  536. * gc->base is invalid or already associated with a different chip.
  537. * Otherwise it returns zero as a success code.
  538. */
  539. #ifdef CONFIG_LOCKDEP
  540. #define gpiochip_add_data(gc, data) ({ \
  541. static struct lock_class_key lock_key; \
  542. static struct lock_class_key request_key; \
  543. gpiochip_add_data_with_key(gc, data, &lock_key, \
  544. &request_key); \
  545. })
  546. #define devm_gpiochip_add_data(dev, gc, data) ({ \
  547. static struct lock_class_key lock_key; \
  548. static struct lock_class_key request_key; \
  549. devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \
  550. &request_key); \
  551. })
  552. #else
  553. #define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL)
  554. #define devm_gpiochip_add_data(dev, gc, data) \
  555. devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
  556. #endif /* CONFIG_LOCKDEP */
  557. static inline int gpiochip_add(struct gpio_chip *gc)
  558. {
  559. return gpiochip_add_data(gc, NULL);
  560. }
  561. extern void gpiochip_remove(struct gpio_chip *gc);
  562. extern int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
  563. struct lock_class_key *lock_key,
  564. struct lock_class_key *request_key);
  565. extern struct gpio_chip *gpiochip_find(void *data,
  566. int (*match)(struct gpio_chip *gc, void *data));
  567. bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
  568. int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
  569. void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
  570. void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset);
  571. void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset);
  572. /* irq_data versions of the above */
  573. int gpiochip_irq_reqres(struct irq_data *data);
  574. void gpiochip_irq_relres(struct irq_data *data);
  575. /* Paste this in your irq_chip structure */
  576. #define GPIOCHIP_IRQ_RESOURCE_HELPERS \
  577. .irq_request_resources = gpiochip_irq_reqres, \
  578. .irq_release_resources = gpiochip_irq_relres
  579. static inline void gpio_irq_chip_set_chip(struct gpio_irq_chip *girq,
  580. const struct irq_chip *chip)
  581. {
  582. /* Yes, dropping const is ugly, but it isn't like we have a choice */
  583. girq->chip = (struct irq_chip *)chip;
  584. }
  585. /* Line status inquiry for drivers */
  586. bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset);
  587. bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset);
  588. /* Sleep persistence inquiry for drivers */
  589. bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset);
  590. bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset);
  591. /* get driver data */
  592. void *gpiochip_get_data(struct gpio_chip *gc);
  593. struct bgpio_pdata {
  594. const char *label;
  595. int base;
  596. int ngpio;
  597. };
  598. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  599. int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
  600. union gpio_irq_fwspec *gfwspec,
  601. unsigned int parent_hwirq,
  602. unsigned int parent_type);
  603. int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
  604. union gpio_irq_fwspec *gfwspec,
  605. unsigned int parent_hwirq,
  606. unsigned int parent_type);
  607. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  608. int bgpio_init(struct gpio_chip *gc, struct device *dev,
  609. unsigned long sz, void __iomem *dat, void __iomem *set,
  610. void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
  611. unsigned long flags);
  612. #define BGPIOF_BIG_ENDIAN BIT(0)
  613. #define BGPIOF_UNREADABLE_REG_SET BIT(1) /* reg_set is unreadable */
  614. #define BGPIOF_UNREADABLE_REG_DIR BIT(2) /* reg_dir is unreadable */
  615. #define BGPIOF_BIG_ENDIAN_BYTE_ORDER BIT(3)
  616. #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */
  617. #define BGPIOF_NO_OUTPUT BIT(5) /* only input */
  618. #define BGPIOF_NO_SET_ON_INPUT BIT(6)
  619. int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  620. irq_hw_number_t hwirq);
  621. void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
  622. int gpiochip_irq_domain_activate(struct irq_domain *domain,
  623. struct irq_data *data, bool reserve);
  624. void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
  625. struct irq_data *data);
  626. bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
  627. unsigned int offset);
  628. #ifdef CONFIG_GPIOLIB_IRQCHIP
  629. int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
  630. struct irq_domain *domain);
  631. #else
  632. static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
  633. struct irq_domain *domain)
  634. {
  635. WARN_ON(1);
  636. return -EINVAL;
  637. }
  638. #endif
  639. int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
  640. void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
  641. int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
  642. unsigned long config);
  643. /**
  644. * struct gpio_pin_range - pin range controlled by a gpio chip
  645. * @node: list for maintaining set of pin ranges, used internally
  646. * @pctldev: pinctrl device which handles corresponding pins
  647. * @range: actual range of pins controlled by a gpio controller
  648. */
  649. struct gpio_pin_range {
  650. struct list_head node;
  651. struct pinctrl_dev *pctldev;
  652. struct pinctrl_gpio_range range;
  653. };
  654. #ifdef CONFIG_PINCTRL
  655. int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
  656. unsigned int gpio_offset, unsigned int pin_offset,
  657. unsigned int npins);
  658. int gpiochip_add_pingroup_range(struct gpio_chip *gc,
  659. struct pinctrl_dev *pctldev,
  660. unsigned int gpio_offset, const char *pin_group);
  661. void gpiochip_remove_pin_ranges(struct gpio_chip *gc);
  662. #else /* ! CONFIG_PINCTRL */
  663. static inline int
  664. gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
  665. unsigned int gpio_offset, unsigned int pin_offset,
  666. unsigned int npins)
  667. {
  668. return 0;
  669. }
  670. static inline int
  671. gpiochip_add_pingroup_range(struct gpio_chip *gc,
  672. struct pinctrl_dev *pctldev,
  673. unsigned int gpio_offset, const char *pin_group)
  674. {
  675. return 0;
  676. }
  677. static inline void
  678. gpiochip_remove_pin_ranges(struct gpio_chip *gc)
  679. {
  680. }
  681. #endif /* CONFIG_PINCTRL */
  682. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
  683. unsigned int hwnum,
  684. const char *label,
  685. enum gpio_lookup_flags lflags,
  686. enum gpiod_flags dflags);
  687. void gpiochip_free_own_desc(struct gpio_desc *desc);
  688. #ifdef CONFIG_GPIOLIB
  689. /* lock/unlock as IRQ */
  690. int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
  691. void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
  692. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
  693. #else /* CONFIG_GPIOLIB */
  694. static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  695. {
  696. /* GPIO can never have been requested */
  697. WARN_ON(1);
  698. return ERR_PTR(-ENODEV);
  699. }
  700. static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
  701. unsigned int offset)
  702. {
  703. WARN_ON(1);
  704. return -EINVAL;
  705. }
  706. static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,
  707. unsigned int offset)
  708. {
  709. WARN_ON(1);
  710. }
  711. #endif /* CONFIG_GPIOLIB */
  712. #define for_each_gpiochip_node(dev, child) \
  713. device_for_each_child_node(dev, child) \
  714. if (!fwnode_property_present(child, "gpio-controller")) {} else
  715. static inline unsigned int gpiochip_node_count(struct device *dev)
  716. {
  717. struct fwnode_handle *child;
  718. unsigned int count = 0;
  719. for_each_gpiochip_node(dev, child)
  720. count++;
  721. return count;
  722. }
  723. static inline struct fwnode_handle *gpiochip_node_get_first(struct device *dev)
  724. {
  725. struct fwnode_handle *fwnode;
  726. for_each_gpiochip_node(dev, fwnode)
  727. return fwnode;
  728. return NULL;
  729. }
  730. #endif /* __LINUX_GPIO_DRIVER_H */