vsp1_lut.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_dl.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 void vsp1_lut_write(struct vsp1_lut *lut, struct vsp1_dl_list *dl,
  25. u32 reg, u32 data)
  26. {
  27. vsp1_dl_list_write(dl, reg, data);
  28. }
  29. /* -----------------------------------------------------------------------------
  30. * Controls
  31. */
  32. #define V4L2_CID_VSP1_LUT_TABLE (V4L2_CID_USER_BASE | 0x1001)
  33. static int lut_set_table(struct vsp1_lut *lut, struct v4l2_ctrl *ctrl)
  34. {
  35. struct vsp1_dl_body *dlb;
  36. unsigned int i;
  37. dlb = vsp1_dl_fragment_alloc(lut->entity.vsp1, 256);
  38. if (!dlb)
  39. return -ENOMEM;
  40. for (i = 0; i < 256; ++i)
  41. vsp1_dl_fragment_write(dlb, VI6_LUT_TABLE + 4 * i,
  42. ctrl->p_new.p_u32[i]);
  43. spin_lock_irq(&lut->lock);
  44. swap(lut->lut, dlb);
  45. spin_unlock_irq(&lut->lock);
  46. vsp1_dl_fragment_free(dlb);
  47. return 0;
  48. }
  49. static int lut_s_ctrl(struct v4l2_ctrl *ctrl)
  50. {
  51. struct vsp1_lut *lut =
  52. container_of(ctrl->handler, struct vsp1_lut, ctrls);
  53. switch (ctrl->id) {
  54. case V4L2_CID_VSP1_LUT_TABLE:
  55. lut_set_table(lut, ctrl);
  56. break;
  57. }
  58. return 0;
  59. }
  60. static const struct v4l2_ctrl_ops lut_ctrl_ops = {
  61. .s_ctrl = lut_s_ctrl,
  62. };
  63. static const struct v4l2_ctrl_config lut_table_control = {
  64. .ops = &lut_ctrl_ops,
  65. .id = V4L2_CID_VSP1_LUT_TABLE,
  66. .name = "Look-Up Table",
  67. .type = V4L2_CTRL_TYPE_U32,
  68. .min = 0x00000000,
  69. .max = 0x00ffffff,
  70. .step = 1,
  71. .def = 0,
  72. .dims = { 256},
  73. };
  74. /* -----------------------------------------------------------------------------
  75. * V4L2 Subdevice Pad Operations
  76. */
  77. static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
  78. struct v4l2_subdev_pad_config *cfg,
  79. struct v4l2_subdev_mbus_code_enum *code)
  80. {
  81. static const unsigned int codes[] = {
  82. MEDIA_BUS_FMT_ARGB8888_1X32,
  83. MEDIA_BUS_FMT_AHSV8888_1X32,
  84. MEDIA_BUS_FMT_AYUV8_1X32,
  85. };
  86. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  87. ARRAY_SIZE(codes));
  88. }
  89. static int lut_enum_frame_size(struct v4l2_subdev *subdev,
  90. struct v4l2_subdev_pad_config *cfg,
  91. struct v4l2_subdev_frame_size_enum *fse)
  92. {
  93. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LUT_MIN_SIZE,
  94. LUT_MIN_SIZE, LUT_MAX_SIZE,
  95. LUT_MAX_SIZE);
  96. }
  97. static int lut_set_format(struct v4l2_subdev *subdev,
  98. struct v4l2_subdev_pad_config *cfg,
  99. struct v4l2_subdev_format *fmt)
  100. {
  101. struct vsp1_lut *lut = to_lut(subdev);
  102. struct v4l2_subdev_pad_config *config;
  103. struct v4l2_mbus_framefmt *format;
  104. int ret = 0;
  105. mutex_lock(&lut->entity.lock);
  106. config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
  107. if (!config) {
  108. ret = -EINVAL;
  109. goto done;
  110. }
  111. /* Default to YUV if the requested format is not supported. */
  112. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  113. fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
  114. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  115. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  116. format = vsp1_entity_get_pad_format(&lut->entity, config, fmt->pad);
  117. if (fmt->pad == LUT_PAD_SOURCE) {
  118. /* The LUT output format can't be modified. */
  119. fmt->format = *format;
  120. goto done;
  121. }
  122. format->code = fmt->format.code;
  123. format->width = clamp_t(unsigned int, fmt->format.width,
  124. LUT_MIN_SIZE, LUT_MAX_SIZE);
  125. format->height = clamp_t(unsigned int, fmt->format.height,
  126. LUT_MIN_SIZE, LUT_MAX_SIZE);
  127. format->field = V4L2_FIELD_NONE;
  128. format->colorspace = V4L2_COLORSPACE_SRGB;
  129. fmt->format = *format;
  130. /* Propagate the format to the source pad. */
  131. format = vsp1_entity_get_pad_format(&lut->entity, config,
  132. LUT_PAD_SOURCE);
  133. *format = fmt->format;
  134. done:
  135. mutex_unlock(&lut->entity.lock);
  136. return ret;
  137. }
  138. /* -----------------------------------------------------------------------------
  139. * V4L2 Subdevice Operations
  140. */
  141. static const struct v4l2_subdev_pad_ops lut_pad_ops = {
  142. .init_cfg = vsp1_entity_init_cfg,
  143. .enum_mbus_code = lut_enum_mbus_code,
  144. .enum_frame_size = lut_enum_frame_size,
  145. .get_fmt = vsp1_subdev_get_pad_format,
  146. .set_fmt = lut_set_format,
  147. };
  148. static const struct v4l2_subdev_ops lut_ops = {
  149. .pad = &lut_pad_ops,
  150. };
  151. /* -----------------------------------------------------------------------------
  152. * VSP1 Entity Operations
  153. */
  154. static void lut_configure(struct vsp1_entity *entity,
  155. struct vsp1_pipeline *pipe,
  156. struct vsp1_dl_list *dl,
  157. enum vsp1_entity_params params)
  158. {
  159. struct vsp1_lut *lut = to_lut(&entity->subdev);
  160. struct vsp1_dl_body *dlb;
  161. unsigned long flags;
  162. switch (params) {
  163. case VSP1_ENTITY_PARAMS_INIT:
  164. vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
  165. break;
  166. case VSP1_ENTITY_PARAMS_PARTITION:
  167. break;
  168. case VSP1_ENTITY_PARAMS_RUNTIME:
  169. spin_lock_irqsave(&lut->lock, flags);
  170. dlb = lut->lut;
  171. lut->lut = NULL;
  172. spin_unlock_irqrestore(&lut->lock, flags);
  173. if (dlb)
  174. vsp1_dl_list_add_fragment(dl, dlb);
  175. break;
  176. }
  177. }
  178. static const struct vsp1_entity_operations lut_entity_ops = {
  179. .configure = lut_configure,
  180. };
  181. /* -----------------------------------------------------------------------------
  182. * Initialization and Cleanup
  183. */
  184. struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
  185. {
  186. struct vsp1_lut *lut;
  187. int ret;
  188. lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
  189. if (lut == NULL)
  190. return ERR_PTR(-ENOMEM);
  191. spin_lock_init(&lut->lock);
  192. lut->entity.ops = &lut_entity_ops;
  193. lut->entity.type = VSP1_ENTITY_LUT;
  194. ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops,
  195. MEDIA_ENT_F_PROC_VIDEO_LUT);
  196. if (ret < 0)
  197. return ERR_PTR(ret);
  198. /* Initialize the control handler. */
  199. v4l2_ctrl_handler_init(&lut->ctrls, 1);
  200. v4l2_ctrl_new_custom(&lut->ctrls, &lut_table_control, NULL);
  201. lut->entity.subdev.ctrl_handler = &lut->ctrls;
  202. if (lut->ctrls.error) {
  203. dev_err(vsp1->dev, "lut: failed to initialize controls\n");
  204. ret = lut->ctrls.error;
  205. vsp1_entity_destroy(&lut->entity);
  206. return ERR_PTR(ret);
  207. }
  208. v4l2_ctrl_handler_setup(&lut->ctrls);
  209. return lut;
  210. }