vsp1_entity.c 6.3 KB

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