vsp1_clu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table
  4. *
  5. * Copyright (C) 2015-2016 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/slab.h>
  11. #include <media/v4l2-subdev.h>
  12. #include "vsp1.h"
  13. #include "vsp1_clu.h"
  14. #include "vsp1_dl.h"
  15. #define CLU_MIN_SIZE 4U
  16. #define CLU_MAX_SIZE 8190U
  17. #define CLU_SIZE (17 * 17 * 17)
  18. /* -----------------------------------------------------------------------------
  19. * Device Access
  20. */
  21. static inline void vsp1_clu_write(struct vsp1_clu *clu,
  22. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  23. {
  24. vsp1_dl_body_write(dlb, reg, data);
  25. }
  26. /* -----------------------------------------------------------------------------
  27. * Controls
  28. */
  29. #define V4L2_CID_VSP1_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001)
  30. #define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002)
  31. #define V4L2_CID_VSP1_CLU_MODE_2D 0
  32. #define V4L2_CID_VSP1_CLU_MODE_3D 1
  33. static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl)
  34. {
  35. struct vsp1_dl_body *dlb;
  36. unsigned int i;
  37. dlb = vsp1_dl_body_get(clu->pool);
  38. if (!dlb)
  39. return -ENOMEM;
  40. vsp1_dl_body_write(dlb, VI6_CLU_ADDR, 0);
  41. for (i = 0; i < CLU_SIZE; ++i)
  42. vsp1_dl_body_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]);
  43. spin_lock_irq(&clu->lock);
  44. swap(clu->clu, dlb);
  45. spin_unlock_irq(&clu->lock);
  46. vsp1_dl_body_put(dlb);
  47. return 0;
  48. }
  49. static int clu_s_ctrl(struct v4l2_ctrl *ctrl)
  50. {
  51. struct vsp1_clu *clu =
  52. container_of(ctrl->handler, struct vsp1_clu, ctrls);
  53. switch (ctrl->id) {
  54. case V4L2_CID_VSP1_CLU_TABLE:
  55. clu_set_table(clu, ctrl);
  56. break;
  57. case V4L2_CID_VSP1_CLU_MODE:
  58. clu->mode = ctrl->val;
  59. break;
  60. }
  61. return 0;
  62. }
  63. static const struct v4l2_ctrl_ops clu_ctrl_ops = {
  64. .s_ctrl = clu_s_ctrl,
  65. };
  66. static const struct v4l2_ctrl_config clu_table_control = {
  67. .ops = &clu_ctrl_ops,
  68. .id = V4L2_CID_VSP1_CLU_TABLE,
  69. .name = "Look-Up Table",
  70. .type = V4L2_CTRL_TYPE_U32,
  71. .min = 0x00000000,
  72. .max = 0x00ffffff,
  73. .step = 1,
  74. .def = 0,
  75. .dims = { 17, 17, 17 },
  76. };
  77. static const char * const clu_mode_menu[] = {
  78. "2D",
  79. "3D",
  80. NULL,
  81. };
  82. static const struct v4l2_ctrl_config clu_mode_control = {
  83. .ops = &clu_ctrl_ops,
  84. .id = V4L2_CID_VSP1_CLU_MODE,
  85. .name = "Mode",
  86. .type = V4L2_CTRL_TYPE_MENU,
  87. .min = 0,
  88. .max = 1,
  89. .def = 1,
  90. .qmenu = clu_mode_menu,
  91. };
  92. /* -----------------------------------------------------------------------------
  93. * V4L2 Subdevice Pad Operations
  94. */
  95. static const unsigned int clu_codes[] = {
  96. MEDIA_BUS_FMT_ARGB8888_1X32,
  97. MEDIA_BUS_FMT_AHSV8888_1X32,
  98. MEDIA_BUS_FMT_AYUV8_1X32,
  99. };
  100. static int clu_enum_mbus_code(struct v4l2_subdev *subdev,
  101. struct v4l2_subdev_pad_config *cfg,
  102. struct v4l2_subdev_mbus_code_enum *code)
  103. {
  104. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, clu_codes,
  105. ARRAY_SIZE(clu_codes));
  106. }
  107. static int clu_enum_frame_size(struct v4l2_subdev *subdev,
  108. struct v4l2_subdev_pad_config *cfg,
  109. struct v4l2_subdev_frame_size_enum *fse)
  110. {
  111. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE,
  112. CLU_MIN_SIZE, CLU_MAX_SIZE,
  113. CLU_MAX_SIZE);
  114. }
  115. static int clu_set_format(struct v4l2_subdev *subdev,
  116. struct v4l2_subdev_pad_config *cfg,
  117. struct v4l2_subdev_format *fmt)
  118. {
  119. return vsp1_subdev_set_pad_format(subdev, cfg, fmt, clu_codes,
  120. ARRAY_SIZE(clu_codes),
  121. CLU_MIN_SIZE, CLU_MIN_SIZE,
  122. CLU_MAX_SIZE, CLU_MAX_SIZE);
  123. }
  124. /* -----------------------------------------------------------------------------
  125. * V4L2 Subdevice Operations
  126. */
  127. static const struct v4l2_subdev_pad_ops clu_pad_ops = {
  128. .init_cfg = vsp1_entity_init_cfg,
  129. .enum_mbus_code = clu_enum_mbus_code,
  130. .enum_frame_size = clu_enum_frame_size,
  131. .get_fmt = vsp1_subdev_get_pad_format,
  132. .set_fmt = clu_set_format,
  133. };
  134. static const struct v4l2_subdev_ops clu_ops = {
  135. .pad = &clu_pad_ops,
  136. };
  137. /* -----------------------------------------------------------------------------
  138. * VSP1 Entity Operations
  139. */
  140. static void clu_configure_stream(struct vsp1_entity *entity,
  141. struct vsp1_pipeline *pipe,
  142. struct vsp1_dl_body *dlb)
  143. {
  144. struct vsp1_clu *clu = to_clu(&entity->subdev);
  145. struct v4l2_mbus_framefmt *format;
  146. /*
  147. * The yuv_mode can't be changed during streaming. Cache it internally
  148. * for future runtime configuration calls.
  149. */
  150. format = vsp1_entity_get_pad_format(&clu->entity,
  151. clu->entity.config,
  152. CLU_PAD_SINK);
  153. clu->yuv_mode = format->code == MEDIA_BUS_FMT_AYUV8_1X32;
  154. }
  155. static void clu_configure_frame(struct vsp1_entity *entity,
  156. struct vsp1_pipeline *pipe,
  157. struct vsp1_dl_list *dl,
  158. struct vsp1_dl_body *dlb)
  159. {
  160. struct vsp1_clu *clu = to_clu(&entity->subdev);
  161. struct vsp1_dl_body *clu_dlb;
  162. unsigned long flags;
  163. u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN;
  164. /* 2D mode can only be used with the YCbCr pixel encoding. */
  165. if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D && clu->yuv_mode)
  166. ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D
  167. | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D
  168. | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D;
  169. vsp1_clu_write(clu, dlb, VI6_CLU_CTRL, ctrl);
  170. spin_lock_irqsave(&clu->lock, flags);
  171. clu_dlb = clu->clu;
  172. clu->clu = NULL;
  173. spin_unlock_irqrestore(&clu->lock, flags);
  174. if (clu_dlb) {
  175. vsp1_dl_list_add_body(dl, clu_dlb);
  176. /* Release our local reference. */
  177. vsp1_dl_body_put(clu_dlb);
  178. }
  179. }
  180. static void clu_destroy(struct vsp1_entity *entity)
  181. {
  182. struct vsp1_clu *clu = to_clu(&entity->subdev);
  183. vsp1_dl_body_pool_destroy(clu->pool);
  184. }
  185. static const struct vsp1_entity_operations clu_entity_ops = {
  186. .configure_stream = clu_configure_stream,
  187. .configure_frame = clu_configure_frame,
  188. .destroy = clu_destroy,
  189. };
  190. /* -----------------------------------------------------------------------------
  191. * Initialization and Cleanup
  192. */
  193. struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1)
  194. {
  195. struct vsp1_clu *clu;
  196. int ret;
  197. clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL);
  198. if (clu == NULL)
  199. return ERR_PTR(-ENOMEM);
  200. spin_lock_init(&clu->lock);
  201. clu->entity.ops = &clu_entity_ops;
  202. clu->entity.type = VSP1_ENTITY_CLU;
  203. ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops,
  204. MEDIA_ENT_F_PROC_VIDEO_LUT);
  205. if (ret < 0)
  206. return ERR_PTR(ret);
  207. /*
  208. * Pre-allocate a body pool, with 3 bodies allowing a userspace update
  209. * before the hardware has committed a previous set of tables, handling
  210. * both the queued and pending dl entries. One extra entry is added to
  211. * the CLU_SIZE to allow for the VI6_CLU_ADDR header.
  212. */
  213. clu->pool = vsp1_dl_body_pool_create(clu->entity.vsp1, 3, CLU_SIZE + 1,
  214. 0);
  215. if (!clu->pool)
  216. return ERR_PTR(-ENOMEM);
  217. /* Initialize the control handler. */
  218. v4l2_ctrl_handler_init(&clu->ctrls, 2);
  219. v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL);
  220. v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL);
  221. clu->entity.subdev.ctrl_handler = &clu->ctrls;
  222. if (clu->ctrls.error) {
  223. dev_err(vsp1->dev, "clu: failed to initialize controls\n");
  224. ret = clu->ctrls.error;
  225. vsp1_entity_destroy(&clu->entity);
  226. return ERR_PTR(ret);
  227. }
  228. v4l2_ctrl_handler_setup(&clu->ctrls);
  229. return clu;
  230. }