v4l2-fwnode.h 14 KB

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