v4l2-of.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/string.h>
  18. #include <linux/types.h>
  19. #include <media/v4l2-of.h>
  20. static int v4l2_of_parse_csi_bus(const struct device_node *node,
  21. struct v4l2_of_endpoint *endpoint)
  22. {
  23. struct v4l2_of_bus_mipi_csi2 *bus = &endpoint->bus.mipi_csi2;
  24. struct property *prop;
  25. bool have_clk_lane = false;
  26. unsigned int flags = 0;
  27. u32 v;
  28. prop = of_find_property(node, "data-lanes", NULL);
  29. if (prop) {
  30. const __be32 *lane = NULL;
  31. unsigned int i;
  32. for (i = 0; i < ARRAY_SIZE(bus->data_lanes); i++) {
  33. lane = of_prop_next_u32(prop, lane, &v);
  34. if (!lane)
  35. break;
  36. bus->data_lanes[i] = v;
  37. }
  38. bus->num_data_lanes = i;
  39. }
  40. prop = of_find_property(node, "lane-polarities", NULL);
  41. if (prop) {
  42. const __be32 *polarity = NULL;
  43. unsigned int i;
  44. for (i = 0; i < ARRAY_SIZE(bus->lane_polarities); i++) {
  45. polarity = of_prop_next_u32(prop, polarity, &v);
  46. if (!polarity)
  47. break;
  48. bus->lane_polarities[i] = v;
  49. }
  50. if (i < 1 + bus->num_data_lanes /* clock + data */) {
  51. pr_warn("%s: too few lane-polarities entries (need %u, got %u)\n",
  52. node->full_name, 1 + bus->num_data_lanes, i);
  53. return -EINVAL;
  54. }
  55. }
  56. if (!of_property_read_u32(node, "clock-lanes", &v)) {
  57. bus->clock_lane = v;
  58. have_clk_lane = true;
  59. }
  60. if (of_get_property(node, "clock-noncontinuous", &v))
  61. flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  62. else if (have_clk_lane || bus->num_data_lanes > 0)
  63. flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  64. bus->flags = flags;
  65. endpoint->bus_type = V4L2_MBUS_CSI2;
  66. return 0;
  67. }
  68. static void v4l2_of_parse_parallel_bus(const struct device_node *node,
  69. struct v4l2_of_endpoint *endpoint)
  70. {
  71. struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
  72. unsigned int flags = 0;
  73. u32 v;
  74. if (!of_property_read_u32(node, "hsync-active", &v))
  75. flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
  76. V4L2_MBUS_HSYNC_ACTIVE_LOW;
  77. if (!of_property_read_u32(node, "vsync-active", &v))
  78. flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
  79. V4L2_MBUS_VSYNC_ACTIVE_LOW;
  80. if (!of_property_read_u32(node, "pclk-sample", &v))
  81. flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
  82. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  83. if (!of_property_read_u32(node, "field-even-active", &v))
  84. flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
  85. V4L2_MBUS_FIELD_EVEN_LOW;
  86. if (flags)
  87. endpoint->bus_type = V4L2_MBUS_PARALLEL;
  88. else
  89. endpoint->bus_type = V4L2_MBUS_BT656;
  90. if (!of_property_read_u32(node, "data-active", &v))
  91. flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
  92. V4L2_MBUS_DATA_ACTIVE_LOW;
  93. if (of_get_property(node, "slave-mode", &v))
  94. flags |= V4L2_MBUS_SLAVE;
  95. else
  96. flags |= V4L2_MBUS_MASTER;
  97. if (!of_property_read_u32(node, "bus-width", &v))
  98. bus->bus_width = v;
  99. if (!of_property_read_u32(node, "data-shift", &v))
  100. bus->data_shift = v;
  101. if (!of_property_read_u32(node, "sync-on-green-active", &v))
  102. flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
  103. V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
  104. bus->flags = flags;
  105. }
  106. /**
  107. * v4l2_of_parse_endpoint() - parse all endpoint node properties
  108. * @node: pointer to endpoint device_node
  109. * @endpoint: pointer to the V4L2 OF endpoint data structure
  110. *
  111. * All properties are optional. If none are found, we don't set any flags.
  112. * This means the port has a static configuration and no properties have
  113. * to be specified explicitly.
  114. * If any properties that identify the bus as parallel are found and
  115. * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
  116. * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
  117. * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
  118. * The caller should hold a reference to @node.
  119. *
  120. * Return: 0.
  121. */
  122. int v4l2_of_parse_endpoint(const struct device_node *node,
  123. struct v4l2_of_endpoint *endpoint)
  124. {
  125. int rval;
  126. of_graph_parse_endpoint(node, &endpoint->base);
  127. /* Zero fields from bus_type to until the end */
  128. memset(&endpoint->bus_type, 0, sizeof(*endpoint) -
  129. offsetof(typeof(*endpoint), bus_type));
  130. rval = v4l2_of_parse_csi_bus(node, endpoint);
  131. if (rval)
  132. return rval;
  133. /*
  134. * Parse the parallel video bus properties only if none
  135. * of the MIPI CSI-2 specific properties were found.
  136. */
  137. if (endpoint->bus.mipi_csi2.flags == 0)
  138. v4l2_of_parse_parallel_bus(node, endpoint);
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(v4l2_of_parse_endpoint);
  142. /**
  143. * v4l2_of_parse_link() - parse a link between two endpoints
  144. * @node: pointer to the endpoint at the local end of the link
  145. * @link: pointer to the V4L2 OF link data structure
  146. *
  147. * Fill the link structure with the local and remote nodes and port numbers.
  148. * The local_node and remote_node fields are set to point to the local and
  149. * remote port's parent nodes respectively (the port parent node being the
  150. * parent node of the port node if that node isn't a 'ports' node, or the
  151. * grand-parent node of the port node otherwise).
  152. *
  153. * A reference is taken to both the local and remote nodes, the caller must use
  154. * v4l2_of_put_link() to drop the references when done with the link.
  155. *
  156. * Return: 0 on success, or -ENOLINK if the remote endpoint can't be found.
  157. */
  158. int v4l2_of_parse_link(const struct device_node *node,
  159. struct v4l2_of_link *link)
  160. {
  161. struct device_node *np;
  162. memset(link, 0, sizeof(*link));
  163. np = of_get_parent(node);
  164. of_property_read_u32(np, "reg", &link->local_port);
  165. np = of_get_next_parent(np);
  166. if (of_node_cmp(np->name, "ports") == 0)
  167. np = of_get_next_parent(np);
  168. link->local_node = np;
  169. np = of_parse_phandle(node, "remote-endpoint", 0);
  170. if (!np) {
  171. of_node_put(link->local_node);
  172. return -ENOLINK;
  173. }
  174. np = of_get_parent(np);
  175. of_property_read_u32(np, "reg", &link->remote_port);
  176. np = of_get_next_parent(np);
  177. if (of_node_cmp(np->name, "ports") == 0)
  178. np = of_get_next_parent(np);
  179. link->remote_node = np;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(v4l2_of_parse_link);
  183. /**
  184. * v4l2_of_put_link() - drop references to nodes in a link
  185. * @link: pointer to the V4L2 OF link data structure
  186. *
  187. * Drop references to the local and remote nodes in the link. This function must
  188. * be called on every link parsed with v4l2_of_parse_link().
  189. */
  190. void v4l2_of_put_link(struct v4l2_of_link *link)
  191. {
  192. of_node_put(link->local_node);
  193. of_node_put(link->remote_node);
  194. }
  195. EXPORT_SYMBOL(v4l2_of_put_link);