vsp1_entity.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * vsp1_entity.c -- R-Car VSP1 Base Entity
  3. *
  4. * Copyright (C) 2013 Renesas Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <media/media-entity.h>
  16. #include <media/v4l2-subdev.h>
  17. #include "vsp1.h"
  18. #include "vsp1_entity.h"
  19. /* -----------------------------------------------------------------------------
  20. * V4L2 Subdevice Operations
  21. */
  22. struct v4l2_mbus_framefmt *
  23. vsp1_entity_get_pad_format(struct vsp1_entity *entity,
  24. struct v4l2_subdev_fh *fh,
  25. unsigned int pad, u32 which)
  26. {
  27. switch (which) {
  28. case V4L2_SUBDEV_FORMAT_TRY:
  29. return v4l2_subdev_get_try_format(fh, pad);
  30. case V4L2_SUBDEV_FORMAT_ACTIVE:
  31. return &entity->formats[pad];
  32. default:
  33. return NULL;
  34. }
  35. }
  36. /*
  37. * vsp1_entity_init_formats - Initialize formats on all pads
  38. * @subdev: V4L2 subdevice
  39. * @fh: V4L2 subdev file handle
  40. *
  41. * Initialize all pad formats with default values. If fh is not NULL, try
  42. * formats are initialized on the file handle. Otherwise active formats are
  43. * initialized on the device.
  44. */
  45. void vsp1_entity_init_formats(struct v4l2_subdev *subdev,
  46. struct v4l2_subdev_fh *fh)
  47. {
  48. struct v4l2_subdev_format format;
  49. unsigned int pad;
  50. for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
  51. memset(&format, 0, sizeof(format));
  52. format.pad = pad;
  53. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY
  54. : V4L2_SUBDEV_FORMAT_ACTIVE;
  55. v4l2_subdev_call(subdev, pad, set_fmt, fh, &format);
  56. }
  57. }
  58. static int vsp1_entity_open(struct v4l2_subdev *subdev,
  59. struct v4l2_subdev_fh *fh)
  60. {
  61. vsp1_entity_init_formats(subdev, fh);
  62. return 0;
  63. }
  64. const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
  65. .open = vsp1_entity_open,
  66. };
  67. /* -----------------------------------------------------------------------------
  68. * Media Operations
  69. */
  70. static int vsp1_entity_link_setup(struct media_entity *entity,
  71. const struct media_pad *local,
  72. const struct media_pad *remote, u32 flags)
  73. {
  74. struct vsp1_entity *source;
  75. if (!(local->flags & MEDIA_PAD_FL_SOURCE))
  76. return 0;
  77. source = container_of(local->entity, struct vsp1_entity, subdev.entity);
  78. if (!source->route)
  79. return 0;
  80. if (flags & MEDIA_LNK_FL_ENABLED) {
  81. if (source->sink)
  82. return -EBUSY;
  83. source->sink = remote->entity;
  84. } else {
  85. source->sink = NULL;
  86. }
  87. return 0;
  88. }
  89. const struct media_entity_operations vsp1_media_ops = {
  90. .link_setup = vsp1_entity_link_setup,
  91. .link_validate = v4l2_subdev_link_validate,
  92. };
  93. /* -----------------------------------------------------------------------------
  94. * Initialization
  95. */
  96. int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
  97. unsigned int num_pads)
  98. {
  99. static const struct {
  100. unsigned int id;
  101. unsigned int reg;
  102. } routes[] = {
  103. { VI6_DPR_NODE_LIF, 0 },
  104. { VI6_DPR_NODE_RPF(0), VI6_DPR_RPF_ROUTE(0) },
  105. { VI6_DPR_NODE_RPF(1), VI6_DPR_RPF_ROUTE(1) },
  106. { VI6_DPR_NODE_RPF(2), VI6_DPR_RPF_ROUTE(2) },
  107. { VI6_DPR_NODE_RPF(3), VI6_DPR_RPF_ROUTE(3) },
  108. { VI6_DPR_NODE_RPF(4), VI6_DPR_RPF_ROUTE(4) },
  109. { VI6_DPR_NODE_UDS(0), VI6_DPR_UDS_ROUTE(0) },
  110. { VI6_DPR_NODE_UDS(1), VI6_DPR_UDS_ROUTE(1) },
  111. { VI6_DPR_NODE_UDS(2), VI6_DPR_UDS_ROUTE(2) },
  112. { VI6_DPR_NODE_WPF(0), 0 },
  113. { VI6_DPR_NODE_WPF(1), 0 },
  114. { VI6_DPR_NODE_WPF(2), 0 },
  115. { VI6_DPR_NODE_WPF(3), 0 },
  116. };
  117. unsigned int i;
  118. for (i = 0; i < ARRAY_SIZE(routes); ++i) {
  119. if (routes[i].id == entity->id) {
  120. entity->route = routes[i].reg;
  121. break;
  122. }
  123. }
  124. if (i == ARRAY_SIZE(routes))
  125. return -EINVAL;
  126. entity->vsp1 = vsp1;
  127. entity->source_pad = num_pads - 1;
  128. /* Allocate formats and pads. */
  129. entity->formats = devm_kzalloc(vsp1->dev,
  130. num_pads * sizeof(*entity->formats),
  131. GFP_KERNEL);
  132. if (entity->formats == NULL)
  133. return -ENOMEM;
  134. entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
  135. GFP_KERNEL);
  136. if (entity->pads == NULL)
  137. return -ENOMEM;
  138. /* Initialize pads. */
  139. for (i = 0; i < num_pads - 1; ++i)
  140. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  141. entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
  142. /* Initialize the media entity. */
  143. return media_entity_init(&entity->subdev.entity, num_pads,
  144. entity->pads, 0);
  145. }
  146. void vsp1_entity_destroy(struct vsp1_entity *entity)
  147. {
  148. media_entity_cleanup(&entity->subdev.entity);
  149. }