v4l2-fwnode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * V4L2 fwnode binding parsing library
  4. *
  5. * Copyright (c) 2016 Intel Corporation.
  6. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  7. *
  8. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  9. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  10. *
  11. * Copyright (C) 2012 Renesas Electronics Corp.
  12. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  13. */
  14. #ifndef _V4L2_FWNODE_H
  15. #define _V4L2_FWNODE_H
  16. #include <linux/errno.h>
  17. #include <linux/fwnode.h>
  18. #include <linux/list.h>
  19. #include <linux/types.h>
  20. #include <media/v4l2-mediabus.h>
  21. /**
  22. * struct v4l2_fwnode_endpoint - the endpoint data structure
  23. * @base: fwnode endpoint of the v4l2_fwnode
  24. * @bus_type: bus type
  25. * @bus: bus configuration data structure
  26. * @bus.parallel: embedded &struct v4l2_mbus_config_parallel.
  27. * Used if the bus is parallel.
  28. * @bus.mipi_csi1: embedded &struct v4l2_mbus_config_mipi_csi1.
  29. * Used if the bus is MIPI Alliance's Camera Serial
  30. * Interface version 1 (MIPI CSI1) or Standard
  31. * Mobile Imaging Architecture's Compact Camera Port 2
  32. * (SMIA CCP2).
  33. * @bus.mipi_csi2: embedded &struct v4l2_mbus_config_mipi_csi2.
  34. * Used if the bus is MIPI Alliance's Camera Serial
  35. * Interface version 2 (MIPI CSI2).
  36. * @link_frequencies: array of supported link frequencies
  37. * @nr_of_link_frequencies: number of elements in link_frequenccies array
  38. */
  39. struct v4l2_fwnode_endpoint {
  40. struct fwnode_endpoint base;
  41. enum v4l2_mbus_type bus_type;
  42. struct {
  43. struct v4l2_mbus_config_parallel parallel;
  44. struct v4l2_mbus_config_mipi_csi1 mipi_csi1;
  45. struct v4l2_mbus_config_mipi_csi2 mipi_csi2;
  46. } bus;
  47. u64 *link_frequencies;
  48. unsigned int nr_of_link_frequencies;
  49. };
  50. /**
  51. * V4L2_FWNODE_PROPERTY_UNSET - identify a non initialized property
  52. *
  53. * All properties in &struct v4l2_fwnode_device_properties are initialized
  54. * to this value.
  55. */
  56. #define V4L2_FWNODE_PROPERTY_UNSET (-1U)
  57. /**
  58. * enum v4l2_fwnode_orientation - possible device orientation
  59. * @V4L2_FWNODE_ORIENTATION_FRONT: device installed on the front side
  60. * @V4L2_FWNODE_ORIENTATION_BACK: device installed on the back side
  61. * @V4L2_FWNODE_ORIENTATION_EXTERNAL: device externally located
  62. */
  63. enum v4l2_fwnode_orientation {
  64. V4L2_FWNODE_ORIENTATION_FRONT,
  65. V4L2_FWNODE_ORIENTATION_BACK,
  66. V4L2_FWNODE_ORIENTATION_EXTERNAL
  67. };
  68. /**
  69. * struct v4l2_fwnode_device_properties - fwnode device properties
  70. * @orientation: device orientation. See &enum v4l2_fwnode_orientation
  71. * @rotation: device rotation
  72. */
  73. struct v4l2_fwnode_device_properties {
  74. enum v4l2_fwnode_orientation orientation;
  75. unsigned int rotation;
  76. };
  77. /**
  78. * struct v4l2_fwnode_link - a link between two endpoints
  79. * @local_node: pointer to device_node of this endpoint
  80. * @local_port: identifier of the port this endpoint belongs to
  81. * @local_id: identifier of the id this endpoint belongs to
  82. * @remote_node: pointer to device_node of the remote endpoint
  83. * @remote_port: identifier of the port the remote endpoint belongs to
  84. * @remote_id: identifier of the id the remote endpoint belongs to
  85. */
  86. struct v4l2_fwnode_link {
  87. struct fwnode_handle *local_node;
  88. unsigned int local_port;
  89. unsigned int local_id;
  90. struct fwnode_handle *remote_node;
  91. unsigned int remote_port;
  92. unsigned int remote_id;
  93. };
  94. /**
  95. * enum v4l2_connector_type - connector type
  96. * @V4L2_CONN_UNKNOWN: unknown connector type, no V4L2 connector configuration
  97. * @V4L2_CONN_COMPOSITE: analog composite connector
  98. * @V4L2_CONN_SVIDEO: analog svideo connector
  99. */
  100. enum v4l2_connector_type {
  101. V4L2_CONN_UNKNOWN,
  102. V4L2_CONN_COMPOSITE,
  103. V4L2_CONN_SVIDEO,
  104. };
  105. /**
  106. * struct v4l2_connector_link - connector link data structure
  107. * @head: structure to be used to add the link to the
  108. * &struct v4l2_fwnode_connector
  109. * @fwnode_link: &struct v4l2_fwnode_link link between the connector and the
  110. * device the connector belongs to.
  111. */
  112. struct v4l2_connector_link {
  113. struct list_head head;
  114. struct v4l2_fwnode_link fwnode_link;
  115. };
  116. /**
  117. * struct v4l2_fwnode_connector_analog - analog connector data structure
  118. * @sdtv_stds: sdtv standards this connector supports, set to V4L2_STD_ALL
  119. * if no restrictions are specified.
  120. */
  121. struct v4l2_fwnode_connector_analog {
  122. v4l2_std_id sdtv_stds;
  123. };
  124. /**
  125. * struct v4l2_fwnode_connector - the connector data structure
  126. * @name: the connector device name
  127. * @label: optional connector label
  128. * @type: connector type
  129. * @links: list of all connector &struct v4l2_connector_link links
  130. * @nr_of_links: total number of links
  131. * @connector: connector configuration
  132. * @connector.analog: analog connector configuration
  133. * &struct v4l2_fwnode_connector_analog
  134. */
  135. struct v4l2_fwnode_connector {
  136. const char *name;
  137. const char *label;
  138. enum v4l2_connector_type type;
  139. struct list_head links;
  140. unsigned int nr_of_links;
  141. union {
  142. struct v4l2_fwnode_connector_analog analog;
  143. /* future connectors */
  144. } connector;
  145. };
  146. /**
  147. * enum v4l2_fwnode_bus_type - Video bus types defined by firmware properties
  148. * @V4L2_FWNODE_BUS_TYPE_GUESS: Default value if no bus-type fwnode property
  149. * @V4L2_FWNODE_BUS_TYPE_CSI2_CPHY: MIPI CSI-2 bus, C-PHY physical layer
  150. * @V4L2_FWNODE_BUS_TYPE_CSI1: MIPI CSI-1 bus
  151. * @V4L2_FWNODE_BUS_TYPE_CCP2: SMIA Compact Camera Port 2 bus
  152. * @V4L2_FWNODE_BUS_TYPE_CSI2_DPHY: MIPI CSI-2 bus, D-PHY physical layer
  153. * @V4L2_FWNODE_BUS_TYPE_PARALLEL: Camera Parallel Interface bus
  154. * @V4L2_FWNODE_BUS_TYPE_BT656: BT.656 video format bus-type
  155. * @V4L2_FWNODE_BUS_TYPE_DPI: Video Parallel Interface bus
  156. * @NR_OF_V4L2_FWNODE_BUS_TYPE: Number of bus-types
  157. */
  158. enum v4l2_fwnode_bus_type {
  159. V4L2_FWNODE_BUS_TYPE_GUESS = 0,
  160. V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
  161. V4L2_FWNODE_BUS_TYPE_CSI1,
  162. V4L2_FWNODE_BUS_TYPE_CCP2,
  163. V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
  164. V4L2_FWNODE_BUS_TYPE_PARALLEL,
  165. V4L2_FWNODE_BUS_TYPE_BT656,
  166. V4L2_FWNODE_BUS_TYPE_DPI,
  167. NR_OF_V4L2_FWNODE_BUS_TYPE
  168. };
  169. /**
  170. * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
  171. * @fwnode: pointer to the endpoint's fwnode handle
  172. * @vep: pointer to the V4L2 fwnode data structure
  173. *
  174. * This function parses the V4L2 fwnode endpoint specific parameters from the
  175. * firmware. There are two ways to use this function, either by letting it
  176. * obtain the type of the bus (by setting the @vep.bus_type field to
  177. * V4L2_MBUS_UNKNOWN) or specifying the bus type explicitly to one of the &enum
  178. * v4l2_mbus_type types.
  179. *
  180. * When @vep.bus_type is V4L2_MBUS_UNKNOWN, the function will use the "bus-type"
  181. * property to determine the type when it is available. The caller is
  182. * responsible for validating the contents of @vep.bus_type field after the call
  183. * returns.
  184. *
  185. * As a deprecated functionality to support older DT bindings without "bus-type"
  186. * property for devices that support multiple types, if the "bus-type" property
  187. * does not exist, the function will attempt to guess the type based on the
  188. * endpoint properties available. NEVER RELY ON GUESSING THE BUS TYPE IN NEW
  189. * DRIVERS OR BINDINGS.
  190. *
  191. * It is also possible to set @vep.bus_type corresponding to an actual bus. In
  192. * this case the function will only attempt to parse properties related to this
  193. * bus, and it will return an error if the value of the "bus-type" property
  194. * corresponds to a different bus.
  195. *
  196. * The caller is required to initialise all fields of @vep, either with
  197. * explicitly values, or by zeroing them.
  198. *
  199. * The function does not change the V4L2 fwnode endpoint state if it fails.
  200. *
  201. * NOTE: This function does not parse "link-frequencies" property as its size is
  202. * not known in advance. Please use v4l2_fwnode_endpoint_alloc_parse() if you
  203. * need properties of variable size.
  204. *
  205. * Return: %0 on success or a negative error code on failure:
  206. * %-ENOMEM on memory allocation failure
  207. * %-EINVAL on parsing failure
  208. * %-ENXIO on mismatching bus types
  209. */
  210. int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
  211. struct v4l2_fwnode_endpoint *vep);
  212. /**
  213. * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
  214. * v4l2_fwnode_endpoint_alloc_parse()
  215. * @vep: the V4L2 fwnode the resources of which are to be released
  216. *
  217. * It is safe to call this function with NULL argument or on a V4L2 fwnode the
  218. * parsing of which failed.
  219. */
  220. void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
  221. /**
  222. * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
  223. * @fwnode: pointer to the endpoint's fwnode handle
  224. * @vep: pointer to the V4L2 fwnode data structure
  225. *
  226. * This function parses the V4L2 fwnode endpoint specific parameters from the
  227. * firmware. There are two ways to use this function, either by letting it
  228. * obtain the type of the bus (by setting the @vep.bus_type field to
  229. * V4L2_MBUS_UNKNOWN) or specifying the bus type explicitly to one of the &enum
  230. * v4l2_mbus_type types.
  231. *
  232. * When @vep.bus_type is V4L2_MBUS_UNKNOWN, the function will use the "bus-type"
  233. * property to determine the type when it is available. The caller is
  234. * responsible for validating the contents of @vep.bus_type field after the call
  235. * returns.
  236. *
  237. * As a deprecated functionality to support older DT bindings without "bus-type"
  238. * property for devices that support multiple types, if the "bus-type" property
  239. * does not exist, the function will attempt to guess the type based on the
  240. * endpoint properties available. NEVER RELY ON GUESSING THE BUS TYPE IN NEW
  241. * DRIVERS OR BINDINGS.
  242. *
  243. * It is also possible to set @vep.bus_type corresponding to an actual bus. In
  244. * this case the function will only attempt to parse properties related to this
  245. * bus, and it will return an error if the value of the "bus-type" property
  246. * corresponds to a different bus.
  247. *
  248. * The caller is required to initialise all fields of @vep, either with
  249. * explicitly values, or by zeroing them.
  250. *
  251. * The function does not change the V4L2 fwnode endpoint state if it fails.
  252. *
  253. * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
  254. * v4l2_fwnode_endpoint_parse():
  255. *
  256. * 1. It also parses variable size data.
  257. *
  258. * 2. The memory it has allocated to store the variable size data must be freed
  259. * using v4l2_fwnode_endpoint_free() when no longer needed.
  260. *
  261. * Return: %0 on success or a negative error code on failure:
  262. * %-ENOMEM on memory allocation failure
  263. * %-EINVAL on parsing failure
  264. * %-ENXIO on mismatching bus types
  265. */
  266. int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
  267. struct v4l2_fwnode_endpoint *vep);
  268. /**
  269. * v4l2_fwnode_parse_link() - parse a link between two endpoints
  270. * @fwnode: pointer to the endpoint's fwnode at the local end of the link
  271. * @link: pointer to the V4L2 fwnode link data structure
  272. *
  273. * Fill the link structure with the local and remote nodes and port numbers.
  274. * The local_node and remote_node fields are set to point to the local and
  275. * remote port's parent nodes respectively (the port parent node being the
  276. * parent node of the port node if that node isn't a 'ports' node, or the
  277. * grand-parent node of the port node otherwise).
  278. *
  279. * A reference is taken to both the local and remote nodes, the caller must use
  280. * v4l2_fwnode_put_link() to drop the references when done with the
  281. * link.
  282. *
  283. * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
  284. * found.
  285. */
  286. int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
  287. struct v4l2_fwnode_link *link);
  288. /**
  289. * v4l2_fwnode_put_link() - drop references to nodes in a link
  290. * @link: pointer to the V4L2 fwnode link data structure
  291. *
  292. * Drop references to the local and remote nodes in the link. This function
  293. * must be called on every link parsed with v4l2_fwnode_parse_link().
  294. */
  295. void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
  296. /**
  297. * v4l2_fwnode_connector_free() - free the V4L2 connector acquired memory
  298. * @connector: the V4L2 connector resources of which are to be released
  299. *
  300. * Free all allocated memory and put all links acquired by
  301. * v4l2_fwnode_connector_parse() and v4l2_fwnode_connector_add_link().
  302. *
  303. * It is safe to call this function with NULL argument or on a V4L2 connector
  304. * the parsing of which failed.
  305. */
  306. void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector);
  307. /**
  308. * v4l2_fwnode_connector_parse() - initialize the 'struct v4l2_fwnode_connector'
  309. * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
  310. * is connected to or to the connector endpoint fwnode handle.
  311. * @connector: pointer to the V4L2 fwnode connector data structure
  312. *
  313. * Fill the &struct v4l2_fwnode_connector with the connector type, label and
  314. * all &enum v4l2_connector_type specific connector data. The label is optional
  315. * so it is set to %NULL if no one was found. The function initialize the links
  316. * to zero. Adding links to the connector is done by calling
  317. * v4l2_fwnode_connector_add_link().
  318. *
  319. * The memory allocated for the label must be freed when no longer needed.
  320. * Freeing the memory is done by v4l2_fwnode_connector_free().
  321. *
  322. * Return:
  323. * * %0 on success or a negative error code on failure:
  324. * * %-EINVAL if @fwnode is invalid
  325. * * %-ENOTCONN if connector type is unknown or connector device can't be found
  326. */
  327. int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
  328. struct v4l2_fwnode_connector *connector);
  329. /**
  330. * v4l2_fwnode_connector_add_link - add a link between a connector node and
  331. * a v4l2-subdev node.
  332. * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
  333. * is connected to
  334. * @connector: pointer to the V4L2 fwnode connector data structure
  335. *
  336. * Add a new &struct v4l2_connector_link link to the
  337. * &struct v4l2_fwnode_connector connector links list. The link local_node
  338. * points to the connector node, the remote_node to the host v4l2 (sub)dev.
  339. *
  340. * The taken references to remote_node and local_node must be dropped and the
  341. * allocated memory must be freed when no longer needed. Both is done by calling
  342. * v4l2_fwnode_connector_free().
  343. *
  344. * Return:
  345. * * %0 on success or a negative error code on failure:
  346. * * %-EINVAL if @fwnode or @connector is invalid or @connector type is unknown
  347. * * %-ENOMEM on link memory allocation failure
  348. * * %-ENOTCONN if remote connector device can't be found
  349. * * %-ENOLINK if link parsing between v4l2 (sub)dev and connector fails
  350. */
  351. int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
  352. struct v4l2_fwnode_connector *connector);
  353. /**
  354. * v4l2_fwnode_device_parse() - parse fwnode device properties
  355. * @dev: pointer to &struct device
  356. * @props: pointer to &struct v4l2_fwnode_device_properties where to store the
  357. * parsed properties values
  358. *
  359. * This function parses and validates the V4L2 fwnode device properties from the
  360. * firmware interface, and fills the @struct v4l2_fwnode_device_properties
  361. * provided by the caller.
  362. *
  363. * Return:
  364. * % 0 on success
  365. * %-EINVAL if a parsed property value is not valid
  366. */
  367. int v4l2_fwnode_device_parse(struct device *dev,
  368. struct v4l2_fwnode_device_properties *props);
  369. /* Helper macros to access the connector links. */
  370. /** v4l2_connector_last_link - Helper macro to get the first
  371. * &struct v4l2_fwnode_connector link
  372. * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
  373. *
  374. * This marco returns the first added &struct v4l2_connector_link connector
  375. * link or @NULL if the connector has no links.
  376. */
  377. #define v4l2_connector_first_link(v4l2c) \
  378. list_first_entry_or_null(&(v4l2c)->links, \
  379. struct v4l2_connector_link, head)
  380. /** v4l2_connector_last_link - Helper macro to get the last
  381. * &struct v4l2_fwnode_connector link
  382. * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
  383. *
  384. * This marco returns the last &struct v4l2_connector_link added connector link.
  385. */
  386. #define v4l2_connector_last_link(v4l2c) \
  387. list_last_entry(&(v4l2c)->links, struct v4l2_connector_link, head)
  388. #endif /* _V4L2_FWNODE_H */