v4l2-fwnode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * V4L2 fwnode binding parsing library
  3. *
  4. * Copyright (c) 2016 Intel Corporation.
  5. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  6. *
  7. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  8. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  9. *
  10. * Copyright (C) 2012 Renesas Electronics Corp.
  11. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. */
  17. #ifndef _V4L2_FWNODE_H
  18. #define _V4L2_FWNODE_H
  19. #include <linux/errno.h>
  20. #include <linux/fwnode.h>
  21. #include <linux/list.h>
  22. #include <linux/types.h>
  23. #include <media/v4l2-mediabus.h>
  24. struct fwnode_handle;
  25. struct v4l2_async_notifier;
  26. struct v4l2_async_subdev;
  27. #define V4L2_FWNODE_CSI2_MAX_DATA_LANES 4
  28. /**
  29. * struct v4l2_fwnode_bus_mipi_csi2 - MIPI CSI-2 bus data structure
  30. * @flags: media bus (V4L2_MBUS_*) flags
  31. * @data_lanes: an array of physical data lane indexes
  32. * @clock_lane: physical lane index of the clock lane
  33. * @num_data_lanes: number of data lanes
  34. * @lane_polarities: polarity of the lanes. The order is the same of
  35. * the physical lanes.
  36. */
  37. struct v4l2_fwnode_bus_mipi_csi2 {
  38. unsigned int flags;
  39. unsigned char data_lanes[V4L2_FWNODE_CSI2_MAX_DATA_LANES];
  40. unsigned char clock_lane;
  41. unsigned short num_data_lanes;
  42. bool lane_polarities[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
  43. };
  44. /**
  45. * struct v4l2_fwnode_bus_parallel - parallel data bus data structure
  46. * @flags: media bus (V4L2_MBUS_*) flags
  47. * @bus_width: bus width in bits
  48. * @data_shift: data shift in bits
  49. */
  50. struct v4l2_fwnode_bus_parallel {
  51. unsigned int flags;
  52. unsigned char bus_width;
  53. unsigned char data_shift;
  54. };
  55. /**
  56. * struct v4l2_fwnode_bus_mipi_csi1 - CSI-1/CCP2 data bus structure
  57. * @clock_inv: polarity of clock/strobe signal
  58. * false - not inverted, true - inverted
  59. * @strobe: false - data/clock, true - data/strobe
  60. * @lane_polarity: the polarities of the clock (index 0) and data lanes
  61. * index (1)
  62. * @data_lane: the number of the data lane
  63. * @clock_lane: the number of the clock lane
  64. */
  65. struct v4l2_fwnode_bus_mipi_csi1 {
  66. bool clock_inv;
  67. bool strobe;
  68. bool lane_polarity[2];
  69. unsigned char data_lane;
  70. unsigned char clock_lane;
  71. };
  72. /**
  73. * struct v4l2_fwnode_endpoint - the endpoint data structure
  74. * @base: fwnode endpoint of the v4l2_fwnode
  75. * @bus_type: bus type
  76. * @bus: union with bus configuration data structure
  77. * @bus.parallel: embedded &struct v4l2_fwnode_bus_parallel.
  78. * Used if the bus is parallel.
  79. * @bus.mipi_csi1: embedded &struct v4l2_fwnode_bus_mipi_csi1.
  80. * Used if the bus is MIPI Alliance's Camera Serial
  81. * Interface version 1 (MIPI CSI1) or Standard
  82. * Mobile Imaging Architecture's Compact Camera Port 2
  83. * (SMIA CCP2).
  84. * @bus.mipi_csi2: embedded &struct v4l2_fwnode_bus_mipi_csi2.
  85. * Used if the bus is MIPI Alliance's Camera Serial
  86. * Interface version 2 (MIPI CSI2).
  87. * @link_frequencies: array of supported link frequencies
  88. * @nr_of_link_frequencies: number of elements in link_frequenccies array
  89. */
  90. struct v4l2_fwnode_endpoint {
  91. struct fwnode_endpoint base;
  92. /*
  93. * Fields below this line will be zeroed by
  94. * v4l2_fwnode_endpoint_parse()
  95. */
  96. enum v4l2_mbus_type bus_type;
  97. union {
  98. struct v4l2_fwnode_bus_parallel parallel;
  99. struct v4l2_fwnode_bus_mipi_csi1 mipi_csi1;
  100. struct v4l2_fwnode_bus_mipi_csi2 mipi_csi2;
  101. } bus;
  102. u64 *link_frequencies;
  103. unsigned int nr_of_link_frequencies;
  104. };
  105. /**
  106. * struct v4l2_fwnode_link - a link between two endpoints
  107. * @local_node: pointer to device_node of this endpoint
  108. * @local_port: identifier of the port this endpoint belongs to
  109. * @remote_node: pointer to device_node of the remote endpoint
  110. * @remote_port: identifier of the port the remote endpoint belongs to
  111. */
  112. struct v4l2_fwnode_link {
  113. struct fwnode_handle *local_node;
  114. unsigned int local_port;
  115. struct fwnode_handle *remote_node;
  116. unsigned int remote_port;
  117. };
  118. /**
  119. * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
  120. * @fwnode: pointer to the endpoint's fwnode handle
  121. * @vep: pointer to the V4L2 fwnode data structure
  122. *
  123. * All properties are optional. If none are found, we don't set any flags. This
  124. * means the port has a static configuration and no properties have to be
  125. * specified explicitly. If any properties that identify the bus as parallel
  126. * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
  127. * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
  128. * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
  129. * reference to @fwnode.
  130. *
  131. * NOTE: This function does not parse properties the size of which is variable
  132. * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
  133. * new drivers instead.
  134. *
  135. * Return: 0 on success or a negative error code on failure.
  136. */
  137. int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
  138. struct v4l2_fwnode_endpoint *vep);
  139. /**
  140. * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
  141. * v4l2_fwnode_endpoint_alloc_parse()
  142. * @vep: the V4L2 fwnode the resources of which are to be released
  143. *
  144. * It is safe to call this function with NULL argument or on a V4L2 fwnode the
  145. * parsing of which failed.
  146. */
  147. void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
  148. /**
  149. * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
  150. * @fwnode: pointer to the endpoint's fwnode handle
  151. *
  152. * All properties are optional. If none are found, we don't set any flags. This
  153. * means the port has a static configuration and no properties have to be
  154. * specified explicitly. If any properties that identify the bus as parallel
  155. * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
  156. * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
  157. * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
  158. * reference to @fwnode.
  159. *
  160. * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
  161. * v4l2_fwnode_endpoint_parse():
  162. *
  163. * 1. It also parses variable size data.
  164. *
  165. * 2. The memory it has allocated to store the variable size data must be freed
  166. * using v4l2_fwnode_endpoint_free() when no longer needed.
  167. *
  168. * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
  169. * on error.
  170. */
  171. struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
  172. struct fwnode_handle *fwnode);
  173. /**
  174. * v4l2_fwnode_parse_link() - parse a link between two endpoints
  175. * @fwnode: pointer to the endpoint's fwnode at the local end of the link
  176. * @link: pointer to the V4L2 fwnode link data structure
  177. *
  178. * Fill the link structure with the local and remote nodes and port numbers.
  179. * The local_node and remote_node fields are set to point to the local and
  180. * remote port's parent nodes respectively (the port parent node being the
  181. * parent node of the port node if that node isn't a 'ports' node, or the
  182. * grand-parent node of the port node otherwise).
  183. *
  184. * A reference is taken to both the local and remote nodes, the caller must use
  185. * v4l2_fwnode_put_link() to drop the references when done with the
  186. * link.
  187. *
  188. * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
  189. * found.
  190. */
  191. int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
  192. struct v4l2_fwnode_link *link);
  193. /**
  194. * v4l2_fwnode_put_link() - drop references to nodes in a link
  195. * @link: pointer to the V4L2 fwnode link data structure
  196. *
  197. * Drop references to the local and remote nodes in the link. This function
  198. * must be called on every link parsed with v4l2_fwnode_parse_link().
  199. */
  200. void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
  201. /**
  202. * typedef parse_endpoint_func - Driver's callback function to be called on
  203. * each V4L2 fwnode endpoint.
  204. *
  205. * @dev: pointer to &struct device
  206. * @vep: pointer to &struct v4l2_fwnode_endpoint
  207. * @asd: pointer to &struct v4l2_async_subdev
  208. *
  209. * Return:
  210. * * %0 on success
  211. * * %-ENOTCONN if the endpoint is to be skipped but this
  212. * should not be considered as an error
  213. * * %-EINVAL if the endpoint configuration is invalid
  214. */
  215. typedef int (*parse_endpoint_func)(struct device *dev,
  216. struct v4l2_fwnode_endpoint *vep,
  217. struct v4l2_async_subdev *asd);
  218. /**
  219. * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
  220. * device node
  221. * @dev: the device the endpoints of which are to be parsed
  222. * @notifier: notifier for @dev
  223. * @asd_struct_size: size of the driver's async sub-device struct, including
  224. * sizeof(struct v4l2_async_subdev). The &struct
  225. * v4l2_async_subdev shall be the first member of
  226. * the driver's async sub-device struct, i.e. both
  227. * begin at the same memory address.
  228. * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
  229. * endpoint. Optional.
  230. *
  231. * Parse the fwnode endpoints of the @dev device and populate the async sub-
  232. * devices array of the notifier. The @parse_endpoint callback function is
  233. * called for each endpoint with the corresponding async sub-device pointer to
  234. * let the caller initialize the driver-specific part of the async sub-device
  235. * structure.
  236. *
  237. * The notifier memory shall be zeroed before this function is called on the
  238. * notifier.
  239. *
  240. * This function may not be called on a registered notifier and may be called on
  241. * a notifier only once.
  242. *
  243. * Do not change the notifier's subdevs array, take references to the subdevs
  244. * array itself or change the notifier's num_subdevs field. This is because this
  245. * function allocates and reallocates the subdevs array based on parsing
  246. * endpoints.
  247. *
  248. * The &struct v4l2_fwnode_endpoint passed to the callback function
  249. * @parse_endpoint is released once the function is finished. If there is a need
  250. * to retain that configuration, the user needs to allocate memory for it.
  251. *
  252. * Any notifier populated using this function must be released with a call to
  253. * v4l2_async_notifier_cleanup() after it has been unregistered and the async
  254. * sub-devices are no longer in use, even if the function returned an error.
  255. *
  256. * Return: %0 on success, including when no async sub-devices are found
  257. * %-ENOMEM if memory allocation failed
  258. * %-EINVAL if graph or endpoint parsing failed
  259. * Other error codes as returned by @parse_endpoint
  260. */
  261. int v4l2_async_notifier_parse_fwnode_endpoints(
  262. struct device *dev, struct v4l2_async_notifier *notifier,
  263. size_t asd_struct_size,
  264. parse_endpoint_func parse_endpoint);
  265. /**
  266. * v4l2_async_notifier_parse_fwnode_endpoints_by_port - Parse V4L2 fwnode
  267. * endpoints of a port in a
  268. * device node
  269. * @dev: the device the endpoints of which are to be parsed
  270. * @notifier: notifier for @dev
  271. * @asd_struct_size: size of the driver's async sub-device struct, including
  272. * sizeof(struct v4l2_async_subdev). The &struct
  273. * v4l2_async_subdev shall be the first member of
  274. * the driver's async sub-device struct, i.e. both
  275. * begin at the same memory address.
  276. * @port: port number where endpoints are to be parsed
  277. * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
  278. * endpoint. Optional.
  279. *
  280. * This function is just like v4l2_async_notifier_parse_fwnode_endpoints() with
  281. * the exception that it only parses endpoints in a given port. This is useful
  282. * on devices that have both sinks and sources: the async sub-devices connected
  283. * to sources have already been configured by another driver (on capture
  284. * devices). In this case the driver must know which ports to parse.
  285. *
  286. * Parse the fwnode endpoints of the @dev device on a given @port and populate
  287. * the async sub-devices array of the notifier. The @parse_endpoint callback
  288. * function is called for each endpoint with the corresponding async sub-device
  289. * pointer to let the caller initialize the driver-specific part of the async
  290. * sub-device structure.
  291. *
  292. * The notifier memory shall be zeroed before this function is called on the
  293. * notifier the first time.
  294. *
  295. * This function may not be called on a registered notifier and may be called on
  296. * a notifier only once per port.
  297. *
  298. * Do not change the notifier's subdevs array, take references to the subdevs
  299. * array itself or change the notifier's num_subdevs field. This is because this
  300. * function allocates and reallocates the subdevs array based on parsing
  301. * endpoints.
  302. *
  303. * The &struct v4l2_fwnode_endpoint passed to the callback function
  304. * @parse_endpoint is released once the function is finished. If there is a need
  305. * to retain that configuration, the user needs to allocate memory for it.
  306. *
  307. * Any notifier populated using this function must be released with a call to
  308. * v4l2_async_notifier_cleanup() after it has been unregistered and the async
  309. * sub-devices are no longer in use, even if the function returned an error.
  310. *
  311. * Return: %0 on success, including when no async sub-devices are found
  312. * %-ENOMEM if memory allocation failed
  313. * %-EINVAL if graph or endpoint parsing failed
  314. * Other error codes as returned by @parse_endpoint
  315. */
  316. int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
  317. struct device *dev, struct v4l2_async_notifier *notifier,
  318. size_t asd_struct_size, unsigned int port,
  319. parse_endpoint_func parse_endpoint);
  320. /**
  321. * v4l2_fwnode_reference_parse_sensor_common - parse common references on
  322. * sensors for async sub-devices
  323. * @dev: the device node the properties of which are parsed for references
  324. * @notifier: the async notifier where the async subdevs will be added
  325. *
  326. * Parse common sensor properties for remote devices related to the
  327. * sensor and set up async sub-devices for them.
  328. *
  329. * Any notifier populated using this function must be released with a call to
  330. * v4l2_async_notifier_release() after it has been unregistered and the async
  331. * sub-devices are no longer in use, even in the case the function returned an
  332. * error.
  333. *
  334. * Return: 0 on success
  335. * -ENOMEM if memory allocation failed
  336. * -EINVAL if property parsing failed
  337. */
  338. int v4l2_async_notifier_parse_fwnode_sensor_common(
  339. struct device *dev, struct v4l2_async_notifier *notifier);
  340. #endif /* _V4L2_FWNODE_H */