v4l2-of.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. endpoint->bus_type = 0;
  128. memset(&endpoint->bus, 0, sizeof(endpoint->bus));
  129. rval = v4l2_of_parse_csi_bus(node, endpoint);
  130. if (rval)
  131. return rval;
  132. /*
  133. * Parse the parallel video bus properties only if none
  134. * of the MIPI CSI-2 specific properties were found.
  135. */
  136. if (endpoint->bus.mipi_csi2.flags == 0)
  137. v4l2_of_parse_parallel_bus(node, endpoint);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(v4l2_of_parse_endpoint);