v4l2-fwnode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include <media/v4l2-subdev.h>
  25. struct fwnode_handle;
  26. struct v4l2_async_notifier;
  27. struct v4l2_async_subdev;
  28. #define V4L2_FWNODE_CSI2_MAX_DATA_LANES 4
  29. /**
  30. * struct v4l2_fwnode_bus_mipi_csi2 - MIPI CSI-2 bus data structure
  31. * @flags: media bus (V4L2_MBUS_*) flags
  32. * @data_lanes: an array of physical data lane indexes
  33. * @clock_lane: physical lane index of the clock lane
  34. * @num_data_lanes: number of data lanes
  35. * @lane_polarities: polarity of the lanes. The order is the same of
  36. * the physical lanes.
  37. */
  38. struct v4l2_fwnode_bus_mipi_csi2 {
  39. unsigned int flags;
  40. unsigned char data_lanes[V4L2_FWNODE_CSI2_MAX_DATA_LANES];
  41. unsigned char clock_lane;
  42. unsigned short num_data_lanes;
  43. bool lane_polarities[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
  44. };
  45. /**
  46. * struct v4l2_fwnode_bus_parallel - parallel data bus data structure
  47. * @flags: media bus (V4L2_MBUS_*) flags
  48. * @bus_width: bus width in bits
  49. * @data_shift: data shift in bits
  50. */
  51. struct v4l2_fwnode_bus_parallel {
  52. unsigned int flags;
  53. unsigned char bus_width;
  54. unsigned char data_shift;
  55. };
  56. /**
  57. * struct v4l2_fwnode_bus_mipi_csi1 - CSI-1/CCP2 data bus structure
  58. * @clock_inv: polarity of clock/strobe signal
  59. * false - not inverted, true - inverted
  60. * @strobe: false - data/clock, true - data/strobe
  61. * @lane_polarity: the polarities of the clock (index 0) and data lanes
  62. * index (1)
  63. * @data_lane: the number of the data lane
  64. * @clock_lane: the number of the clock lane
  65. */
  66. struct v4l2_fwnode_bus_mipi_csi1 {
  67. unsigned char clock_inv:1;
  68. unsigned char strobe:1;
  69. bool lane_polarity[2];
  70. unsigned char data_lane;
  71. unsigned char clock_lane;
  72. };
  73. /**
  74. * struct v4l2_fwnode_endpoint - the endpoint data structure
  75. * @base: fwnode endpoint of the v4l2_fwnode
  76. * @bus_type: bus type
  77. * @bus: union with bus configuration data structure
  78. * @bus.parallel: embedded &struct v4l2_fwnode_bus_parallel.
  79. * Used if the bus is parallel.
  80. * @bus.mipi_csi1: embedded &struct v4l2_fwnode_bus_mipi_csi1.
  81. * Used if the bus is MIPI Alliance's Camera Serial
  82. * Interface version 1 (MIPI CSI1) or Standard
  83. * Mobile Imaging Architecture's Compact Camera Port 2
  84. * (SMIA CCP2).
  85. * @bus.mipi_csi2: embedded &struct v4l2_fwnode_bus_mipi_csi2.
  86. * Used if the bus is MIPI Alliance's Camera Serial
  87. * Interface version 2 (MIPI CSI2).
  88. * @link_frequencies: array of supported link frequencies
  89. * @nr_of_link_frequencies: number of elements in link_frequenccies array
  90. */
  91. struct v4l2_fwnode_endpoint {
  92. struct fwnode_endpoint base;
  93. /*
  94. * Fields below this line will be zeroed by
  95. * v4l2_fwnode_endpoint_parse()
  96. */
  97. enum v4l2_mbus_type bus_type;
  98. union {
  99. struct v4l2_fwnode_bus_parallel parallel;
  100. struct v4l2_fwnode_bus_mipi_csi1 mipi_csi1;
  101. struct v4l2_fwnode_bus_mipi_csi2 mipi_csi2;
  102. } bus;
  103. u64 *link_frequencies;
  104. unsigned int nr_of_link_frequencies;
  105. };
  106. /**
  107. * struct v4l2_fwnode_link - a link between two endpoints
  108. * @local_node: pointer to device_node of this endpoint
  109. * @local_port: identifier of the port this endpoint belongs to
  110. * @remote_node: pointer to device_node of the remote endpoint
  111. * @remote_port: identifier of the port the remote endpoint belongs to
  112. */
  113. struct v4l2_fwnode_link {
  114. struct fwnode_handle *local_node;
  115. unsigned int local_port;
  116. struct fwnode_handle *remote_node;
  117. unsigned int remote_port;
  118. };
  119. /**
  120. * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
  121. * @fwnode: pointer to the endpoint's fwnode handle
  122. * @vep: pointer to the V4L2 fwnode data structure
  123. *
  124. * This function parses the V4L2 fwnode endpoint specific parameters from the
  125. * firmware. The caller is responsible for assigning @vep.bus_type to a valid
  126. * media bus type. The caller may also set the default configuration for the
  127. * endpoint --- a configuration that shall be in line with the DT binding
  128. * documentation. Should a device support multiple bus types, the caller may
  129. * call this function once the correct type is found --- with a default
  130. * configuration valid for that type.
  131. *
  132. * As a compatibility means guessing the bus type is also supported by setting
  133. * @vep.bus_type to V4L2_MBUS_UNKNOWN. The caller may not provide a default
  134. * configuration in this case as the defaults are specific to a given bus type.
  135. * This functionality is deprecated and should not be used in new drivers and it
  136. * is only supported for CSI-2 D-PHY, parallel and Bt.656 busses.
  137. *
  138. * The function does not change the V4L2 fwnode endpoint state if it fails.
  139. *
  140. * NOTE: This function does not parse properties the size of which is variable
  141. * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
  142. * new drivers instead.
  143. *
  144. * Return: %0 on success or a negative error code on failure:
  145. * %-ENOMEM on memory allocation failure
  146. * %-EINVAL on parsing failure
  147. * %-ENXIO on mismatching bus types
  148. */
  149. int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
  150. struct v4l2_fwnode_endpoint *vep);
  151. /**
  152. * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
  153. * v4l2_fwnode_endpoint_alloc_parse()
  154. * @vep: the V4L2 fwnode the resources of which are to be released
  155. *
  156. * It is safe to call this function with NULL argument or on a V4L2 fwnode the
  157. * parsing of which failed.
  158. */
  159. void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
  160. /**
  161. * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
  162. * @fwnode: pointer to the endpoint's fwnode handle
  163. * @vep: pointer to the V4L2 fwnode data structure
  164. *
  165. * This function parses the V4L2 fwnode endpoint specific parameters from the
  166. * firmware. The caller is responsible for assigning @vep.bus_type to a valid
  167. * media bus type. The caller may also set the default configuration for the
  168. * endpoint --- a configuration that shall be in line with the DT binding
  169. * documentation. Should a device support multiple bus types, the caller may
  170. * call this function once the correct type is found --- with a default
  171. * configuration valid for that type.
  172. *
  173. * As a compatibility means guessing the bus type is also supported by setting
  174. * @vep.bus_type to V4L2_MBUS_UNKNOWN. The caller may not provide a default
  175. * configuration in this case as the defaults are specific to a given bus type.
  176. * This functionality is deprecated and should not be used in new drivers and it
  177. * is only supported for CSI-2 D-PHY, parallel and Bt.656 busses.
  178. *
  179. * The function does not change the V4L2 fwnode endpoint state if it fails.
  180. *
  181. * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
  182. * v4l2_fwnode_endpoint_parse():
  183. *
  184. * 1. It also parses variable size data.
  185. *
  186. * 2. The memory it has allocated to store the variable size data must be freed
  187. * using v4l2_fwnode_endpoint_free() when no longer needed.
  188. *
  189. * Return: %0 on success or a negative error code on failure:
  190. * %-ENOMEM on memory allocation failure
  191. * %-EINVAL on parsing failure
  192. * %-ENXIO on mismatching bus types
  193. */
  194. int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
  195. struct v4l2_fwnode_endpoint *vep);
  196. /**
  197. * v4l2_fwnode_parse_link() - parse a link between two endpoints
  198. * @fwnode: pointer to the endpoint's fwnode at the local end of the link
  199. * @link: pointer to the V4L2 fwnode link data structure
  200. *
  201. * Fill the link structure with the local and remote nodes and port numbers.
  202. * The local_node and remote_node fields are set to point to the local and
  203. * remote port's parent nodes respectively (the port parent node being the
  204. * parent node of the port node if that node isn't a 'ports' node, or the
  205. * grand-parent node of the port node otherwise).
  206. *
  207. * A reference is taken to both the local and remote nodes, the caller must use
  208. * v4l2_fwnode_put_link() to drop the references when done with the
  209. * link.
  210. *
  211. * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
  212. * found.
  213. */
  214. int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
  215. struct v4l2_fwnode_link *link);
  216. /**
  217. * v4l2_fwnode_put_link() - drop references to nodes in a link
  218. * @link: pointer to the V4L2 fwnode link data structure
  219. *
  220. * Drop references to the local and remote nodes in the link. This function
  221. * must be called on every link parsed with v4l2_fwnode_parse_link().
  222. */
  223. void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
  224. /**
  225. * typedef parse_endpoint_func - Driver's callback function to be called on
  226. * each V4L2 fwnode endpoint.
  227. *
  228. * @dev: pointer to &struct device
  229. * @vep: pointer to &struct v4l2_fwnode_endpoint
  230. * @asd: pointer to &struct v4l2_async_subdev
  231. *
  232. * Return:
  233. * * %0 on success
  234. * * %-ENOTCONN if the endpoint is to be skipped but this
  235. * should not be considered as an error
  236. * * %-EINVAL if the endpoint configuration is invalid
  237. */
  238. typedef int (*parse_endpoint_func)(struct device *dev,
  239. struct v4l2_fwnode_endpoint *vep,
  240. struct v4l2_async_subdev *asd);
  241. /**
  242. * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
  243. * device node
  244. * @dev: the device the endpoints of which are to be parsed
  245. * @notifier: notifier for @dev
  246. * @asd_struct_size: size of the driver's async sub-device struct, including
  247. * sizeof(struct v4l2_async_subdev). The &struct
  248. * v4l2_async_subdev shall be the first member of
  249. * the driver's async sub-device struct, i.e. both
  250. * begin at the same memory address.
  251. * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
  252. * endpoint. Optional.
  253. *
  254. * Parse the fwnode endpoints of the @dev device and populate the async sub-
  255. * devices list in the notifier. The @parse_endpoint callback function is
  256. * called for each endpoint with the corresponding async sub-device pointer to
  257. * let the caller initialize the driver-specific part of the async sub-device
  258. * structure.
  259. *
  260. * The notifier memory shall be zeroed before this function is called on the
  261. * notifier.
  262. *
  263. * This function may not be called on a registered notifier and may be called on
  264. * a notifier only once.
  265. *
  266. * The &struct v4l2_fwnode_endpoint passed to the callback function
  267. * @parse_endpoint is released once the function is finished. If there is a need
  268. * to retain that configuration, the user needs to allocate memory for it.
  269. *
  270. * Any notifier populated using this function must be released with a call to
  271. * v4l2_async_notifier_cleanup() after it has been unregistered and the async
  272. * sub-devices are no longer in use, even if the function returned an error.
  273. *
  274. * Return: %0 on success, including when no async sub-devices are found
  275. * %-ENOMEM if memory allocation failed
  276. * %-EINVAL if graph or endpoint parsing failed
  277. * Other error codes as returned by @parse_endpoint
  278. */
  279. int
  280. v4l2_async_notifier_parse_fwnode_endpoints(struct device *dev,
  281. struct v4l2_async_notifier *notifier,
  282. size_t asd_struct_size,
  283. parse_endpoint_func parse_endpoint);
  284. /**
  285. * v4l2_async_notifier_parse_fwnode_endpoints_by_port - Parse V4L2 fwnode
  286. * endpoints of a port in a
  287. * device node
  288. * @dev: the device the endpoints of which are to be parsed
  289. * @notifier: notifier for @dev
  290. * @asd_struct_size: size of the driver's async sub-device struct, including
  291. * sizeof(struct v4l2_async_subdev). The &struct
  292. * v4l2_async_subdev shall be the first member of
  293. * the driver's async sub-device struct, i.e. both
  294. * begin at the same memory address.
  295. * @port: port number where endpoints are to be parsed
  296. * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
  297. * endpoint. Optional.
  298. *
  299. * This function is just like v4l2_async_notifier_parse_fwnode_endpoints() with
  300. * the exception that it only parses endpoints in a given port. This is useful
  301. * on devices that have both sinks and sources: the async sub-devices connected
  302. * to sources have already been configured by another driver (on capture
  303. * devices). In this case the driver must know which ports to parse.
  304. *
  305. * Parse the fwnode endpoints of the @dev device on a given @port and populate
  306. * the async sub-devices list of the notifier. The @parse_endpoint callback
  307. * function is called for each endpoint with the corresponding async sub-device
  308. * pointer to let the caller initialize the driver-specific part of the async
  309. * sub-device structure.
  310. *
  311. * The notifier memory shall be zeroed before this function is called on the
  312. * notifier the first time.
  313. *
  314. * This function may not be called on a registered notifier and may be called on
  315. * a notifier only once per port.
  316. *
  317. * The &struct v4l2_fwnode_endpoint passed to the callback function
  318. * @parse_endpoint is released once the function is finished. If there is a need
  319. * to retain that configuration, the user needs to allocate memory for it.
  320. *
  321. * Any notifier populated using this function must be released with a call to
  322. * v4l2_async_notifier_cleanup() after it has been unregistered and the async
  323. * sub-devices are no longer in use, even if the function returned an error.
  324. *
  325. * Return: %0 on success, including when no async sub-devices are found
  326. * %-ENOMEM if memory allocation failed
  327. * %-EINVAL if graph or endpoint parsing failed
  328. * Other error codes as returned by @parse_endpoint
  329. */
  330. int
  331. v4l2_async_notifier_parse_fwnode_endpoints_by_port(struct device *dev,
  332. struct v4l2_async_notifier *notifier,
  333. size_t asd_struct_size,
  334. unsigned int port,
  335. parse_endpoint_func parse_endpoint);
  336. /**
  337. * v4l2_fwnode_reference_parse_sensor_common - parse common references on
  338. * sensors for async sub-devices
  339. * @dev: the device node the properties of which are parsed for references
  340. * @notifier: the async notifier where the async subdevs will be added
  341. *
  342. * Parse common sensor properties for remote devices related to the
  343. * sensor and set up async sub-devices for them.
  344. *
  345. * Any notifier populated using this function must be released with a call to
  346. * v4l2_async_notifier_release() after it has been unregistered and the async
  347. * sub-devices are no longer in use, even in the case the function returned an
  348. * error.
  349. *
  350. * Return: 0 on success
  351. * -ENOMEM if memory allocation failed
  352. * -EINVAL if property parsing failed
  353. */
  354. int v4l2_async_notifier_parse_fwnode_sensor_common(struct device *dev,
  355. struct v4l2_async_notifier *notifier);
  356. /**
  357. * v4l2_async_register_fwnode_subdev - registers a sub-device to the
  358. * asynchronous sub-device framework
  359. * and parses fwnode endpoints
  360. *
  361. * @sd: pointer to struct &v4l2_subdev
  362. * @asd_struct_size: size of the driver's async sub-device struct, including
  363. * sizeof(struct v4l2_async_subdev). The &struct
  364. * v4l2_async_subdev shall be the first member of
  365. * the driver's async sub-device struct, i.e. both
  366. * begin at the same memory address.
  367. * @ports: array of port id's to parse for fwnode endpoints. If NULL, will
  368. * parse all ports owned by the sub-device.
  369. * @num_ports: number of ports in @ports array. Ignored if @ports is NULL.
  370. * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
  371. * endpoint. Optional.
  372. *
  373. * This function is just like v4l2_async_register_subdev() with the
  374. * exception that calling it will also allocate a notifier for the
  375. * sub-device, parse the sub-device's firmware node endpoints using
  376. * v4l2_async_notifier_parse_fwnode_endpoints() or
  377. * v4l2_async_notifier_parse_fwnode_endpoints_by_port(), and
  378. * registers the sub-device notifier. The sub-device is similarly
  379. * unregistered by calling v4l2_async_unregister_subdev().
  380. *
  381. * While registered, the subdev module is marked as in-use.
  382. *
  383. * An error is returned if the module is no longer loaded on any attempts
  384. * to register it.
  385. */
  386. int
  387. v4l2_async_register_fwnode_subdev(struct v4l2_subdev *sd,
  388. size_t asd_struct_size,
  389. unsigned int *ports,
  390. unsigned int num_ports,
  391. parse_endpoint_func parse_endpoint);
  392. #endif /* _V4L2_FWNODE_H */