vsp1_hsit.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_hsit.c -- R-Car VSP1 Hue Saturation value (Inverse) Transform
  4. *
  5. * Copyright (C) 2013 Renesas Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gfp.h>
  11. #include <media/v4l2-subdev.h>
  12. #include "vsp1.h"
  13. #include "vsp1_dl.h"
  14. #include "vsp1_hsit.h"
  15. #define HSIT_MIN_SIZE 4U
  16. #define HSIT_MAX_SIZE 8190U
  17. /* -----------------------------------------------------------------------------
  18. * Device Access
  19. */
  20. static inline void vsp1_hsit_write(struct vsp1_hsit *hsit,
  21. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  22. {
  23. vsp1_dl_body_write(dlb, reg, data);
  24. }
  25. /* -----------------------------------------------------------------------------
  26. * V4L2 Subdevice Operations
  27. */
  28. static int hsit_enum_mbus_code(struct v4l2_subdev *subdev,
  29. struct v4l2_subdev_pad_config *cfg,
  30. struct v4l2_subdev_mbus_code_enum *code)
  31. {
  32. struct vsp1_hsit *hsit = to_hsit(subdev);
  33. if (code->index > 0)
  34. return -EINVAL;
  35. if ((code->pad == HSIT_PAD_SINK && !hsit->inverse) |
  36. (code->pad == HSIT_PAD_SOURCE && hsit->inverse))
  37. code->code = MEDIA_BUS_FMT_ARGB8888_1X32;
  38. else
  39. code->code = MEDIA_BUS_FMT_AHSV8888_1X32;
  40. return 0;
  41. }
  42. static int hsit_enum_frame_size(struct v4l2_subdev *subdev,
  43. struct v4l2_subdev_pad_config *cfg,
  44. struct v4l2_subdev_frame_size_enum *fse)
  45. {
  46. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, HSIT_MIN_SIZE,
  47. HSIT_MIN_SIZE, HSIT_MAX_SIZE,
  48. HSIT_MAX_SIZE);
  49. }
  50. static int hsit_set_format(struct v4l2_subdev *subdev,
  51. struct v4l2_subdev_pad_config *cfg,
  52. struct v4l2_subdev_format *fmt)
  53. {
  54. struct vsp1_hsit *hsit = to_hsit(subdev);
  55. struct v4l2_subdev_pad_config *config;
  56. struct v4l2_mbus_framefmt *format;
  57. int ret = 0;
  58. mutex_lock(&hsit->entity.lock);
  59. config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fmt->which);
  60. if (!config) {
  61. ret = -EINVAL;
  62. goto done;
  63. }
  64. format = vsp1_entity_get_pad_format(&hsit->entity, config, fmt->pad);
  65. if (fmt->pad == HSIT_PAD_SOURCE) {
  66. /*
  67. * The HST and HSI output format code and resolution can't be
  68. * modified.
  69. */
  70. fmt->format = *format;
  71. goto done;
  72. }
  73. format->code = hsit->inverse ? MEDIA_BUS_FMT_AHSV8888_1X32
  74. : MEDIA_BUS_FMT_ARGB8888_1X32;
  75. format->width = clamp_t(unsigned int, fmt->format.width,
  76. HSIT_MIN_SIZE, HSIT_MAX_SIZE);
  77. format->height = clamp_t(unsigned int, fmt->format.height,
  78. HSIT_MIN_SIZE, HSIT_MAX_SIZE);
  79. format->field = V4L2_FIELD_NONE;
  80. format->colorspace = V4L2_COLORSPACE_SRGB;
  81. fmt->format = *format;
  82. /* Propagate the format to the source pad. */
  83. format = vsp1_entity_get_pad_format(&hsit->entity, config,
  84. HSIT_PAD_SOURCE);
  85. *format = fmt->format;
  86. format->code = hsit->inverse ? MEDIA_BUS_FMT_ARGB8888_1X32
  87. : MEDIA_BUS_FMT_AHSV8888_1X32;
  88. done:
  89. mutex_unlock(&hsit->entity.lock);
  90. return ret;
  91. }
  92. static const struct v4l2_subdev_pad_ops hsit_pad_ops = {
  93. .init_cfg = vsp1_entity_init_cfg,
  94. .enum_mbus_code = hsit_enum_mbus_code,
  95. .enum_frame_size = hsit_enum_frame_size,
  96. .get_fmt = vsp1_subdev_get_pad_format,
  97. .set_fmt = hsit_set_format,
  98. };
  99. static const struct v4l2_subdev_ops hsit_ops = {
  100. .pad = &hsit_pad_ops,
  101. };
  102. /* -----------------------------------------------------------------------------
  103. * VSP1 Entity Operations
  104. */
  105. static void hsit_configure_stream(struct vsp1_entity *entity,
  106. struct vsp1_pipeline *pipe,
  107. struct vsp1_dl_body *dlb)
  108. {
  109. struct vsp1_hsit *hsit = to_hsit(&entity->subdev);
  110. if (hsit->inverse)
  111. vsp1_hsit_write(hsit, dlb, VI6_HSI_CTRL, VI6_HSI_CTRL_EN);
  112. else
  113. vsp1_hsit_write(hsit, dlb, VI6_HST_CTRL, VI6_HST_CTRL_EN);
  114. }
  115. static const struct vsp1_entity_operations hsit_entity_ops = {
  116. .configure_stream = hsit_configure_stream,
  117. };
  118. /* -----------------------------------------------------------------------------
  119. * Initialization and Cleanup
  120. */
  121. struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
  122. {
  123. struct vsp1_hsit *hsit;
  124. int ret;
  125. hsit = devm_kzalloc(vsp1->dev, sizeof(*hsit), GFP_KERNEL);
  126. if (hsit == NULL)
  127. return ERR_PTR(-ENOMEM);
  128. hsit->inverse = inverse;
  129. hsit->entity.ops = &hsit_entity_ops;
  130. if (inverse)
  131. hsit->entity.type = VSP1_ENTITY_HSI;
  132. else
  133. hsit->entity.type = VSP1_ENTITY_HST;
  134. ret = vsp1_entity_init(vsp1, &hsit->entity, inverse ? "hsi" : "hst",
  135. 2, &hsit_ops,
  136. MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV);
  137. if (ret < 0)
  138. return ERR_PTR(ret);
  139. return hsit;
  140. }