v4l2-of.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * V4L2 OF binding parsing library
  3. *
  4. * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
  5. * 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 void 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. u32 data_lanes[ARRAY_SIZE(bus->data_lanes)];
  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. int i;
  33. for (i = 0; i < ARRAY_SIZE(data_lanes); i++) {
  34. lane = of_prop_next_u32(prop, lane, &data_lanes[i]);
  35. if (!lane)
  36. break;
  37. }
  38. bus->num_data_lanes = i;
  39. while (i--)
  40. bus->data_lanes[i] = data_lanes[i];
  41. }
  42. if (!of_property_read_u32(node, "clock-lanes", &v)) {
  43. bus->clock_lane = v;
  44. have_clk_lane = true;
  45. }
  46. if (of_get_property(node, "clock-noncontinuous", &v))
  47. flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  48. else if (have_clk_lane || bus->num_data_lanes > 0)
  49. flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  50. bus->flags = flags;
  51. endpoint->bus_type = V4L2_MBUS_CSI2;
  52. }
  53. static void v4l2_of_parse_parallel_bus(const struct device_node *node,
  54. struct v4l2_of_endpoint *endpoint)
  55. {
  56. struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
  57. unsigned int flags = 0;
  58. u32 v;
  59. if (!of_property_read_u32(node, "hsync-active", &v))
  60. flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
  61. V4L2_MBUS_HSYNC_ACTIVE_LOW;
  62. if (!of_property_read_u32(node, "vsync-active", &v))
  63. flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
  64. V4L2_MBUS_VSYNC_ACTIVE_LOW;
  65. if (!of_property_read_u32(node, "pclk-sample", &v))
  66. flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
  67. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  68. if (!of_property_read_u32(node, "field-even-active", &v))
  69. flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
  70. V4L2_MBUS_FIELD_EVEN_LOW;
  71. if (flags)
  72. endpoint->bus_type = V4L2_MBUS_PARALLEL;
  73. else
  74. endpoint->bus_type = V4L2_MBUS_BT656;
  75. if (!of_property_read_u32(node, "data-active", &v))
  76. flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
  77. V4L2_MBUS_DATA_ACTIVE_LOW;
  78. if (of_get_property(node, "slave-mode", &v))
  79. flags |= V4L2_MBUS_SLAVE;
  80. else
  81. flags |= V4L2_MBUS_MASTER;
  82. if (!of_property_read_u32(node, "bus-width", &v))
  83. bus->bus_width = v;
  84. if (!of_property_read_u32(node, "data-shift", &v))
  85. bus->data_shift = v;
  86. bus->flags = flags;
  87. }
  88. EXPORT_SYMBOL(v4l2_of_parse_parallel_bus);
  89. /**
  90. * v4l2_of_parse_endpoint() - parse all endpoint node properties
  91. * @node: pointer to endpoint device_node
  92. * @endpoint: pointer to the V4L2 OF endpoint data structure
  93. *
  94. * All properties are optional. If none are found, we don't set any flags.
  95. * This means the port has a static configuration and no properties have
  96. * to be specified explicitly.
  97. * If any properties that identify the bus as parallel are found and
  98. * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
  99. * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
  100. * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
  101. * The caller should hold a reference to @node.
  102. */
  103. void v4l2_of_parse_endpoint(const struct device_node *node,
  104. struct v4l2_of_endpoint *endpoint)
  105. {
  106. struct device_node *port_node = of_get_parent(node);
  107. memset(endpoint, 0, offsetof(struct v4l2_of_endpoint, head));
  108. endpoint->local_node = node;
  109. /*
  110. * It doesn't matter whether the two calls below succeed.
  111. * If they don't then the default value 0 is used.
  112. */
  113. of_property_read_u32(port_node, "reg", &endpoint->port);
  114. of_property_read_u32(node, "reg", &endpoint->id);
  115. v4l2_of_parse_csi_bus(node, endpoint);
  116. /*
  117. * Parse the parallel video bus properties only if none
  118. * of the MIPI CSI-2 specific properties were found.
  119. */
  120. if (endpoint->bus.mipi_csi2.flags == 0)
  121. v4l2_of_parse_parallel_bus(node, endpoint);
  122. of_node_put(port_node);
  123. }
  124. EXPORT_SYMBOL(v4l2_of_parse_endpoint);
  125. /**
  126. * v4l2_of_get_next_endpoint() - get next endpoint node
  127. * @parent: pointer to the parent device node
  128. * @prev: previous endpoint node, or NULL to get first
  129. *
  130. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  131. * of the passed @prev node is not decremented, the caller have to use
  132. * of_node_put() on it when done.
  133. */
  134. struct device_node *v4l2_of_get_next_endpoint(const struct device_node *parent,
  135. struct device_node *prev)
  136. {
  137. struct device_node *endpoint;
  138. struct device_node *port = NULL;
  139. if (!parent)
  140. return NULL;
  141. if (!prev) {
  142. struct device_node *node;
  143. /*
  144. * It's the first call, we have to find a port subnode
  145. * within this node or within an optional 'ports' node.
  146. */
  147. node = of_get_child_by_name(parent, "ports");
  148. if (node)
  149. parent = node;
  150. for_each_child_of_node(parent, node) {
  151. if (!of_node_cmp(node->name, "port")) {
  152. port = node;
  153. break;
  154. }
  155. }
  156. if (port) {
  157. /* Found a port, get an endpoint. */
  158. endpoint = of_get_next_child(port, NULL);
  159. of_node_put(port);
  160. } else {
  161. endpoint = NULL;
  162. }
  163. if (!endpoint)
  164. pr_err("%s(): no endpoint nodes specified for %s\n",
  165. __func__, parent->full_name);
  166. } else {
  167. port = of_get_parent(prev);
  168. if (!port)
  169. /* Hm, has someone given us the root node ?... */
  170. return NULL;
  171. /* Avoid dropping prev node refcount to 0. */
  172. of_node_get(prev);
  173. endpoint = of_get_next_child(port, prev);
  174. if (endpoint) {
  175. of_node_put(port);
  176. return endpoint;
  177. }
  178. /* No more endpoints under this port, try the next one. */
  179. do {
  180. port = of_get_next_child(parent, port);
  181. if (!port)
  182. return NULL;
  183. } while (of_node_cmp(port->name, "port"));
  184. /* Pick up the first endpoint in this port. */
  185. endpoint = of_get_next_child(port, NULL);
  186. of_node_put(port);
  187. }
  188. return endpoint;
  189. }
  190. EXPORT_SYMBOL(v4l2_of_get_next_endpoint);
  191. /**
  192. * v4l2_of_get_remote_port_parent() - get remote port's parent node
  193. * @node: pointer to a local endpoint device_node
  194. *
  195. * Return: Remote device node associated with remote endpoint node linked
  196. * to @node. Use of_node_put() on it when done.
  197. */
  198. struct device_node *v4l2_of_get_remote_port_parent(
  199. const struct device_node *node)
  200. {
  201. struct device_node *np;
  202. unsigned int depth;
  203. /* Get remote endpoint node. */
  204. np = of_parse_phandle(node, "remote-endpoint", 0);
  205. /* Walk 3 levels up only if there is 'ports' node. */
  206. for (depth = 3; depth && np; depth--) {
  207. np = of_get_next_parent(np);
  208. if (depth == 2 && of_node_cmp(np->name, "ports"))
  209. break;
  210. }
  211. return np;
  212. }
  213. EXPORT_SYMBOL(v4l2_of_get_remote_port_parent);
  214. /**
  215. * v4l2_of_get_remote_port() - get remote port node
  216. * @node: pointer to a local endpoint device_node
  217. *
  218. * Return: Remote port node associated with remote endpoint node linked
  219. * to @node. Use of_node_put() on it when done.
  220. */
  221. struct device_node *v4l2_of_get_remote_port(const struct device_node *node)
  222. {
  223. struct device_node *np;
  224. /* Get remote endpoint node. */
  225. np = of_parse_phandle(node, "remote-endpoint", 0);
  226. if (!np)
  227. return NULL;
  228. return of_get_parent(np);
  229. }
  230. EXPORT_SYMBOL(v4l2_of_get_remote_port);