vsp1_entity.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * vsp1_entity.c -- R-Car VSP1 Base Entity
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics 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-ctrls.h>
  17. #include <media/v4l2-subdev.h>
  18. #include "vsp1.h"
  19. #include "vsp1_entity.h"
  20. #include "vsp1_video.h"
  21. bool vsp1_entity_is_streaming(struct vsp1_entity *entity)
  22. {
  23. bool streaming;
  24. mutex_lock(&entity->lock);
  25. streaming = entity->streaming;
  26. mutex_unlock(&entity->lock);
  27. return streaming;
  28. }
  29. int vsp1_entity_set_streaming(struct vsp1_entity *entity, bool streaming)
  30. {
  31. int ret;
  32. mutex_lock(&entity->lock);
  33. entity->streaming = streaming;
  34. mutex_unlock(&entity->lock);
  35. if (!streaming)
  36. return 0;
  37. if (!entity->subdev.ctrl_handler)
  38. return 0;
  39. ret = v4l2_ctrl_handler_setup(entity->subdev.ctrl_handler);
  40. if (ret < 0) {
  41. mutex_lock(&entity->lock);
  42. entity->streaming = false;
  43. mutex_unlock(&entity->lock);
  44. }
  45. return ret;
  46. }
  47. /* -----------------------------------------------------------------------------
  48. * V4L2 Subdevice Operations
  49. */
  50. struct v4l2_mbus_framefmt *
  51. vsp1_entity_get_pad_format(struct vsp1_entity *entity,
  52. struct v4l2_subdev_pad_config *cfg,
  53. unsigned int pad, u32 which)
  54. {
  55. switch (which) {
  56. case V4L2_SUBDEV_FORMAT_TRY:
  57. return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
  58. case V4L2_SUBDEV_FORMAT_ACTIVE:
  59. return &entity->formats[pad];
  60. default:
  61. return NULL;
  62. }
  63. }
  64. /*
  65. * vsp1_entity_init_formats - Initialize formats on all pads
  66. * @subdev: V4L2 subdevice
  67. * @cfg: V4L2 subdev pad configuration
  68. *
  69. * Initialize all pad formats with default values. If cfg is not NULL, try
  70. * formats are initialized on the file handle. Otherwise active formats are
  71. * initialized on the device.
  72. */
  73. void vsp1_entity_init_formats(struct v4l2_subdev *subdev,
  74. struct v4l2_subdev_pad_config *cfg)
  75. {
  76. struct v4l2_subdev_format format;
  77. unsigned int pad;
  78. for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
  79. memset(&format, 0, sizeof(format));
  80. format.pad = pad;
  81. format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
  82. : V4L2_SUBDEV_FORMAT_ACTIVE;
  83. v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
  84. }
  85. }
  86. static int vsp1_entity_open(struct v4l2_subdev *subdev,
  87. struct v4l2_subdev_fh *fh)
  88. {
  89. vsp1_entity_init_formats(subdev, fh->pad);
  90. return 0;
  91. }
  92. const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
  93. .open = vsp1_entity_open,
  94. };
  95. /* -----------------------------------------------------------------------------
  96. * Media Operations
  97. */
  98. static int vsp1_entity_link_setup(struct media_entity *entity,
  99. const struct media_pad *local,
  100. const struct media_pad *remote, u32 flags)
  101. {
  102. struct vsp1_entity *source;
  103. if (!(local->flags & MEDIA_PAD_FL_SOURCE))
  104. return 0;
  105. source = container_of(local->entity, struct vsp1_entity, subdev.entity);
  106. if (!source->route)
  107. return 0;
  108. if (flags & MEDIA_LNK_FL_ENABLED) {
  109. if (source->sink)
  110. return -EBUSY;
  111. source->sink = remote->entity;
  112. source->sink_pad = remote->index;
  113. } else {
  114. source->sink = NULL;
  115. source->sink_pad = 0;
  116. }
  117. return 0;
  118. }
  119. const struct media_entity_operations vsp1_media_ops = {
  120. .link_setup = vsp1_entity_link_setup,
  121. .link_validate = v4l2_subdev_link_validate,
  122. };
  123. /* -----------------------------------------------------------------------------
  124. * Initialization
  125. */
  126. static const struct vsp1_route vsp1_routes[] = {
  127. { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
  128. { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
  129. VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), } },
  130. { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
  131. { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
  132. { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
  133. { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
  134. { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
  135. { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
  136. { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
  137. { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
  138. { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
  139. { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
  140. { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
  141. { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
  142. { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
  143. { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
  144. { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
  145. { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
  146. { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
  147. };
  148. int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
  149. unsigned int num_pads)
  150. {
  151. unsigned int i;
  152. for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
  153. if (vsp1_routes[i].type == entity->type &&
  154. vsp1_routes[i].index == entity->index) {
  155. entity->route = &vsp1_routes[i];
  156. break;
  157. }
  158. }
  159. if (i == ARRAY_SIZE(vsp1_routes))
  160. return -EINVAL;
  161. mutex_init(&entity->lock);
  162. entity->vsp1 = vsp1;
  163. entity->source_pad = num_pads - 1;
  164. /* Allocate formats and pads. */
  165. entity->formats = devm_kzalloc(vsp1->dev,
  166. num_pads * sizeof(*entity->formats),
  167. GFP_KERNEL);
  168. if (entity->formats == NULL)
  169. return -ENOMEM;
  170. entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
  171. GFP_KERNEL);
  172. if (entity->pads == NULL)
  173. return -ENOMEM;
  174. /* Initialize pads. */
  175. for (i = 0; i < num_pads - 1; ++i)
  176. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  177. entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
  178. /* Initialize the media entity. */
  179. return media_entity_init(&entity->subdev.entity, num_pads,
  180. entity->pads, 0);
  181. }
  182. void vsp1_entity_destroy(struct vsp1_entity *entity)
  183. {
  184. if (entity->video)
  185. vsp1_video_cleanup(entity->video);
  186. if (entity->subdev.ctrl_handler)
  187. v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
  188. media_entity_cleanup(&entity->subdev.entity);
  189. mutex_destroy(&entity->lock);
  190. }