v4l2-fwnode.c 12 KB

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