v4l2-async.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * V4L2 asynchronous subdevice registration API
  4. *
  5. * Copyright (C) 2012-2013, Guennadi Liakhovetski <[email protected]>
  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_I2C: Match will check for I2C adapter ID and address
  22. * @V4L2_ASYNC_MATCH_FWNODE: Match will use firmware node
  23. *
  24. * This enum is used by the asynchronous sub-device 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_I2C,
  29. V4L2_ASYNC_MATCH_FWNODE,
  30. };
  31. /**
  32. * struct v4l2_async_subdev - sub-device descriptor, as known to a bridge
  33. *
  34. * @match_type: type of match that will be used
  35. * @match: union of per-bus type matching data sets
  36. * @match.fwnode:
  37. * pointer to &struct fwnode_handle to be matched.
  38. * Used if @match_type is %V4L2_ASYNC_MATCH_FWNODE.
  39. * @match.i2c: embedded struct with I2C parameters to be matched.
  40. * Both @match.i2c.adapter_id and @match.i2c.address
  41. * should be matched.
  42. * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
  43. * @match.i2c.adapter_id:
  44. * I2C adapter ID to be matched.
  45. * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
  46. * @match.i2c.address:
  47. * I2C address to be matched.
  48. * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
  49. * @asd_list: used to add struct v4l2_async_subdev objects to the
  50. * master notifier @asd_list
  51. * @list: used to link struct v4l2_async_subdev objects, waiting to be
  52. * probed, to a notifier->waiting list
  53. *
  54. * When this struct is used as a member in a driver specific struct,
  55. * the driver specific struct shall contain the &struct
  56. * v4l2_async_subdev as its first member.
  57. */
  58. struct v4l2_async_subdev {
  59. enum v4l2_async_match_type match_type;
  60. union {
  61. struct fwnode_handle *fwnode;
  62. struct {
  63. int adapter_id;
  64. unsigned short address;
  65. } i2c;
  66. } match;
  67. /* v4l2-async core private: not to be used by drivers */
  68. struct list_head list;
  69. struct list_head asd_list;
  70. };
  71. /**
  72. * struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations
  73. * @bound: a subdevice driver has successfully probed one of the subdevices
  74. * @complete: All subdevices have been probed successfully. The complete
  75. * callback is only executed for the root notifier.
  76. * @unbind: a subdevice is leaving
  77. * @destroy: the asd is about to be freed
  78. */
  79. struct v4l2_async_notifier_operations {
  80. int (*bound)(struct v4l2_async_notifier *notifier,
  81. struct v4l2_subdev *subdev,
  82. struct v4l2_async_subdev *asd);
  83. int (*complete)(struct v4l2_async_notifier *notifier);
  84. void (*unbind)(struct v4l2_async_notifier *notifier,
  85. struct v4l2_subdev *subdev,
  86. struct v4l2_async_subdev *asd);
  87. void (*destroy)(struct v4l2_async_subdev *asd);
  88. };
  89. /**
  90. * struct v4l2_async_notifier - v4l2_device notifier data
  91. *
  92. * @ops: notifier operations
  93. * @v4l2_dev: v4l2_device of the root notifier, NULL otherwise
  94. * @sd: sub-device that registered the notifier, NULL otherwise
  95. * @parent: parent notifier
  96. * @asd_list: master list of struct v4l2_async_subdev
  97. * @waiting: list of struct v4l2_async_subdev, waiting for their drivers
  98. * @done: list of struct v4l2_subdev, already probed
  99. * @list: member in a global list of notifiers
  100. */
  101. struct v4l2_async_notifier {
  102. const struct v4l2_async_notifier_operations *ops;
  103. struct v4l2_device *v4l2_dev;
  104. struct v4l2_subdev *sd;
  105. struct v4l2_async_notifier *parent;
  106. struct list_head asd_list;
  107. struct list_head waiting;
  108. struct list_head done;
  109. struct list_head list;
  110. };
  111. /**
  112. * v4l2_async_debug_init - Initialize debugging tools.
  113. *
  114. * @debugfs_dir: pointer to the parent debugfs &struct dentry
  115. */
  116. void v4l2_async_debug_init(struct dentry *debugfs_dir);
  117. /**
  118. * v4l2_async_nf_init - Initialize a notifier.
  119. *
  120. * @notifier: pointer to &struct v4l2_async_notifier
  121. *
  122. * This function initializes the notifier @asd_list. It must be called
  123. * before adding a subdevice to a notifier, using one of:
  124. * v4l2_async_nf_add_fwnode_remote(),
  125. * v4l2_async_nf_add_fwnode(),
  126. * v4l2_async_nf_add_i2c(),
  127. * __v4l2_async_nf_add_subdev() or
  128. * v4l2_async_nf_parse_fwnode_endpoints().
  129. */
  130. void v4l2_async_nf_init(struct v4l2_async_notifier *notifier);
  131. /**
  132. * __v4l2_async_nf_add_subdev - Add an async subdev to the
  133. * notifier's master asd list.
  134. *
  135. * @notifier: pointer to &struct v4l2_async_notifier
  136. * @asd: pointer to &struct v4l2_async_subdev
  137. *
  138. * \warning: Drivers should avoid using this function and instead use one of:
  139. * v4l2_async_nf_add_fwnode(),
  140. * v4l2_async_nf_add_fwnode_remote() or
  141. * v4l2_async_nf_add_i2c().
  142. *
  143. * Call this function before registering a notifier to link the provided @asd to
  144. * the notifiers master @asd_list. The @asd must be allocated with k*alloc() as
  145. * it will be freed by the framework when the notifier is destroyed.
  146. */
  147. int __v4l2_async_nf_add_subdev(struct v4l2_async_notifier *notifier,
  148. struct v4l2_async_subdev *asd);
  149. struct v4l2_async_subdev *
  150. __v4l2_async_nf_add_fwnode(struct v4l2_async_notifier *notifier,
  151. struct fwnode_handle *fwnode,
  152. unsigned int asd_struct_size);
  153. /**
  154. * v4l2_async_nf_add_fwnode - Allocate and add a fwnode async
  155. * subdev to the notifier's master asd_list.
  156. *
  157. * @notifier: pointer to &struct v4l2_async_notifier
  158. * @fwnode: fwnode handle of the sub-device to be matched, pointer to
  159. * &struct fwnode_handle
  160. * @type: Type of the driver's async sub-device struct. The &struct
  161. * v4l2_async_subdev shall be the first member of the driver's async
  162. * sub-device struct, i.e. both begin at the same memory address.
  163. *
  164. * Allocate a fwnode-matched asd of size asd_struct_size, and add it to the
  165. * notifiers @asd_list. The function also gets a reference of the fwnode which
  166. * is released later at notifier cleanup time.
  167. */
  168. #define v4l2_async_nf_add_fwnode(notifier, fwnode, type) \
  169. ((type *)__v4l2_async_nf_add_fwnode(notifier, fwnode, sizeof(type)))
  170. struct v4l2_async_subdev *
  171. __v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier *notif,
  172. struct fwnode_handle *endpoint,
  173. unsigned int asd_struct_size);
  174. /**
  175. * v4l2_async_nf_add_fwnode_remote - Allocate and add a fwnode
  176. * remote async subdev to the
  177. * notifier's master asd_list.
  178. *
  179. * @notifier: pointer to &struct v4l2_async_notifier
  180. * @ep: local endpoint pointing to the remote sub-device to be matched,
  181. * pointer to &struct fwnode_handle
  182. * @type: Type of the driver's async sub-device struct. The &struct
  183. * v4l2_async_subdev shall be the first member of the driver's async
  184. * sub-device struct, i.e. both begin at the same memory address.
  185. *
  186. * Gets the remote endpoint of a given local endpoint, set it up for fwnode
  187. * matching and adds the async sub-device to the notifier's @asd_list. The
  188. * function also gets a reference of the fwnode which is released later at
  189. * notifier cleanup time.
  190. *
  191. * This is just like v4l2_async_nf_add_fwnode(), but with the
  192. * exception that the fwnode refers to a local endpoint, not the remote one.
  193. */
  194. #define v4l2_async_nf_add_fwnode_remote(notifier, ep, type) \
  195. ((type *)__v4l2_async_nf_add_fwnode_remote(notifier, ep, sizeof(type)))
  196. struct v4l2_async_subdev *
  197. __v4l2_async_nf_add_i2c(struct v4l2_async_notifier *notifier,
  198. int adapter_id, unsigned short address,
  199. unsigned int asd_struct_size);
  200. /**
  201. * v4l2_async_nf_add_i2c - Allocate and add an i2c async
  202. * subdev to the notifier's master asd_list.
  203. *
  204. * @notifier: pointer to &struct v4l2_async_notifier
  205. * @adapter: I2C adapter ID to be matched
  206. * @address: I2C address of sub-device to be matched
  207. * @type: Type of the driver's async sub-device struct. The &struct
  208. * v4l2_async_subdev shall be the first member of the driver's async
  209. * sub-device struct, i.e. both begin at the same memory address.
  210. *
  211. * Same as v4l2_async_nf_add_fwnode() but for I2C matched
  212. * sub-devices.
  213. */
  214. #define v4l2_async_nf_add_i2c(notifier, adapter, address, type) \
  215. ((type *)__v4l2_async_nf_add_i2c(notifier, adapter, address, \
  216. sizeof(type)))
  217. /**
  218. * v4l2_async_nf_register - registers a subdevice asynchronous notifier
  219. *
  220. * @v4l2_dev: pointer to &struct v4l2_device
  221. * @notifier: pointer to &struct v4l2_async_notifier
  222. */
  223. int v4l2_async_nf_register(struct v4l2_device *v4l2_dev,
  224. struct v4l2_async_notifier *notifier);
  225. /**
  226. * v4l2_async_subdev_nf_register - registers a subdevice asynchronous
  227. * notifier for a sub-device
  228. *
  229. * @sd: pointer to &struct v4l2_subdev
  230. * @notifier: pointer to &struct v4l2_async_notifier
  231. */
  232. int v4l2_async_subdev_nf_register(struct v4l2_subdev *sd,
  233. struct v4l2_async_notifier *notifier);
  234. /**
  235. * v4l2_async_nf_unregister - unregisters a subdevice
  236. * asynchronous notifier
  237. *
  238. * @notifier: pointer to &struct v4l2_async_notifier
  239. */
  240. void v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier);
  241. /**
  242. * v4l2_async_nf_cleanup - clean up notifier resources
  243. * @notifier: the notifier the resources of which are to be cleaned up
  244. *
  245. * Release memory resources related to a notifier, including the async
  246. * sub-devices allocated for the purposes of the notifier but not the notifier
  247. * itself. The user is responsible for calling this function to clean up the
  248. * notifier after calling
  249. * v4l2_async_nf_add_fwnode_remote(),
  250. * v4l2_async_nf_add_fwnode(),
  251. * v4l2_async_nf_add_i2c(),
  252. * __v4l2_async_nf_add_subdev() or
  253. * v4l2_async_nf_parse_fwnode_endpoints().
  254. *
  255. * There is no harm from calling v4l2_async_nf_cleanup() in other
  256. * cases as long as its memory has been zeroed after it has been
  257. * allocated.
  258. */
  259. void v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier);
  260. /**
  261. * v4l2_async_register_subdev - registers a sub-device to the asynchronous
  262. * subdevice framework
  263. *
  264. * @sd: pointer to &struct v4l2_subdev
  265. */
  266. int v4l2_async_register_subdev(struct v4l2_subdev *sd);
  267. /**
  268. * v4l2_async_register_subdev_sensor - registers a sensor sub-device to the
  269. * asynchronous sub-device framework and
  270. * parse set up common sensor related
  271. * devices
  272. *
  273. * @sd: pointer to struct &v4l2_subdev
  274. *
  275. * This function is just like v4l2_async_register_subdev() with the exception
  276. * that calling it will also parse firmware interfaces for remote references
  277. * using v4l2_async_nf_parse_fwnode_sensor() and registers the
  278. * async sub-devices. The sub-device is similarly unregistered by calling
  279. * v4l2_async_unregister_subdev().
  280. *
  281. * While registered, the subdev module is marked as in-use.
  282. *
  283. * An error is returned if the module is no longer loaded on any attempts
  284. * to register it.
  285. */
  286. int __must_check
  287. v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd);
  288. /**
  289. * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
  290. * subdevice framework
  291. *
  292. * @sd: pointer to &struct v4l2_subdev
  293. */
  294. void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);
  295. #endif