vsp1_hsit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * vsp1_hsit.c -- R-Car VSP1 Hue Saturation value (Inverse) Transform
  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/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_hsit.h"
  18. #define HSIT_MIN_SIZE 4U
  19. #define HSIT_MAX_SIZE 8190U
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline void vsp1_hsit_write(struct vsp1_hsit *hsit, u32 reg, u32 data)
  24. {
  25. vsp1_write(hsit->entity.vsp1, reg, data);
  26. }
  27. /* -----------------------------------------------------------------------------
  28. * V4L2 Subdevice Core Operations
  29. */
  30. static int hsit_s_stream(struct v4l2_subdev *subdev, int enable)
  31. {
  32. struct vsp1_hsit *hsit = to_hsit(subdev);
  33. if (!enable)
  34. return 0;
  35. if (hsit->inverse)
  36. vsp1_hsit_write(hsit, VI6_HSI_CTRL, VI6_HSI_CTRL_EN);
  37. else
  38. vsp1_hsit_write(hsit, VI6_HST_CTRL, VI6_HST_CTRL_EN);
  39. return 0;
  40. }
  41. /* -----------------------------------------------------------------------------
  42. * V4L2 Subdevice Pad Operations
  43. */
  44. static int hsit_enum_mbus_code(struct v4l2_subdev *subdev,
  45. struct v4l2_subdev_pad_config *cfg,
  46. struct v4l2_subdev_mbus_code_enum *code)
  47. {
  48. struct vsp1_hsit *hsit = to_hsit(subdev);
  49. if (code->index > 0)
  50. return -EINVAL;
  51. if ((code->pad == HSIT_PAD_SINK && !hsit->inverse) |
  52. (code->pad == HSIT_PAD_SOURCE && hsit->inverse))
  53. code->code = MEDIA_BUS_FMT_ARGB8888_1X32;
  54. else
  55. code->code = MEDIA_BUS_FMT_AHSV8888_1X32;
  56. return 0;
  57. }
  58. static int hsit_enum_frame_size(struct v4l2_subdev *subdev,
  59. struct v4l2_subdev_pad_config *cfg,
  60. struct v4l2_subdev_frame_size_enum *fse)
  61. {
  62. struct vsp1_hsit *hsit = to_hsit(subdev);
  63. struct v4l2_mbus_framefmt *format;
  64. format = vsp1_entity_get_pad_format(&hsit->entity, cfg, fse->pad,
  65. fse->which);
  66. if (fse->index || fse->code != format->code)
  67. return -EINVAL;
  68. if (fse->pad == HSIT_PAD_SINK) {
  69. fse->min_width = HSIT_MIN_SIZE;
  70. fse->max_width = HSIT_MAX_SIZE;
  71. fse->min_height = HSIT_MIN_SIZE;
  72. fse->max_height = HSIT_MAX_SIZE;
  73. } else {
  74. /* The size on the source pad are fixed and always identical to
  75. * the size on the sink pad.
  76. */
  77. fse->min_width = format->width;
  78. fse->max_width = format->width;
  79. fse->min_height = format->height;
  80. fse->max_height = format->height;
  81. }
  82. return 0;
  83. }
  84. static int hsit_get_format(struct v4l2_subdev *subdev,
  85. struct v4l2_subdev_pad_config *cfg,
  86. struct v4l2_subdev_format *fmt)
  87. {
  88. struct vsp1_hsit *hsit = to_hsit(subdev);
  89. fmt->format = *vsp1_entity_get_pad_format(&hsit->entity, cfg, fmt->pad,
  90. fmt->which);
  91. return 0;
  92. }
  93. static int hsit_set_format(struct v4l2_subdev *subdev,
  94. struct v4l2_subdev_pad_config *cfg,
  95. struct v4l2_subdev_format *fmt)
  96. {
  97. struct vsp1_hsit *hsit = to_hsit(subdev);
  98. struct v4l2_mbus_framefmt *format;
  99. format = vsp1_entity_get_pad_format(&hsit->entity, cfg, fmt->pad,
  100. fmt->which);
  101. if (fmt->pad == HSIT_PAD_SOURCE) {
  102. /* The HST and HSI output format code and resolution can't be
  103. * modified.
  104. */
  105. fmt->format = *format;
  106. return 0;
  107. }
  108. format->code = hsit->inverse ? MEDIA_BUS_FMT_AHSV8888_1X32
  109. : MEDIA_BUS_FMT_ARGB8888_1X32;
  110. format->width = clamp_t(unsigned int, fmt->format.width,
  111. HSIT_MIN_SIZE, HSIT_MAX_SIZE);
  112. format->height = clamp_t(unsigned int, fmt->format.height,
  113. HSIT_MIN_SIZE, HSIT_MAX_SIZE);
  114. format->field = V4L2_FIELD_NONE;
  115. format->colorspace = V4L2_COLORSPACE_SRGB;
  116. fmt->format = *format;
  117. /* Propagate the format to the source pad. */
  118. format = vsp1_entity_get_pad_format(&hsit->entity, cfg, HSIT_PAD_SOURCE,
  119. fmt->which);
  120. *format = fmt->format;
  121. format->code = hsit->inverse ? MEDIA_BUS_FMT_ARGB8888_1X32
  122. : MEDIA_BUS_FMT_AHSV8888_1X32;
  123. return 0;
  124. }
  125. /* -----------------------------------------------------------------------------
  126. * V4L2 Subdevice Operations
  127. */
  128. static struct v4l2_subdev_video_ops hsit_video_ops = {
  129. .s_stream = hsit_s_stream,
  130. };
  131. static struct v4l2_subdev_pad_ops hsit_pad_ops = {
  132. .enum_mbus_code = hsit_enum_mbus_code,
  133. .enum_frame_size = hsit_enum_frame_size,
  134. .get_fmt = hsit_get_format,
  135. .set_fmt = hsit_set_format,
  136. };
  137. static struct v4l2_subdev_ops hsit_ops = {
  138. .video = &hsit_video_ops,
  139. .pad = &hsit_pad_ops,
  140. };
  141. /* -----------------------------------------------------------------------------
  142. * Initialization and Cleanup
  143. */
  144. struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
  145. {
  146. struct v4l2_subdev *subdev;
  147. struct vsp1_hsit *hsit;
  148. int ret;
  149. hsit = devm_kzalloc(vsp1->dev, sizeof(*hsit), GFP_KERNEL);
  150. if (hsit == NULL)
  151. return ERR_PTR(-ENOMEM);
  152. hsit->inverse = inverse;
  153. if (inverse)
  154. hsit->entity.type = VSP1_ENTITY_HSI;
  155. else
  156. hsit->entity.type = VSP1_ENTITY_HST;
  157. ret = vsp1_entity_init(vsp1, &hsit->entity, 2);
  158. if (ret < 0)
  159. return ERR_PTR(ret);
  160. /* Initialize the V4L2 subdev. */
  161. subdev = &hsit->entity.subdev;
  162. v4l2_subdev_init(subdev, &hsit_ops);
  163. subdev->entity.ops = &vsp1_media_ops;
  164. subdev->internal_ops = &vsp1_subdev_internal_ops;
  165. snprintf(subdev->name, sizeof(subdev->name), "%s %s",
  166. dev_name(vsp1->dev), inverse ? "hsi" : "hst");
  167. v4l2_set_subdevdata(subdev, hsit);
  168. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  169. vsp1_entity_init_formats(subdev, NULL);
  170. return hsit;
  171. }