v4l2-of.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * V4L2 OF binding parsing library
  3. *
  4. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  5. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * Copyright (C) 2012 Renesas Electronics Corp.
  8. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/types.h>
  20. #include <media/v4l2-of.h>
  21. static int v4l2_of_parse_csi_bus(const struct device_node *node,
  22. struct v4l2_of_endpoint *endpoint)
  23. {
  24. struct v4l2_of_bus_mipi_csi2 *bus = &endpoint->bus.mipi_csi2;
  25. struct property *prop;
  26. bool have_clk_lane = false;
  27. unsigned int flags = 0;
  28. u32 v;
  29. prop = of_find_property(node, "data-lanes", NULL);
  30. if (prop) {
  31. const __be32 *lane = NULL;
  32. unsigned int i;
  33. for (i = 0; i < ARRAY_SIZE(bus->data_lanes); i++) {
  34. lane = of_prop_next_u32(prop, lane, &v);
  35. if (!lane)
  36. break;
  37. bus->data_lanes[i] = v;
  38. }
  39. bus->num_data_lanes = i;
  40. }
  41. prop = of_find_property(node, "lane-polarities", NULL);
  42. if (prop) {
  43. const __be32 *polarity = NULL;
  44. unsigned int i;
  45. for (i = 0; i < ARRAY_SIZE(bus->lane_polarities); i++) {
  46. polarity = of_prop_next_u32(prop, polarity, &v);
  47. if (!polarity)
  48. break;
  49. bus->lane_polarities[i] = v;
  50. }
  51. if (i < 1 + bus->num_data_lanes /* clock + data */) {
  52. pr_warn("%s: too few lane-polarities entries (need %u, got %u)\n",
  53. node->full_name, 1 + bus->num_data_lanes, i);
  54. return -EINVAL;
  55. }
  56. }
  57. if (!of_property_read_u32(node, "clock-lanes", &v)) {
  58. bus->clock_lane = v;
  59. have_clk_lane = true;
  60. }
  61. if (of_get_property(node, "clock-noncontinuous", &v))
  62. flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  63. else if (have_clk_lane || bus->num_data_lanes > 0)
  64. flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  65. bus->flags = flags;
  66. endpoint->bus_type = V4L2_MBUS_CSI2;
  67. return 0;
  68. }
  69. static void v4l2_of_parse_parallel_bus(const struct device_node *node,
  70. struct v4l2_of_endpoint *endpoint)
  71. {
  72. struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
  73. unsigned int flags = 0;
  74. u32 v;
  75. if (!of_property_read_u32(node, "hsync-active", &v))
  76. flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
  77. V4L2_MBUS_HSYNC_ACTIVE_LOW;
  78. if (!of_property_read_u32(node, "vsync-active", &v))
  79. flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
  80. V4L2_MBUS_VSYNC_ACTIVE_LOW;
  81. if (!of_property_read_u32(node, "field-even-active", &v))
  82. flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
  83. V4L2_MBUS_FIELD_EVEN_LOW;
  84. if (flags)
  85. endpoint->bus_type = V4L2_MBUS_PARALLEL;
  86. else
  87. endpoint->bus_type = V4L2_MBUS_BT656;
  88. if (!of_property_read_u32(node, "pclk-sample", &v))
  89. flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
  90. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  91. if (!of_property_read_u32(node, "data-active", &v))
  92. flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
  93. V4L2_MBUS_DATA_ACTIVE_LOW;
  94. if (of_get_property(node, "slave-mode", &v))
  95. flags |= V4L2_MBUS_SLAVE;
  96. else
  97. flags |= V4L2_MBUS_MASTER;
  98. if (!of_property_read_u32(node, "bus-width", &v))
  99. bus->bus_width = v;
  100. if (!of_property_read_u32(node, "data-shift", &v))
  101. bus->data_shift = v;
  102. if (!of_property_read_u32(node, "sync-on-green-active", &v))
  103. flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
  104. V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
  105. bus->flags = flags;
  106. }
  107. /**
  108. * v4l2_of_parse_endpoint() - parse all endpoint node properties
  109. * @node: pointer to endpoint device_node
  110. * @endpoint: pointer to the V4L2 OF endpoint data structure
  111. *
  112. * All properties are optional. If none are found, we don't set any flags.
  113. * This means the port has a static configuration and no properties have
  114. * to be specified explicitly.
  115. * If any properties that identify the bus as parallel are found and
  116. * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
  117. * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
  118. * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
  119. * The caller should hold a reference to @node.
  120. *
  121. * NOTE: This function does not parse properties the size of which is
  122. * variable without a low fixed limit. Please use
  123. * v4l2_of_alloc_parse_endpoint() in new drivers instead.
  124. *
  125. * Return: 0.
  126. */
  127. int v4l2_of_parse_endpoint(const struct device_node *node,
  128. struct v4l2_of_endpoint *endpoint)
  129. {
  130. int rval;
  131. of_graph_parse_endpoint(node, &endpoint->base);
  132. /* Zero fields from bus_type to until the end */
  133. memset(&endpoint->bus_type, 0, sizeof(*endpoint) -
  134. offsetof(typeof(*endpoint), bus_type));
  135. rval = v4l2_of_parse_csi_bus(node, endpoint);
  136. if (rval)
  137. return rval;
  138. /*
  139. * Parse the parallel video bus properties only if none
  140. * of the MIPI CSI-2 specific properties were found.
  141. */
  142. if (endpoint->bus.mipi_csi2.flags == 0)
  143. v4l2_of_parse_parallel_bus(node, endpoint);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(v4l2_of_parse_endpoint);
  147. /*
  148. * v4l2_of_free_endpoint() - free the endpoint acquired by
  149. * v4l2_of_alloc_parse_endpoint()
  150. * @endpoint - the endpoint the resources of which are to be released
  151. *
  152. * It is safe to call this function with NULL argument or on an
  153. * endpoint the parsing of which failed.
  154. */
  155. void v4l2_of_free_endpoint(struct v4l2_of_endpoint *endpoint)
  156. {
  157. if (IS_ERR_OR_NULL(endpoint))
  158. return;
  159. kfree(endpoint->link_frequencies);
  160. kfree(endpoint);
  161. }
  162. EXPORT_SYMBOL(v4l2_of_free_endpoint);
  163. /**
  164. * v4l2_of_alloc_parse_endpoint() - parse all endpoint node properties
  165. * @node: pointer to endpoint device_node
  166. *
  167. * All properties are optional. If none are found, we don't set any flags.
  168. * This means the port has a static configuration and no properties have
  169. * to be specified explicitly.
  170. * If any properties that identify the bus as parallel are found and
  171. * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
  172. * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
  173. * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
  174. * The caller should hold a reference to @node.
  175. *
  176. * v4l2_of_alloc_parse_endpoint() has two important differences to
  177. * v4l2_of_parse_endpoint():
  178. *
  179. * 1. It also parses variable size data and
  180. *
  181. * 2. The memory it has allocated to store the variable size data must
  182. * be freed using v4l2_of_free_endpoint() when no longer needed.
  183. *
  184. * Return: Pointer to v4l2_of_endpoint if successful, on error a
  185. * negative error code.
  186. */
  187. struct v4l2_of_endpoint *v4l2_of_alloc_parse_endpoint(
  188. const struct device_node *node)
  189. {
  190. struct v4l2_of_endpoint *endpoint;
  191. int len;
  192. int rval;
  193. endpoint = kzalloc(sizeof(*endpoint), GFP_KERNEL);
  194. if (!endpoint)
  195. return ERR_PTR(-ENOMEM);
  196. rval = v4l2_of_parse_endpoint(node, endpoint);
  197. if (rval < 0)
  198. goto out_err;
  199. if (of_get_property(node, "link-frequencies", &len)) {
  200. endpoint->link_frequencies = kmalloc(len, GFP_KERNEL);
  201. if (!endpoint->link_frequencies) {
  202. rval = -ENOMEM;
  203. goto out_err;
  204. }
  205. endpoint->nr_of_link_frequencies =
  206. len / sizeof(*endpoint->link_frequencies);
  207. rval = of_property_read_u64_array(
  208. node, "link-frequencies", endpoint->link_frequencies,
  209. endpoint->nr_of_link_frequencies);
  210. if (rval < 0)
  211. goto out_err;
  212. }
  213. return endpoint;
  214. out_err:
  215. v4l2_of_free_endpoint(endpoint);
  216. return ERR_PTR(rval);
  217. }
  218. EXPORT_SYMBOL(v4l2_of_alloc_parse_endpoint);
  219. /**
  220. * v4l2_of_parse_link() - parse a link between two endpoints
  221. * @node: pointer to the endpoint at the local end of the link
  222. * @link: pointer to the V4L2 OF link data structure
  223. *
  224. * Fill the link structure with the local and remote nodes and port numbers.
  225. * The local_node and remote_node fields are set to point to the local and
  226. * remote port's parent nodes respectively (the port parent node being the
  227. * parent node of the port node if that node isn't a 'ports' node, or the
  228. * grand-parent node of the port node otherwise).
  229. *
  230. * A reference is taken to both the local and remote nodes, the caller must use
  231. * v4l2_of_put_link() to drop the references when done with the link.
  232. *
  233. * Return: 0 on success, or -ENOLINK if the remote endpoint can't be found.
  234. */
  235. int v4l2_of_parse_link(const struct device_node *node,
  236. struct v4l2_of_link *link)
  237. {
  238. struct device_node *np;
  239. memset(link, 0, sizeof(*link));
  240. np = of_get_parent(node);
  241. of_property_read_u32(np, "reg", &link->local_port);
  242. np = of_get_next_parent(np);
  243. if (of_node_cmp(np->name, "ports") == 0)
  244. np = of_get_next_parent(np);
  245. link->local_node = np;
  246. np = of_parse_phandle(node, "remote-endpoint", 0);
  247. if (!np) {
  248. of_node_put(link->local_node);
  249. return -ENOLINK;
  250. }
  251. np = of_get_parent(np);
  252. of_property_read_u32(np, "reg", &link->remote_port);
  253. np = of_get_next_parent(np);
  254. if (of_node_cmp(np->name, "ports") == 0)
  255. np = of_get_next_parent(np);
  256. link->remote_node = np;
  257. return 0;
  258. }
  259. EXPORT_SYMBOL(v4l2_of_parse_link);
  260. /**
  261. * v4l2_of_put_link() - drop references to nodes in a link
  262. * @link: pointer to the V4L2 OF link data structure
  263. *
  264. * Drop references to the local and remote nodes in the link. This function must
  265. * be called on every link parsed with v4l2_of_parse_link().
  266. */
  267. void v4l2_of_put_link(struct v4l2_of_link *link)
  268. {
  269. of_node_put(link->local_node);
  270. of_node_put(link->remote_node);
  271. }
  272. EXPORT_SYMBOL(v4l2_of_put_link);