vsp1_uif.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_uif.c -- R-Car VSP1 User Logic Interface
  4. *
  5. * Copyright (C) 2017-2018 Laurent Pinchart
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gfp.h>
  11. #include <linux/sys_soc.h>
  12. #include <media/media-entity.h>
  13. #include <media/v4l2-subdev.h>
  14. #include "vsp1.h"
  15. #include "vsp1_dl.h"
  16. #include "vsp1_entity.h"
  17. #include "vsp1_uif.h"
  18. #define UIF_MIN_SIZE 4U
  19. #define UIF_MAX_SIZE 8190U
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_uif_read(struct vsp1_uif *uif, u32 reg)
  24. {
  25. return vsp1_read(uif->entity.vsp1,
  26. uif->entity.index * VI6_UIF_OFFSET + reg);
  27. }
  28. static inline void vsp1_uif_write(struct vsp1_uif *uif,
  29. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  30. {
  31. vsp1_dl_body_write(dlb, reg + uif->entity.index * VI6_UIF_OFFSET, data);
  32. }
  33. u32 vsp1_uif_get_crc(struct vsp1_uif *uif)
  34. {
  35. return vsp1_uif_read(uif, VI6_UIF_DISCOM_DOCMCCRCR);
  36. }
  37. /* -----------------------------------------------------------------------------
  38. * V4L2 Subdevice Pad Operations
  39. */
  40. static const unsigned int uif_codes[] = {
  41. MEDIA_BUS_FMT_ARGB8888_1X32,
  42. MEDIA_BUS_FMT_AHSV8888_1X32,
  43. MEDIA_BUS_FMT_AYUV8_1X32,
  44. };
  45. static int uif_enum_mbus_code(struct v4l2_subdev *subdev,
  46. struct v4l2_subdev_pad_config *cfg,
  47. struct v4l2_subdev_mbus_code_enum *code)
  48. {
  49. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, uif_codes,
  50. ARRAY_SIZE(uif_codes));
  51. }
  52. static int uif_enum_frame_size(struct v4l2_subdev *subdev,
  53. struct v4l2_subdev_pad_config *cfg,
  54. struct v4l2_subdev_frame_size_enum *fse)
  55. {
  56. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, UIF_MIN_SIZE,
  57. UIF_MIN_SIZE, UIF_MAX_SIZE,
  58. UIF_MAX_SIZE);
  59. }
  60. static int uif_set_format(struct v4l2_subdev *subdev,
  61. struct v4l2_subdev_pad_config *cfg,
  62. struct v4l2_subdev_format *fmt)
  63. {
  64. return vsp1_subdev_set_pad_format(subdev, cfg, fmt, uif_codes,
  65. ARRAY_SIZE(uif_codes),
  66. UIF_MIN_SIZE, UIF_MIN_SIZE,
  67. UIF_MAX_SIZE, UIF_MAX_SIZE);
  68. }
  69. static int uif_get_selection(struct v4l2_subdev *subdev,
  70. struct v4l2_subdev_pad_config *cfg,
  71. struct v4l2_subdev_selection *sel)
  72. {
  73. struct vsp1_uif *uif = to_uif(subdev);
  74. struct v4l2_subdev_pad_config *config;
  75. struct v4l2_mbus_framefmt *format;
  76. int ret = 0;
  77. if (sel->pad != UIF_PAD_SINK)
  78. return -EINVAL;
  79. mutex_lock(&uif->entity.lock);
  80. config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which);
  81. if (!config) {
  82. ret = -EINVAL;
  83. goto done;
  84. }
  85. switch (sel->target) {
  86. case V4L2_SEL_TGT_CROP_BOUNDS:
  87. case V4L2_SEL_TGT_CROP_DEFAULT:
  88. format = vsp1_entity_get_pad_format(&uif->entity, config,
  89. UIF_PAD_SINK);
  90. sel->r.left = 0;
  91. sel->r.top = 0;
  92. sel->r.width = format->width;
  93. sel->r.height = format->height;
  94. break;
  95. case V4L2_SEL_TGT_CROP:
  96. sel->r = *vsp1_entity_get_pad_selection(&uif->entity, config,
  97. sel->pad, sel->target);
  98. break;
  99. default:
  100. ret = -EINVAL;
  101. break;
  102. }
  103. done:
  104. mutex_unlock(&uif->entity.lock);
  105. return ret;
  106. }
  107. static int uif_set_selection(struct v4l2_subdev *subdev,
  108. struct v4l2_subdev_pad_config *cfg,
  109. struct v4l2_subdev_selection *sel)
  110. {
  111. struct vsp1_uif *uif = to_uif(subdev);
  112. struct v4l2_subdev_pad_config *config;
  113. struct v4l2_mbus_framefmt *format;
  114. struct v4l2_rect *selection;
  115. int ret = 0;
  116. if (sel->pad != UIF_PAD_SINK ||
  117. sel->target != V4L2_SEL_TGT_CROP)
  118. return -EINVAL;
  119. mutex_lock(&uif->entity.lock);
  120. config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which);
  121. if (!config) {
  122. ret = -EINVAL;
  123. goto done;
  124. }
  125. /* The crop rectangle must be inside the input frame. */
  126. format = vsp1_entity_get_pad_format(&uif->entity, config, UIF_PAD_SINK);
  127. sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
  128. sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
  129. sel->r.width = clamp_t(unsigned int, sel->r.width, UIF_MIN_SIZE,
  130. format->width - sel->r.left);
  131. sel->r.height = clamp_t(unsigned int, sel->r.height, UIF_MIN_SIZE,
  132. format->height - sel->r.top);
  133. /* Store the crop rectangle. */
  134. selection = vsp1_entity_get_pad_selection(&uif->entity, config,
  135. sel->pad, V4L2_SEL_TGT_CROP);
  136. *selection = sel->r;
  137. done:
  138. mutex_unlock(&uif->entity.lock);
  139. return ret;
  140. }
  141. /* -----------------------------------------------------------------------------
  142. * V4L2 Subdevice Operations
  143. */
  144. static const struct v4l2_subdev_pad_ops uif_pad_ops = {
  145. .init_cfg = vsp1_entity_init_cfg,
  146. .enum_mbus_code = uif_enum_mbus_code,
  147. .enum_frame_size = uif_enum_frame_size,
  148. .get_fmt = vsp1_subdev_get_pad_format,
  149. .set_fmt = uif_set_format,
  150. .get_selection = uif_get_selection,
  151. .set_selection = uif_set_selection,
  152. };
  153. static const struct v4l2_subdev_ops uif_ops = {
  154. .pad = &uif_pad_ops,
  155. };
  156. /* -----------------------------------------------------------------------------
  157. * VSP1 Entity Operations
  158. */
  159. static void uif_configure_stream(struct vsp1_entity *entity,
  160. struct vsp1_pipeline *pipe,
  161. struct vsp1_dl_body *dlb)
  162. {
  163. struct vsp1_uif *uif = to_uif(&entity->subdev);
  164. const struct v4l2_rect *crop;
  165. unsigned int left;
  166. unsigned int width;
  167. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMPMR,
  168. VI6_UIF_DISCOM_DOCMPMR_SEL(9));
  169. crop = vsp1_entity_get_pad_selection(entity, entity->config,
  170. UIF_PAD_SINK, V4L2_SEL_TGT_CROP);
  171. left = crop->left;
  172. width = crop->width;
  173. /* On M3-W the horizontal coordinates are twice the register value. */
  174. if (uif->m3w_quirk) {
  175. left /= 2;
  176. width /= 2;
  177. }
  178. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPXR, left);
  179. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPYR, crop->top);
  180. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZXR, width);
  181. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZYR, crop->height);
  182. vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMCR,
  183. VI6_UIF_DISCOM_DOCMCR_CMPR);
  184. }
  185. static const struct vsp1_entity_operations uif_entity_ops = {
  186. .configure_stream = uif_configure_stream,
  187. };
  188. /* -----------------------------------------------------------------------------
  189. * Initialization and Cleanup
  190. */
  191. static const struct soc_device_attribute vsp1_r8a7796[] = {
  192. { .soc_id = "r8a7796" },
  193. { /* sentinel */ }
  194. };
  195. struct vsp1_uif *vsp1_uif_create(struct vsp1_device *vsp1, unsigned int index)
  196. {
  197. struct vsp1_uif *uif;
  198. char name[6];
  199. int ret;
  200. uif = devm_kzalloc(vsp1->dev, sizeof(*uif), GFP_KERNEL);
  201. if (!uif)
  202. return ERR_PTR(-ENOMEM);
  203. if (soc_device_match(vsp1_r8a7796))
  204. uif->m3w_quirk = true;
  205. uif->entity.ops = &uif_entity_ops;
  206. uif->entity.type = VSP1_ENTITY_UIF;
  207. uif->entity.index = index;
  208. /* The datasheet names the two UIF instances UIF4 and UIF5. */
  209. sprintf(name, "uif.%u", index + 4);
  210. ret = vsp1_entity_init(vsp1, &uif->entity, name, 2, &uif_ops,
  211. MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
  212. if (ret < 0)
  213. return ERR_PTR(ret);
  214. return uif;
  215. }