vsp1_clu.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table
  3. *
  4. * Copyright (C) 2015-2016 Renesas Electronics 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/slab.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_clu.h"
  18. #include "vsp1_dl.h"
  19. #define CLU_MIN_SIZE 4U
  20. #define CLU_MAX_SIZE 8190U
  21. /* -----------------------------------------------------------------------------
  22. * Device Access
  23. */
  24. static inline void vsp1_clu_write(struct vsp1_clu *clu, 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_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001)
  33. #define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002)
  34. #define V4L2_CID_VSP1_CLU_MODE_2D 0
  35. #define V4L2_CID_VSP1_CLU_MODE_3D 1
  36. static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl)
  37. {
  38. struct vsp1_dl_body *dlb;
  39. unsigned int i;
  40. dlb = vsp1_dl_fragment_alloc(clu->entity.vsp1, 1 + 17 * 17 * 17);
  41. if (!dlb)
  42. return -ENOMEM;
  43. vsp1_dl_fragment_write(dlb, VI6_CLU_ADDR, 0);
  44. for (i = 0; i < 17 * 17 * 17; ++i)
  45. vsp1_dl_fragment_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]);
  46. spin_lock_irq(&clu->lock);
  47. swap(clu->clu, dlb);
  48. spin_unlock_irq(&clu->lock);
  49. vsp1_dl_fragment_free(dlb);
  50. return 0;
  51. }
  52. static int clu_s_ctrl(struct v4l2_ctrl *ctrl)
  53. {
  54. struct vsp1_clu *clu =
  55. container_of(ctrl->handler, struct vsp1_clu, ctrls);
  56. switch (ctrl->id) {
  57. case V4L2_CID_VSP1_CLU_TABLE:
  58. clu_set_table(clu, ctrl);
  59. break;
  60. case V4L2_CID_VSP1_CLU_MODE:
  61. clu->mode = ctrl->val;
  62. break;
  63. }
  64. return 0;
  65. }
  66. static const struct v4l2_ctrl_ops clu_ctrl_ops = {
  67. .s_ctrl = clu_s_ctrl,
  68. };
  69. static const struct v4l2_ctrl_config clu_table_control = {
  70. .ops = &clu_ctrl_ops,
  71. .id = V4L2_CID_VSP1_CLU_TABLE,
  72. .name = "Look-Up Table",
  73. .type = V4L2_CTRL_TYPE_U32,
  74. .min = 0x00000000,
  75. .max = 0x00ffffff,
  76. .step = 1,
  77. .def = 0,
  78. .dims = { 17, 17, 17 },
  79. };
  80. static const char * const clu_mode_menu[] = {
  81. "2D",
  82. "3D",
  83. NULL,
  84. };
  85. static const struct v4l2_ctrl_config clu_mode_control = {
  86. .ops = &clu_ctrl_ops,
  87. .id = V4L2_CID_VSP1_CLU_MODE,
  88. .name = "Mode",
  89. .type = V4L2_CTRL_TYPE_MENU,
  90. .min = 0,
  91. .max = 1,
  92. .def = 1,
  93. .qmenu = clu_mode_menu,
  94. };
  95. /* -----------------------------------------------------------------------------
  96. * V4L2 Subdevice Pad Operations
  97. */
  98. static int clu_enum_mbus_code(struct v4l2_subdev *subdev,
  99. struct v4l2_subdev_pad_config *cfg,
  100. struct v4l2_subdev_mbus_code_enum *code)
  101. {
  102. static const unsigned int codes[] = {
  103. MEDIA_BUS_FMT_ARGB8888_1X32,
  104. MEDIA_BUS_FMT_AHSV8888_1X32,
  105. MEDIA_BUS_FMT_AYUV8_1X32,
  106. };
  107. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  108. ARRAY_SIZE(codes));
  109. }
  110. static int clu_enum_frame_size(struct v4l2_subdev *subdev,
  111. struct v4l2_subdev_pad_config *cfg,
  112. struct v4l2_subdev_frame_size_enum *fse)
  113. {
  114. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE,
  115. CLU_MIN_SIZE, CLU_MAX_SIZE,
  116. CLU_MAX_SIZE);
  117. }
  118. static int clu_set_format(struct v4l2_subdev *subdev,
  119. struct v4l2_subdev_pad_config *cfg,
  120. struct v4l2_subdev_format *fmt)
  121. {
  122. struct vsp1_clu *clu = to_clu(subdev);
  123. struct v4l2_subdev_pad_config *config;
  124. struct v4l2_mbus_framefmt *format;
  125. int ret = 0;
  126. mutex_lock(&clu->entity.lock);
  127. config = vsp1_entity_get_pad_config(&clu->entity, cfg, fmt->which);
  128. if (!config) {
  129. ret = -EINVAL;
  130. goto done;
  131. }
  132. /* Default to YUV if the requested format is not supported. */
  133. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  134. fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
  135. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  136. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  137. format = vsp1_entity_get_pad_format(&clu->entity, config, fmt->pad);
  138. if (fmt->pad == CLU_PAD_SOURCE) {
  139. /* The CLU output format can't be modified. */
  140. fmt->format = *format;
  141. goto done;
  142. }
  143. format->code = fmt->format.code;
  144. format->width = clamp_t(unsigned int, fmt->format.width,
  145. CLU_MIN_SIZE, CLU_MAX_SIZE);
  146. format->height = clamp_t(unsigned int, fmt->format.height,
  147. CLU_MIN_SIZE, CLU_MAX_SIZE);
  148. format->field = V4L2_FIELD_NONE;
  149. format->colorspace = V4L2_COLORSPACE_SRGB;
  150. fmt->format = *format;
  151. /* Propagate the format to the source pad. */
  152. format = vsp1_entity_get_pad_format(&clu->entity, config,
  153. CLU_PAD_SOURCE);
  154. *format = fmt->format;
  155. done:
  156. mutex_unlock(&clu->entity.lock);
  157. return ret;
  158. }
  159. /* -----------------------------------------------------------------------------
  160. * V4L2 Subdevice Operations
  161. */
  162. static const struct v4l2_subdev_pad_ops clu_pad_ops = {
  163. .init_cfg = vsp1_entity_init_cfg,
  164. .enum_mbus_code = clu_enum_mbus_code,
  165. .enum_frame_size = clu_enum_frame_size,
  166. .get_fmt = vsp1_subdev_get_pad_format,
  167. .set_fmt = clu_set_format,
  168. };
  169. static const struct v4l2_subdev_ops clu_ops = {
  170. .pad = &clu_pad_ops,
  171. };
  172. /* -----------------------------------------------------------------------------
  173. * VSP1 Entity Operations
  174. */
  175. static void clu_configure(struct vsp1_entity *entity,
  176. struct vsp1_pipeline *pipe,
  177. struct vsp1_dl_list *dl,
  178. enum vsp1_entity_params params)
  179. {
  180. struct vsp1_clu *clu = to_clu(&entity->subdev);
  181. struct vsp1_dl_body *dlb;
  182. unsigned long flags;
  183. u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN;
  184. switch (params) {
  185. case VSP1_ENTITY_PARAMS_INIT: {
  186. /*
  187. * The format can't be changed during streaming, only verify it
  188. * at setup time and store the information internally for future
  189. * runtime configuration calls.
  190. */
  191. struct v4l2_mbus_framefmt *format;
  192. format = vsp1_entity_get_pad_format(&clu->entity,
  193. clu->entity.config,
  194. CLU_PAD_SINK);
  195. clu->yuv_mode = format->code == MEDIA_BUS_FMT_AYUV8_1X32;
  196. break;
  197. }
  198. case VSP1_ENTITY_PARAMS_PARTITION:
  199. break;
  200. case VSP1_ENTITY_PARAMS_RUNTIME:
  201. /* 2D mode can only be used with the YCbCr pixel encoding. */
  202. if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D && clu->yuv_mode)
  203. ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D
  204. | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D
  205. | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D;
  206. vsp1_clu_write(clu, dl, VI6_CLU_CTRL, ctrl);
  207. spin_lock_irqsave(&clu->lock, flags);
  208. dlb = clu->clu;
  209. clu->clu = NULL;
  210. spin_unlock_irqrestore(&clu->lock, flags);
  211. if (dlb)
  212. vsp1_dl_list_add_fragment(dl, dlb);
  213. break;
  214. }
  215. }
  216. static const struct vsp1_entity_operations clu_entity_ops = {
  217. .configure = clu_configure,
  218. };
  219. /* -----------------------------------------------------------------------------
  220. * Initialization and Cleanup
  221. */
  222. struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1)
  223. {
  224. struct vsp1_clu *clu;
  225. int ret;
  226. clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL);
  227. if (clu == NULL)
  228. return ERR_PTR(-ENOMEM);
  229. spin_lock_init(&clu->lock);
  230. clu->entity.ops = &clu_entity_ops;
  231. clu->entity.type = VSP1_ENTITY_CLU;
  232. ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops,
  233. MEDIA_ENT_F_PROC_VIDEO_LUT);
  234. if (ret < 0)
  235. return ERR_PTR(ret);
  236. /* Initialize the control handler. */
  237. v4l2_ctrl_handler_init(&clu->ctrls, 2);
  238. v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL);
  239. v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL);
  240. clu->entity.subdev.ctrl_handler = &clu->ctrls;
  241. if (clu->ctrls.error) {
  242. dev_err(vsp1->dev, "clu: failed to initialize controls\n");
  243. ret = clu->ctrls.error;
  244. vsp1_entity_destroy(&clu->entity);
  245. return ERR_PTR(ret);
  246. }
  247. v4l2_ctrl_handler_setup(&clu->ctrls);
  248. return clu;
  249. }