vsp1_lut.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * vsp1_lut.c -- R-Car VSP1 Look-Up Table
  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 <linux/vsp1.h>
  16. #include <media/v4l2-subdev.h>
  17. #include "vsp1.h"
  18. #include "vsp1_lut.h"
  19. #define LUT_MIN_SIZE 4U
  20. #define LUT_MAX_SIZE 8190U
  21. /* -----------------------------------------------------------------------------
  22. * Device Access
  23. */
  24. static inline u32 vsp1_lut_read(struct vsp1_lut *lut, u32 reg)
  25. {
  26. return vsp1_read(lut->entity.vsp1, reg);
  27. }
  28. static inline void vsp1_lut_write(struct vsp1_lut *lut, u32 reg, u32 data)
  29. {
  30. vsp1_write(lut->entity.vsp1, reg, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * V4L2 Subdevice Core Operations
  34. */
  35. static void lut_configure(struct vsp1_lut *lut, struct vsp1_lut_config *config)
  36. {
  37. memcpy_toio(lut->entity.vsp1->mmio + VI6_LUT_TABLE, config->lut,
  38. sizeof(config->lut));
  39. }
  40. static long lut_ioctl(struct v4l2_subdev *subdev, unsigned int cmd, void *arg)
  41. {
  42. struct vsp1_lut *lut = to_lut(subdev);
  43. switch (cmd) {
  44. case VIDIOC_VSP1_LUT_CONFIG:
  45. lut_configure(lut, arg);
  46. return 0;
  47. default:
  48. return -ENOIOCTLCMD;
  49. }
  50. }
  51. /* -----------------------------------------------------------------------------
  52. * V4L2 Subdevice Video Operations
  53. */
  54. static int lut_s_stream(struct v4l2_subdev *subdev, int enable)
  55. {
  56. struct vsp1_lut *lut = to_lut(subdev);
  57. if (!enable)
  58. return 0;
  59. vsp1_lut_write(lut, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
  60. return 0;
  61. }
  62. /* -----------------------------------------------------------------------------
  63. * V4L2 Subdevice Pad Operations
  64. */
  65. static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
  66. struct v4l2_subdev_fh *fh,
  67. struct v4l2_subdev_mbus_code_enum *code)
  68. {
  69. static const unsigned int codes[] = {
  70. V4L2_MBUS_FMT_ARGB8888_1X32,
  71. V4L2_MBUS_FMT_AHSV8888_1X32,
  72. V4L2_MBUS_FMT_AYUV8_1X32,
  73. };
  74. struct v4l2_mbus_framefmt *format;
  75. if (code->pad == LUT_PAD_SINK) {
  76. if (code->index >= ARRAY_SIZE(codes))
  77. return -EINVAL;
  78. code->code = codes[code->index];
  79. } else {
  80. /* The LUT can't perform format conversion, the sink format is
  81. * always identical to the source format.
  82. */
  83. if (code->index)
  84. return -EINVAL;
  85. format = v4l2_subdev_get_try_format(fh, LUT_PAD_SINK);
  86. code->code = format->code;
  87. }
  88. return 0;
  89. }
  90. static int lut_enum_frame_size(struct v4l2_subdev *subdev,
  91. struct v4l2_subdev_fh *fh,
  92. struct v4l2_subdev_frame_size_enum *fse)
  93. {
  94. struct v4l2_mbus_framefmt *format;
  95. format = v4l2_subdev_get_try_format(fh, fse->pad);
  96. if (fse->index || fse->code != format->code)
  97. return -EINVAL;
  98. if (fse->pad == LUT_PAD_SINK) {
  99. fse->min_width = LUT_MIN_SIZE;
  100. fse->max_width = LUT_MAX_SIZE;
  101. fse->min_height = LUT_MIN_SIZE;
  102. fse->max_height = LUT_MAX_SIZE;
  103. } else {
  104. /* The size on the source pad are fixed and always identical to
  105. * the size on the sink pad.
  106. */
  107. fse->min_width = format->width;
  108. fse->max_width = format->width;
  109. fse->min_height = format->height;
  110. fse->max_height = format->height;
  111. }
  112. return 0;
  113. }
  114. static int lut_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  115. struct v4l2_subdev_format *fmt)
  116. {
  117. struct vsp1_lut *lut = to_lut(subdev);
  118. fmt->format = *vsp1_entity_get_pad_format(&lut->entity, fh, fmt->pad,
  119. fmt->which);
  120. return 0;
  121. }
  122. static int lut_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  123. struct v4l2_subdev_format *fmt)
  124. {
  125. struct vsp1_lut *lut = to_lut(subdev);
  126. struct v4l2_mbus_framefmt *format;
  127. /* Default to YUV if the requested format is not supported. */
  128. if (fmt->format.code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  129. fmt->format.code != V4L2_MBUS_FMT_AHSV8888_1X32 &&
  130. fmt->format.code != V4L2_MBUS_FMT_AYUV8_1X32)
  131. fmt->format.code = V4L2_MBUS_FMT_AYUV8_1X32;
  132. format = vsp1_entity_get_pad_format(&lut->entity, fh, fmt->pad,
  133. fmt->which);
  134. if (fmt->pad == LUT_PAD_SOURCE) {
  135. /* The LUT output format can't be modified. */
  136. fmt->format = *format;
  137. return 0;
  138. }
  139. format->width = clamp_t(unsigned int, fmt->format.width,
  140. LUT_MIN_SIZE, LUT_MAX_SIZE);
  141. format->height = clamp_t(unsigned int, fmt->format.height,
  142. LUT_MIN_SIZE, LUT_MAX_SIZE);
  143. format->field = V4L2_FIELD_NONE;
  144. format->colorspace = V4L2_COLORSPACE_SRGB;
  145. fmt->format = *format;
  146. /* Propagate the format to the source pad. */
  147. format = vsp1_entity_get_pad_format(&lut->entity, fh, LUT_PAD_SOURCE,
  148. fmt->which);
  149. *format = fmt->format;
  150. return 0;
  151. }
  152. /* -----------------------------------------------------------------------------
  153. * V4L2 Subdevice Operations
  154. */
  155. static struct v4l2_subdev_core_ops lut_core_ops = {
  156. .ioctl = lut_ioctl,
  157. };
  158. static struct v4l2_subdev_video_ops lut_video_ops = {
  159. .s_stream = lut_s_stream,
  160. };
  161. static struct v4l2_subdev_pad_ops lut_pad_ops = {
  162. .enum_mbus_code = lut_enum_mbus_code,
  163. .enum_frame_size = lut_enum_frame_size,
  164. .get_fmt = lut_get_format,
  165. .set_fmt = lut_set_format,
  166. };
  167. static struct v4l2_subdev_ops lut_ops = {
  168. .core = &lut_core_ops,
  169. .video = &lut_video_ops,
  170. .pad = &lut_pad_ops,
  171. };
  172. /* -----------------------------------------------------------------------------
  173. * Initialization and Cleanup
  174. */
  175. struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
  176. {
  177. struct v4l2_subdev *subdev;
  178. struct vsp1_lut *lut;
  179. int ret;
  180. lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
  181. if (lut == NULL)
  182. return ERR_PTR(-ENOMEM);
  183. lut->entity.type = VSP1_ENTITY_LUT;
  184. ret = vsp1_entity_init(vsp1, &lut->entity, 2);
  185. if (ret < 0)
  186. return ERR_PTR(ret);
  187. /* Initialize the V4L2 subdev. */
  188. subdev = &lut->entity.subdev;
  189. v4l2_subdev_init(subdev, &lut_ops);
  190. subdev->entity.ops = &vsp1_media_ops;
  191. subdev->internal_ops = &vsp1_subdev_internal_ops;
  192. snprintf(subdev->name, sizeof(subdev->name), "%s lut",
  193. dev_name(vsp1->dev));
  194. v4l2_set_subdevdata(subdev, lut);
  195. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  196. vsp1_entity_init_formats(subdev, NULL);
  197. return lut;
  198. }