v4l2-async.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * V4L2 asynchronous subdevice registration API
  4. *
  5. * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  6. */
  7. #ifndef V4L2_ASYNC_H
  8. #define V4L2_ASYNC_H
  9. #include <linux/list.h>
  10. #include <linux/mutex.h>
  11. struct dentry;
  12. struct device;
  13. struct device_node;
  14. struct v4l2_device;
  15. struct v4l2_subdev;
  16. struct v4l2_async_notifier;
  17. /**
  18. * enum v4l2_async_match_type - type of asynchronous subdevice logic to be used
  19. * in order to identify a match
  20. *
  21. * @V4L2_ASYNC_MATCH_TYPE_I2C: Match will check for I2C adapter ID and address
  22. * @V4L2_ASYNC_MATCH_TYPE_FWNODE: Match will use firmware node
  23. *
  24. * This enum is used by the asynchronous connection logic to define the
  25. * algorithm that will be used to match an asynchronous device.
  26. */
  27. enum v4l2_async_match_type {
  28. V4L2_ASYNC_MATCH_TYPE_I2C,
  29. V4L2_ASYNC_MATCH_TYPE_FWNODE,
  30. };
  31. /**
  32. * struct v4l2_async_match_desc - async connection match information
  33. *
  34. * @type: type of match that will be used
  35. * @fwnode: pointer to &struct fwnode_handle to be matched.
  36. * Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_FWNODE.
  37. * @i2c: embedded struct with I2C parameters to be matched.
  38. * Both @match.i2c.adapter_id and @match.i2c.address
  39. * should be matched.
  40. * Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.
  41. * @i2c.adapter_id:
  42. * I2C adapter ID to be matched.
  43. * Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.
  44. * @i2c.address:
  45. * I2C address to be matched.
  46. * Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.
  47. */
  48. struct v4l2_async_match_desc {
  49. enum v4l2_async_match_type type;
  50. union {
  51. struct fwnode_handle *fwnode;
  52. struct {
  53. int adapter_id;
  54. unsigned short address;
  55. } i2c;
  56. };
  57. };
  58. /**
  59. * struct v4l2_async_connection - sub-device connection descriptor, as known to
  60. * a bridge
  61. *
  62. * @match: struct of match type and per-bus type matching data sets
  63. * @notifier: the async notifier the connection is related to
  64. * @asc_entry: used to add struct v4l2_async_connection objects to the
  65. * notifier @waiting_list or @done_list
  66. * @asc_subdev_entry: entry in struct v4l2_async_subdev.asc_list list
  67. * @sd: the related sub-device
  68. *
  69. * When this struct is used as a member in a driver specific struct, the driver
  70. * specific struct shall contain the &struct v4l2_async_connection as its first
  71. * member.
  72. */
  73. struct v4l2_async_connection {
  74. struct v4l2_async_match_desc match;
  75. struct v4l2_async_notifier *notifier;
  76. struct list_head asc_entry;
  77. struct list_head asc_subdev_entry;
  78. struct v4l2_subdev *sd;
  79. };
  80. /**
  81. * struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations
  82. * @bound: a sub-device has been bound by the given connection
  83. * @complete: All connections have been bound successfully. The complete
  84. * callback is only executed for the root notifier.
  85. * @unbind: a subdevice is leaving
  86. * @destroy: the asc is about to be freed
  87. */
  88. struct v4l2_async_notifier_operations {
  89. int (*bound)(struct v4l2_async_notifier *notifier,
  90. struct v4l2_subdev *subdev,
  91. struct v4l2_async_connection *asc);
  92. int (*complete)(struct v4l2_async_notifier *notifier);
  93. void (*unbind)(struct v4l2_async_notifier *notifier,
  94. struct v4l2_subdev *subdev,
  95. struct v4l2_async_connection *asc);
  96. void (*destroy)(struct v4l2_async_connection *asc);
  97. };
  98. /**
  99. * struct v4l2_async_notifier - v4l2_device notifier data
  100. *
  101. * @ops: notifier operations
  102. * @v4l2_dev: v4l2_device of the root notifier, NULL otherwise
  103. * @sd: sub-device that registered the notifier, NULL otherwise
  104. * @parent: parent notifier
  105. * @waiting_list: list of struct v4l2_async_connection, waiting for their
  106. * drivers
  107. * @done_list: list of struct v4l2_subdev, already probed
  108. * @notifier_entry: member in a global list of notifiers
  109. */
  110. struct v4l2_async_notifier {
  111. const struct v4l2_async_notifier_operations *ops;
  112. struct v4l2_device *v4l2_dev;
  113. struct v4l2_subdev *sd;
  114. struct v4l2_async_notifier *parent;
  115. struct list_head waiting_list;
  116. struct list_head done_list;
  117. struct list_head notifier_entry;
  118. };
  119. /**
  120. * struct v4l2_async_subdev_endpoint - Entry in sub-device's fwnode list
  121. *
  122. * @async_subdev_endpoint_entry: An entry in async_subdev_endpoint_list of
  123. * &struct v4l2_subdev
  124. * @endpoint: Endpoint fwnode agains which to match the sub-device
  125. */
  126. struct v4l2_async_subdev_endpoint {
  127. struct list_head async_subdev_endpoint_entry;
  128. struct fwnode_handle *endpoint;
  129. };
  130. /**
  131. * v4l2_async_debug_init - Initialize debugging tools.
  132. *
  133. * @debugfs_dir: pointer to the parent debugfs &struct dentry
  134. */
  135. void v4l2_async_debug_init(struct dentry *debugfs_dir);
  136. /**
  137. * v4l2_async_nf_init - Initialize a notifier.
  138. *
  139. * @notifier: pointer to &struct v4l2_async_notifier
  140. * @v4l2_dev: pointer to &struct v4l2_device
  141. *
  142. * This function initializes the notifier @asc_entry. It must be called
  143. * before adding a subdevice to a notifier, using one of:
  144. * v4l2_async_nf_add_fwnode_remote(),
  145. * v4l2_async_nf_add_fwnode() or
  146. * v4l2_async_nf_add_i2c().
  147. */
  148. void v4l2_async_nf_init(struct v4l2_async_notifier *notifier,
  149. struct v4l2_device *v4l2_dev);
  150. /**
  151. * v4l2_async_subdev_nf_init - Initialize a sub-device notifier.
  152. *
  153. * @notifier: pointer to &struct v4l2_async_notifier
  154. * @sd: pointer to &struct v4l2_subdev
  155. *
  156. * This function initializes the notifier @asc_list. It must be called
  157. * before adding a subdevice to a notifier, using one of:
  158. * v4l2_async_nf_add_fwnode_remote(), v4l2_async_nf_add_fwnode() or
  159. * v4l2_async_nf_add_i2c().
  160. */
  161. void v4l2_async_subdev_nf_init(struct v4l2_async_notifier *notifier,
  162. struct v4l2_subdev *sd);
  163. struct v4l2_async_connection *
  164. __v4l2_async_nf_add_fwnode(struct v4l2_async_notifier *notifier,
  165. struct fwnode_handle *fwnode,
  166. unsigned int asc_struct_size);
  167. /**
  168. * v4l2_async_nf_add_fwnode - Allocate and add a fwnode async
  169. * subdev to the notifier's master asc_list.
  170. *
  171. * @notifier: pointer to &struct v4l2_async_notifier
  172. * @fwnode: fwnode handle of the sub-device to be matched, pointer to
  173. * &struct fwnode_handle
  174. * @type: Type of the driver's async sub-device or connection struct. The
  175. * &struct v4l2_async_connection shall be the first member of the
  176. * driver's async struct, i.e. both begin at the same memory address.
  177. *
  178. * Allocate a fwnode-matched asc of size asc_struct_size, and add it to the
  179. * notifiers @asc_list. The function also gets a reference of the fwnode which
  180. * is released later at notifier cleanup time.
  181. */
  182. #define v4l2_async_nf_add_fwnode(notifier, fwnode, type) \
  183. ((type *)__v4l2_async_nf_add_fwnode(notifier, fwnode, sizeof(type)))
  184. struct v4l2_async_connection *
  185. __v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier *notif,
  186. struct fwnode_handle *endpoint,
  187. unsigned int asc_struct_size);
  188. /**
  189. * v4l2_async_nf_add_fwnode_remote - Allocate and add a fwnode
  190. * remote async subdev to the
  191. * notifier's master asc_list.
  192. *
  193. * @notifier: pointer to &struct v4l2_async_notifier
  194. * @ep: local endpoint pointing to the remote connection to be matched,
  195. * pointer to &struct fwnode_handle
  196. * @type: Type of the driver's async connection struct. The &struct
  197. * v4l2_async_connection shall be the first member of the driver's async
  198. * connection struct, i.e. both begin at the same memory address.
  199. *
  200. * Gets the remote endpoint of a given local endpoint, set it up for fwnode
  201. * matching and adds the async connection to the notifier's @asc_list. The
  202. * function also gets a reference of the fwnode which is released later at
  203. * notifier cleanup time.
  204. *
  205. * This is just like v4l2_async_nf_add_fwnode(), but with the
  206. * exception that the fwnode refers to a local endpoint, not the remote one.
  207. */
  208. #define v4l2_async_nf_add_fwnode_remote(notifier, ep, type) \
  209. ((type *)__v4l2_async_nf_add_fwnode_remote(notifier, ep, sizeof(type)))
  210. struct v4l2_async_connection *
  211. __v4l2_async_nf_add_i2c(struct v4l2_async_notifier *notifier,
  212. int adapter_id, unsigned short address,
  213. unsigned int asc_struct_size);
  214. /**
  215. * v4l2_async_nf_add_i2c - Allocate and add an i2c async
  216. * subdev to the notifier's master asc_list.
  217. *
  218. * @notifier: pointer to &struct v4l2_async_notifier
  219. * @adapter: I2C adapter ID to be matched
  220. * @address: I2C address of connection to be matched
  221. * @type: Type of the driver's async connection struct. The &struct
  222. * v4l2_async_connection shall be the first member of the driver's async
  223. * connection struct, i.e. both begin at the same memory address.
  224. *
  225. * Same as v4l2_async_nf_add_fwnode() but for I2C matched
  226. * connections.
  227. */
  228. #define v4l2_async_nf_add_i2c(notifier, adapter, address, type) \
  229. ((type *)__v4l2_async_nf_add_i2c(notifier, adapter, address, \
  230. sizeof(type)))
  231. /**
  232. * v4l2_async_subdev_endpoint_add - Add an endpoint fwnode to async sub-device
  233. * matching list
  234. *
  235. * @sd: the sub-device
  236. * @fwnode: the endpoint fwnode to match
  237. *
  238. * Add a fwnode to the async sub-device's matching list. This allows registering
  239. * multiple async sub-devices from a single device.
  240. *
  241. * Note that calling v4l2_subdev_cleanup() as part of the sub-device's cleanup
  242. * if endpoints have been added to the sub-device's fwnode matching list.
  243. *
  244. * Returns an error on failure, 0 on success.
  245. */
  246. int v4l2_async_subdev_endpoint_add(struct v4l2_subdev *sd,
  247. struct fwnode_handle *fwnode);
  248. /**
  249. * v4l2_async_connection_unique - return a unique &struct v4l2_async_connection
  250. * for a sub-device
  251. * @sd: the sub-device
  252. *
  253. * Return an async connection for a sub-device, when there is a single
  254. * one only.
  255. */
  256. struct v4l2_async_connection *
  257. v4l2_async_connection_unique(struct v4l2_subdev *sd);
  258. /**
  259. * v4l2_async_nf_register - registers a subdevice asynchronous notifier
  260. *
  261. * @notifier: pointer to &struct v4l2_async_notifier
  262. */
  263. int v4l2_async_nf_register(struct v4l2_async_notifier *notifier);
  264. /**
  265. * v4l2_async_nf_unregister - unregisters a subdevice
  266. * asynchronous notifier
  267. *
  268. * @notifier: pointer to &struct v4l2_async_notifier
  269. */
  270. void v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier);
  271. /**
  272. * v4l2_async_nf_cleanup - clean up notifier resources
  273. * @notifier: the notifier the resources of which are to be cleaned up
  274. *
  275. * Release memory resources related to a notifier, including the async
  276. * connections allocated for the purposes of the notifier but not the notifier
  277. * itself. The user is responsible for calling this function to clean up the
  278. * notifier after calling v4l2_async_nf_add_fwnode_remote(),
  279. * v4l2_async_nf_add_fwnode() or v4l2_async_nf_add_i2c().
  280. *
  281. * There is no harm from calling v4l2_async_nf_cleanup() in other
  282. * cases as long as its memory has been zeroed after it has been
  283. * allocated.
  284. */
  285. void v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier);
  286. /**
  287. * v4l2_async_register_subdev - registers a sub-device to the asynchronous
  288. * subdevice framework
  289. *
  290. * @sd: pointer to &struct v4l2_subdev
  291. */
  292. #define v4l2_async_register_subdev(sd) \
  293. __v4l2_async_register_subdev(sd, THIS_MODULE)
  294. int __v4l2_async_register_subdev(struct v4l2_subdev *sd, struct module *module);
  295. /**
  296. * v4l2_async_register_subdev_sensor - registers a sensor sub-device to the
  297. * asynchronous sub-device framework and
  298. * parse set up common sensor related
  299. * devices
  300. *
  301. * @sd: pointer to struct &v4l2_subdev
  302. *
  303. * This function is just like v4l2_async_register_subdev() with the exception
  304. * that calling it will also parse firmware interfaces for remote references
  305. * using v4l2_async_nf_parse_fwnode_sensor() and registers the
  306. * async sub-devices. The sub-device is similarly unregistered by calling
  307. * v4l2_async_unregister_subdev().
  308. *
  309. * While registered, the subdev module is marked as in-use.
  310. *
  311. * An error is returned if the module is no longer loaded on any attempts
  312. * to register it.
  313. */
  314. int __must_check
  315. v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd);
  316. /**
  317. * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
  318. * subdevice framework
  319. *
  320. * @sd: pointer to &struct v4l2_subdev
  321. */
  322. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);
  323. #endif