v4l2-of.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 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. if (!of_property_read_u32(node, "sync-on-green-active", &v))
  87. flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
  88. V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
  89. bus->flags = flags;
  90. }
  91. /**
  92. * v4l2_of_parse_endpoint() - parse all endpoint node properties
  93. * @node: pointer to endpoint device_node
  94. * @endpoint: pointer to the V4L2 OF endpoint data structure
  95. *
  96. * All properties are optional. If none are found, we don't set any flags.
  97. * This means the port has a static configuration and no properties have
  98. * to be specified explicitly.
  99. * If any properties that identify the bus as parallel are found and
  100. * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
  101. * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
  102. * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
  103. * The caller should hold a reference to @node.
  104. */
  105. void v4l2_of_parse_endpoint(const struct device_node *node,
  106. struct v4l2_of_endpoint *endpoint)
  107. {
  108. struct device_node *port_node = of_get_parent(node);
  109. memset(endpoint, 0, offsetof(struct v4l2_of_endpoint, head));
  110. endpoint->local_node = node;
  111. /*
  112. * It doesn't matter whether the two calls below succeed.
  113. * If they don't then the default value 0 is used.
  114. */
  115. of_property_read_u32(port_node, "reg", &endpoint->port);
  116. of_property_read_u32(node, "reg", &endpoint->id);
  117. v4l2_of_parse_csi_bus(node, endpoint);
  118. /*
  119. * Parse the parallel video bus properties only if none
  120. * of the MIPI CSI-2 specific properties were found.
  121. */
  122. if (endpoint->bus.mipi_csi2.flags == 0)
  123. v4l2_of_parse_parallel_bus(node, endpoint);
  124. of_node_put(port_node);
  125. }
  126. EXPORT_SYMBOL(v4l2_of_parse_endpoint);
  127. /**
  128. * v4l2_of_get_next_endpoint() - get next endpoint node
  129. * @parent: pointer to the parent device node
  130. * @prev: previous endpoint node, or NULL to get first
  131. *
  132. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  133. * of the passed @prev node is not decremented, the caller have to use
  134. * of_node_put() on it when done.
  135. */
  136. struct device_node *v4l2_of_get_next_endpoint(const struct device_node *parent,
  137. struct device_node *prev)
  138. {
  139. struct device_node *endpoint;
  140. struct device_node *port = NULL;
  141. if (!parent)
  142. return NULL;
  143. if (!prev) {
  144. struct device_node *node;
  145. /*
  146. * It's the first call, we have to find a port subnode
  147. * within this node or within an optional 'ports' node.
  148. */
  149. node = of_get_child_by_name(parent, "ports");
  150. if (node)
  151. parent = node;
  152. port = of_get_child_by_name(parent, "port");
  153. if (port) {
  154. /* Found a port, get an endpoint. */
  155. endpoint = of_get_next_child(port, NULL);
  156. of_node_put(port);
  157. } else {
  158. endpoint = NULL;
  159. }
  160. if (!endpoint)
  161. pr_err("%s(): no endpoint nodes specified for %s\n",
  162. __func__, parent->full_name);
  163. of_node_put(node);
  164. } else {
  165. port = of_get_parent(prev);
  166. if (!port)
  167. /* Hm, has someone given us the root node ?... */
  168. return NULL;
  169. /* Avoid dropping prev node refcount to 0. */
  170. of_node_get(prev);
  171. endpoint = of_get_next_child(port, prev);
  172. if (endpoint) {
  173. of_node_put(port);
  174. return endpoint;
  175. }
  176. /* No more endpoints under this port, try the next one. */
  177. do {
  178. port = of_get_next_child(parent, port);
  179. if (!port)
  180. return NULL;
  181. } while (of_node_cmp(port->name, "port"));
  182. /* Pick up the first endpoint in this port. */
  183. endpoint = of_get_next_child(port, NULL);
  184. of_node_put(port);
  185. }
  186. return endpoint;
  187. }
  188. EXPORT_SYMBOL(v4l2_of_get_next_endpoint);
  189. /**
  190. * v4l2_of_get_remote_port_parent() - get remote port's parent node
  191. * @node: pointer to a local endpoint device_node
  192. *
  193. * Return: Remote device node associated with remote endpoint node linked
  194. * to @node. Use of_node_put() on it when done.
  195. */
  196. struct device_node *v4l2_of_get_remote_port_parent(
  197. const struct device_node *node)
  198. {
  199. struct device_node *np;
  200. unsigned int depth;
  201. /* Get remote endpoint node. */
  202. np = of_parse_phandle(node, "remote-endpoint", 0);
  203. /* Walk 3 levels up only if there is 'ports' node. */
  204. for (depth = 3; depth && np; depth--) {
  205. np = of_get_next_parent(np);
  206. if (depth == 2 && of_node_cmp(np->name, "ports"))
  207. break;
  208. }
  209. return np;
  210. }
  211. EXPORT_SYMBOL(v4l2_of_get_remote_port_parent);
  212. /**
  213. * v4l2_of_get_remote_port() - get remote port node
  214. * @node: pointer to a local endpoint device_node
  215. *
  216. * Return: Remote port node associated with remote endpoint node linked
  217. * to @node. Use of_node_put() on it when done.
  218. */
  219. struct device_node *v4l2_of_get_remote_port(const struct device_node *node)
  220. {
  221. struct device_node *np;
  222. /* Get remote endpoint node. */
  223. np = of_parse_phandle(node, "remote-endpoint", 0);
  224. if (!np)
  225. return NULL;
  226. return of_get_parent(np);
  227. }
  228. EXPORT_SYMBOL(v4l2_of_get_remote_port);