v4l2-fwnode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * V4L2 fwnode binding parsing library
  3. *
  4. * The origins of the V4L2 fwnode library are in V4L2 OF library that
  5. * formerly was located in v4l2-of.c.
  6. *
  7. * Copyright (c) 2016 Intel Corporation.
  8. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  9. *
  10. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  11. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  12. *
  13. * Copyright (C) 2012 Renesas Electronics Corp.
  14. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of version 2 of the GNU General Public License as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/acpi.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/property.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/types.h>
  28. #include <media/v4l2-fwnode.h>
  29. enum v4l2_fwnode_bus_type {
  30. V4L2_FWNODE_BUS_TYPE_GUESS = 0,
  31. V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
  32. V4L2_FWNODE_BUS_TYPE_CSI1,
  33. V4L2_FWNODE_BUS_TYPE_CCP2,
  34. NR_OF_V4L2_FWNODE_BUS_TYPE,
  35. };
  36. static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
  37. struct v4l2_fwnode_endpoint *vep)
  38. {
  39. struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
  40. bool have_clk_lane = false;
  41. unsigned int flags = 0, lanes_used = 0;
  42. unsigned int i;
  43. u32 v;
  44. int rval;
  45. rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
  46. if (rval > 0) {
  47. u32 array[MAX_DATA_LANES + 1];
  48. bus->num_data_lanes = min_t(int, MAX_DATA_LANES, rval);
  49. fwnode_property_read_u32_array(fwnode, "data-lanes", array,
  50. bus->num_data_lanes);
  51. for (i = 0; i < bus->num_data_lanes; i++) {
  52. if (lanes_used & BIT(array[i]))
  53. pr_warn("duplicated lane %u in data-lanes\n",
  54. array[i]);
  55. lanes_used |= BIT(array[i]);
  56. bus->data_lanes[i] = array[i];
  57. }
  58. rval = fwnode_property_read_u32_array(fwnode,
  59. "lane-polarities", NULL,
  60. 0);
  61. if (rval > 0) {
  62. if (rval != 1 + bus->num_data_lanes /* clock+data */) {
  63. pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
  64. 1 + bus->num_data_lanes, rval);
  65. return -EINVAL;
  66. }
  67. fwnode_property_read_u32_array(fwnode,
  68. "lane-polarities", array,
  69. 1 + bus->num_data_lanes);
  70. for (i = 0; i < 1 + bus->num_data_lanes; i++)
  71. bus->lane_polarities[i] = array[i];
  72. }
  73. }
  74. if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
  75. if (lanes_used & BIT(v))
  76. pr_warn("duplicated lane %u in clock-lanes\n", v);
  77. lanes_used |= BIT(v);
  78. bus->clock_lane = v;
  79. have_clk_lane = true;
  80. }
  81. if (fwnode_property_present(fwnode, "clock-noncontinuous"))
  82. flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  83. else if (have_clk_lane || bus->num_data_lanes > 0)
  84. flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  85. bus->flags = flags;
  86. vep->bus_type = V4L2_MBUS_CSI2;
  87. return 0;
  88. }
  89. static void v4l2_fwnode_endpoint_parse_parallel_bus(
  90. struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
  91. {
  92. struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
  93. unsigned int flags = 0;
  94. u32 v;
  95. if (!fwnode_property_read_u32(fwnode, "hsync-active", &v))
  96. flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
  97. V4L2_MBUS_HSYNC_ACTIVE_LOW;
  98. if (!fwnode_property_read_u32(fwnode, "vsync-active", &v))
  99. flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
  100. V4L2_MBUS_VSYNC_ACTIVE_LOW;
  101. if (!fwnode_property_read_u32(fwnode, "field-even-active", &v))
  102. flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
  103. V4L2_MBUS_FIELD_EVEN_LOW;
  104. if (flags)
  105. vep->bus_type = V4L2_MBUS_PARALLEL;
  106. else
  107. vep->bus_type = V4L2_MBUS_BT656;
  108. if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v))
  109. flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
  110. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  111. if (!fwnode_property_read_u32(fwnode, "data-active", &v))
  112. flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
  113. V4L2_MBUS_DATA_ACTIVE_LOW;
  114. if (fwnode_property_present(fwnode, "slave-mode"))
  115. flags |= V4L2_MBUS_SLAVE;
  116. else
  117. flags |= V4L2_MBUS_MASTER;
  118. if (!fwnode_property_read_u32(fwnode, "bus-width", &v))
  119. bus->bus_width = v;
  120. if (!fwnode_property_read_u32(fwnode, "data-shift", &v))
  121. bus->data_shift = v;
  122. if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v))
  123. flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
  124. V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
  125. bus->flags = flags;
  126. }
  127. static void
  128. v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
  129. struct v4l2_fwnode_endpoint *vep,
  130. u32 bus_type)
  131. {
  132. struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
  133. u32 v;
  134. if (!fwnode_property_read_u32(fwnode, "clock-inv", &v))
  135. bus->clock_inv = v;
  136. if (!fwnode_property_read_u32(fwnode, "strobe", &v))
  137. bus->strobe = v;
  138. if (!fwnode_property_read_u32(fwnode, "data-lanes", &v))
  139. bus->data_lane = v;
  140. if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v))
  141. bus->clock_lane = v;
  142. if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
  143. vep->bus_type = V4L2_MBUS_CCP2;
  144. else
  145. vep->bus_type = V4L2_MBUS_CSI1;
  146. }
  147. /**
  148. * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
  149. * @fwnode: pointer to the endpoint's fwnode handle
  150. * @vep: pointer to the V4L2 fwnode data structure
  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. * NOTE: This function does not parse properties the size of which is variable
  161. * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
  162. * new drivers instead.
  163. *
  164. * Return: 0 on success or a negative error code on failure.
  165. */
  166. int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
  167. struct v4l2_fwnode_endpoint *vep)
  168. {
  169. u32 bus_type = 0;
  170. int rval;
  171. fwnode_graph_parse_endpoint(fwnode, &vep->base);
  172. /* Zero fields from bus_type to until the end */
  173. memset(&vep->bus_type, 0, sizeof(*vep) -
  174. offsetof(typeof(*vep), bus_type));
  175. fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
  176. switch (bus_type) {
  177. case V4L2_FWNODE_BUS_TYPE_GUESS:
  178. rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
  179. if (rval)
  180. return rval;
  181. /*
  182. * Parse the parallel video bus properties only if none
  183. * of the MIPI CSI-2 specific properties were found.
  184. */
  185. if (vep->bus.mipi_csi2.flags == 0)
  186. v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
  187. return 0;
  188. case V4L2_FWNODE_BUS_TYPE_CCP2:
  189. case V4L2_FWNODE_BUS_TYPE_CSI1:
  190. v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
  191. return 0;
  192. default:
  193. pr_warn("unsupported bus type %u\n", bus_type);
  194. return -EINVAL;
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
  198. /*
  199. * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
  200. * v4l2_fwnode_endpoint_alloc_parse()
  201. * @vep - the V4L2 fwnode the resources of which are to be released
  202. *
  203. * It is safe to call this function with NULL argument or on a V4L2 fwnode the
  204. * parsing of which failed.
  205. */
  206. void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
  207. {
  208. if (IS_ERR_OR_NULL(vep))
  209. return;
  210. kfree(vep->link_frequencies);
  211. kfree(vep);
  212. }
  213. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
  214. /**
  215. * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
  216. * @fwnode: pointer to the endpoint's fwnode handle
  217. *
  218. * All properties are optional. If none are found, we don't set any flags. This
  219. * means the port has a static configuration and no properties have to be
  220. * specified explicitly. If any properties that identify the bus as parallel
  221. * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
  222. * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
  223. * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
  224. * reference to @fwnode.
  225. *
  226. * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
  227. * v4l2_fwnode_endpoint_parse():
  228. *
  229. * 1. It also parses variable size data.
  230. *
  231. * 2. The memory it has allocated to store the variable size data must be freed
  232. * using v4l2_fwnode_endpoint_free() when no longer needed.
  233. *
  234. * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
  235. * on error.
  236. */
  237. struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
  238. struct fwnode_handle *fwnode)
  239. {
  240. struct v4l2_fwnode_endpoint *vep;
  241. int rval;
  242. vep = kzalloc(sizeof(*vep), GFP_KERNEL);
  243. if (!vep)
  244. return ERR_PTR(-ENOMEM);
  245. rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
  246. if (rval < 0)
  247. goto out_err;
  248. rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
  249. NULL, 0);
  250. if (rval > 0) {
  251. vep->link_frequencies =
  252. kmalloc_array(rval, sizeof(*vep->link_frequencies),
  253. GFP_KERNEL);
  254. if (!vep->link_frequencies) {
  255. rval = -ENOMEM;
  256. goto out_err;
  257. }
  258. vep->nr_of_link_frequencies = rval;
  259. rval = fwnode_property_read_u64_array(
  260. fwnode, "link-frequencies", vep->link_frequencies,
  261. vep->nr_of_link_frequencies);
  262. if (rval < 0)
  263. goto out_err;
  264. }
  265. return vep;
  266. out_err:
  267. v4l2_fwnode_endpoint_free(vep);
  268. return ERR_PTR(rval);
  269. }
  270. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
  271. /**
  272. * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints
  273. * @__fwnode: pointer to the endpoint's fwnode at the local end of the link
  274. * @link: pointer to the V4L2 fwnode link data structure
  275. *
  276. * Fill the link structure with the local and remote nodes and port numbers.
  277. * The local_node and remote_node fields are set to point to the local and
  278. * remote port's parent nodes respectively (the port parent node being the
  279. * parent node of the port node if that node isn't a 'ports' node, or the
  280. * grand-parent node of the port node otherwise).
  281. *
  282. * A reference is taken to both the local and remote nodes, the caller must use
  283. * v4l2_fwnode_endpoint_put_link() to drop the references when done with the
  284. * link.
  285. *
  286. * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
  287. * found.
  288. */
  289. int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
  290. struct v4l2_fwnode_link *link)
  291. {
  292. const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
  293. struct fwnode_handle *fwnode;
  294. memset(link, 0, sizeof(*link));
  295. fwnode = fwnode_get_parent(__fwnode);
  296. fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
  297. fwnode = fwnode_get_next_parent(fwnode);
  298. if (is_of_node(fwnode) &&
  299. of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
  300. fwnode = fwnode_get_next_parent(fwnode);
  301. link->local_node = fwnode;
  302. fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
  303. if (!fwnode) {
  304. fwnode_handle_put(fwnode);
  305. return -ENOLINK;
  306. }
  307. fwnode = fwnode_get_parent(fwnode);
  308. fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
  309. fwnode = fwnode_get_next_parent(fwnode);
  310. if (is_of_node(fwnode) &&
  311. of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
  312. fwnode = fwnode_get_next_parent(fwnode);
  313. link->remote_node = fwnode;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
  317. /**
  318. * v4l2_fwnode_put_link() - drop references to nodes in a link
  319. * @link: pointer to the V4L2 fwnode link data structure
  320. *
  321. * Drop references to the local and remote nodes in the link. This function
  322. * must be called on every link parsed with v4l2_fwnode_parse_link().
  323. */
  324. void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
  325. {
  326. fwnode_handle_put(link->local_node);
  327. fwnode_handle_put(link->remote_node);
  328. }
  329. EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
  330. MODULE_LICENSE("GPL");
  331. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
  332. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  333. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");