vsp1_lut.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_pad_config *cfg,
  67. struct v4l2_subdev_mbus_code_enum *code)
  68. {
  69. static const unsigned int codes[] = {
  70. MEDIA_BUS_FMT_ARGB8888_1X32,
  71. MEDIA_BUS_FMT_AHSV8888_1X32,
  72. MEDIA_BUS_FMT_AYUV8_1X32,
  73. };
  74. struct vsp1_lut *lut = to_lut(subdev);
  75. struct v4l2_mbus_framefmt *format;
  76. if (code->pad == LUT_PAD_SINK) {
  77. if (code->index >= ARRAY_SIZE(codes))
  78. return -EINVAL;
  79. code->code = codes[code->index];
  80. } else {
  81. /* The LUT can't perform format conversion, the sink format is
  82. * always identical to the source format.
  83. */
  84. if (code->index)
  85. return -EINVAL;
  86. format = vsp1_entity_get_pad_format(&lut->entity, cfg,
  87. LUT_PAD_SINK, code->which);
  88. code->code = format->code;
  89. }
  90. return 0;
  91. }
  92. static int lut_enum_frame_size(struct v4l2_subdev *subdev,
  93. struct v4l2_subdev_pad_config *cfg,
  94. struct v4l2_subdev_frame_size_enum *fse)
  95. {
  96. struct vsp1_lut *lut = to_lut(subdev);
  97. struct v4l2_mbus_framefmt *format;
  98. format = vsp1_entity_get_pad_format(&lut->entity, cfg,
  99. fse->pad, fse->which);
  100. if (fse->index || fse->code != format->code)
  101. return -EINVAL;
  102. if (fse->pad == LUT_PAD_SINK) {
  103. fse->min_width = LUT_MIN_SIZE;
  104. fse->max_width = LUT_MAX_SIZE;
  105. fse->min_height = LUT_MIN_SIZE;
  106. fse->max_height = LUT_MAX_SIZE;
  107. } else {
  108. /* The size on the source pad are fixed and always identical to
  109. * the size on the sink pad.
  110. */
  111. fse->min_width = format->width;
  112. fse->max_width = format->width;
  113. fse->min_height = format->height;
  114. fse->max_height = format->height;
  115. }
  116. return 0;
  117. }
  118. static int lut_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  119. struct v4l2_subdev_format *fmt)
  120. {
  121. struct vsp1_lut *lut = to_lut(subdev);
  122. fmt->format = *vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
  123. fmt->which);
  124. return 0;
  125. }
  126. static int lut_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  127. struct v4l2_subdev_format *fmt)
  128. {
  129. struct vsp1_lut *lut = to_lut(subdev);
  130. struct v4l2_mbus_framefmt *format;
  131. /* Default to YUV if the requested format is not supported. */
  132. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  133. fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
  134. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  135. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  136. format = vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
  137. fmt->which);
  138. if (fmt->pad == LUT_PAD_SOURCE) {
  139. /* The LUT output format can't be modified. */
  140. fmt->format = *format;
  141. return 0;
  142. }
  143. format->width = clamp_t(unsigned int, fmt->format.width,
  144. LUT_MIN_SIZE, LUT_MAX_SIZE);
  145. format->height = clamp_t(unsigned int, fmt->format.height,
  146. LUT_MIN_SIZE, LUT_MAX_SIZE);
  147. format->field = V4L2_FIELD_NONE;
  148. format->colorspace = V4L2_COLORSPACE_SRGB;
  149. fmt->format = *format;
  150. /* Propagate the format to the source pad. */
  151. format = vsp1_entity_get_pad_format(&lut->entity, cfg, LUT_PAD_SOURCE,
  152. fmt->which);
  153. *format = fmt->format;
  154. return 0;
  155. }
  156. /* -----------------------------------------------------------------------------
  157. * V4L2 Subdevice Operations
  158. */
  159. static struct v4l2_subdev_core_ops lut_core_ops = {
  160. .ioctl = lut_ioctl,
  161. };
  162. static struct v4l2_subdev_video_ops lut_video_ops = {
  163. .s_stream = lut_s_stream,
  164. };
  165. static struct v4l2_subdev_pad_ops lut_pad_ops = {
  166. .enum_mbus_code = lut_enum_mbus_code,
  167. .enum_frame_size = lut_enum_frame_size,
  168. .get_fmt = lut_get_format,
  169. .set_fmt = lut_set_format,
  170. };
  171. static struct v4l2_subdev_ops lut_ops = {
  172. .core = &lut_core_ops,
  173. .video = &lut_video_ops,
  174. .pad = &lut_pad_ops,
  175. };
  176. /* -----------------------------------------------------------------------------
  177. * Initialization and Cleanup
  178. */
  179. struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
  180. {
  181. struct v4l2_subdev *subdev;
  182. struct vsp1_lut *lut;
  183. int ret;
  184. lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
  185. if (lut == NULL)
  186. return ERR_PTR(-ENOMEM);
  187. lut->entity.type = VSP1_ENTITY_LUT;
  188. ret = vsp1_entity_init(vsp1, &lut->entity, 2);
  189. if (ret < 0)
  190. return ERR_PTR(ret);
  191. /* Initialize the V4L2 subdev. */
  192. subdev = &lut->entity.subdev;
  193. v4l2_subdev_init(subdev, &lut_ops);
  194. subdev->entity.ops = &vsp1_media_ops;
  195. subdev->internal_ops = &vsp1_subdev_internal_ops;
  196. snprintf(subdev->name, sizeof(subdev->name), "%s lut",
  197. dev_name(vsp1->dev));
  198. v4l2_set_subdevdata(subdev, lut);
  199. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  200. vsp1_entity_init_formats(subdev, NULL);
  201. return lut;
  202. }