v4l2-of.c 9.4 KB

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