vsp1_bru.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * vsp1_bru.c -- R-Car VSP1 Blend ROP Unit
  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_bru.h"
  18. #include "vsp1_dl.h"
  19. #include "vsp1_pipe.h"
  20. #include "vsp1_rwpf.h"
  21. #include "vsp1_video.h"
  22. #define BRU_MIN_SIZE 1U
  23. #define BRU_MAX_SIZE 8190U
  24. /* -----------------------------------------------------------------------------
  25. * Device Access
  26. */
  27. static inline void vsp1_bru_write(struct vsp1_bru *bru, struct vsp1_dl_list *dl,
  28. u32 reg, u32 data)
  29. {
  30. vsp1_dl_list_write(dl, reg, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * Controls
  34. */
  35. static int bru_s_ctrl(struct v4l2_ctrl *ctrl)
  36. {
  37. struct vsp1_bru *bru =
  38. container_of(ctrl->handler, struct vsp1_bru, ctrls);
  39. switch (ctrl->id) {
  40. case V4L2_CID_BG_COLOR:
  41. bru->bgcolor = ctrl->val;
  42. break;
  43. }
  44. return 0;
  45. }
  46. static const struct v4l2_ctrl_ops bru_ctrl_ops = {
  47. .s_ctrl = bru_s_ctrl,
  48. };
  49. /* -----------------------------------------------------------------------------
  50. * V4L2 Subdevice Operations
  51. */
  52. /*
  53. * The BRU can't perform format conversion, all sink and source formats must be
  54. * identical. We pick the format on the first sink pad (pad 0) and propagate it
  55. * to all other pads.
  56. */
  57. static int bru_enum_mbus_code(struct v4l2_subdev *subdev,
  58. struct v4l2_subdev_pad_config *cfg,
  59. struct v4l2_subdev_mbus_code_enum *code)
  60. {
  61. static const unsigned int codes[] = {
  62. MEDIA_BUS_FMT_ARGB8888_1X32,
  63. MEDIA_BUS_FMT_AYUV8_1X32,
  64. };
  65. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  66. ARRAY_SIZE(codes));
  67. }
  68. static int bru_enum_frame_size(struct v4l2_subdev *subdev,
  69. struct v4l2_subdev_pad_config *cfg,
  70. struct v4l2_subdev_frame_size_enum *fse)
  71. {
  72. if (fse->index)
  73. return -EINVAL;
  74. if (fse->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  75. fse->code != MEDIA_BUS_FMT_AYUV8_1X32)
  76. return -EINVAL;
  77. fse->min_width = BRU_MIN_SIZE;
  78. fse->max_width = BRU_MAX_SIZE;
  79. fse->min_height = BRU_MIN_SIZE;
  80. fse->max_height = BRU_MAX_SIZE;
  81. return 0;
  82. }
  83. static struct v4l2_rect *bru_get_compose(struct vsp1_bru *bru,
  84. struct v4l2_subdev_pad_config *cfg,
  85. unsigned int pad)
  86. {
  87. return v4l2_subdev_get_try_compose(&bru->entity.subdev, cfg, pad);
  88. }
  89. static void bru_try_format(struct vsp1_bru *bru,
  90. struct v4l2_subdev_pad_config *config,
  91. unsigned int pad, struct v4l2_mbus_framefmt *fmt)
  92. {
  93. struct v4l2_mbus_framefmt *format;
  94. switch (pad) {
  95. case BRU_PAD_SINK(0):
  96. /* Default to YUV if the requested format is not supported. */
  97. if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  98. fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
  99. fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
  100. break;
  101. default:
  102. /* The BRU can't perform format conversion. */
  103. format = vsp1_entity_get_pad_format(&bru->entity, config,
  104. BRU_PAD_SINK(0));
  105. fmt->code = format->code;
  106. break;
  107. }
  108. fmt->width = clamp(fmt->width, BRU_MIN_SIZE, BRU_MAX_SIZE);
  109. fmt->height = clamp(fmt->height, BRU_MIN_SIZE, BRU_MAX_SIZE);
  110. fmt->field = V4L2_FIELD_NONE;
  111. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  112. }
  113. static int bru_set_format(struct v4l2_subdev *subdev,
  114. struct v4l2_subdev_pad_config *cfg,
  115. struct v4l2_subdev_format *fmt)
  116. {
  117. struct vsp1_bru *bru = to_bru(subdev);
  118. struct v4l2_subdev_pad_config *config;
  119. struct v4l2_mbus_framefmt *format;
  120. int ret = 0;
  121. mutex_lock(&bru->entity.lock);
  122. config = vsp1_entity_get_pad_config(&bru->entity, cfg, fmt->which);
  123. if (!config) {
  124. ret = -EINVAL;
  125. goto done;
  126. }
  127. bru_try_format(bru, config, fmt->pad, &fmt->format);
  128. format = vsp1_entity_get_pad_format(&bru->entity, config, fmt->pad);
  129. *format = fmt->format;
  130. /* Reset the compose rectangle */
  131. if (fmt->pad != bru->entity.source_pad) {
  132. struct v4l2_rect *compose;
  133. compose = bru_get_compose(bru, config, fmt->pad);
  134. compose->left = 0;
  135. compose->top = 0;
  136. compose->width = format->width;
  137. compose->height = format->height;
  138. }
  139. /* Propagate the format code to all pads */
  140. if (fmt->pad == BRU_PAD_SINK(0)) {
  141. unsigned int i;
  142. for (i = 0; i <= bru->entity.source_pad; ++i) {
  143. format = vsp1_entity_get_pad_format(&bru->entity,
  144. config, i);
  145. format->code = fmt->format.code;
  146. }
  147. }
  148. done:
  149. mutex_unlock(&bru->entity.lock);
  150. return ret;
  151. }
  152. static int bru_get_selection(struct v4l2_subdev *subdev,
  153. struct v4l2_subdev_pad_config *cfg,
  154. struct v4l2_subdev_selection *sel)
  155. {
  156. struct vsp1_bru *bru = to_bru(subdev);
  157. struct v4l2_subdev_pad_config *config;
  158. if (sel->pad == bru->entity.source_pad)
  159. return -EINVAL;
  160. switch (sel->target) {
  161. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  162. sel->r.left = 0;
  163. sel->r.top = 0;
  164. sel->r.width = BRU_MAX_SIZE;
  165. sel->r.height = BRU_MAX_SIZE;
  166. return 0;
  167. case V4L2_SEL_TGT_COMPOSE:
  168. config = vsp1_entity_get_pad_config(&bru->entity, cfg,
  169. sel->which);
  170. if (!config)
  171. return -EINVAL;
  172. mutex_lock(&bru->entity.lock);
  173. sel->r = *bru_get_compose(bru, config, sel->pad);
  174. mutex_unlock(&bru->entity.lock);
  175. return 0;
  176. default:
  177. return -EINVAL;
  178. }
  179. }
  180. static int bru_set_selection(struct v4l2_subdev *subdev,
  181. struct v4l2_subdev_pad_config *cfg,
  182. struct v4l2_subdev_selection *sel)
  183. {
  184. struct vsp1_bru *bru = to_bru(subdev);
  185. struct v4l2_subdev_pad_config *config;
  186. struct v4l2_mbus_framefmt *format;
  187. struct v4l2_rect *compose;
  188. int ret = 0;
  189. if (sel->pad == bru->entity.source_pad)
  190. return -EINVAL;
  191. if (sel->target != V4L2_SEL_TGT_COMPOSE)
  192. return -EINVAL;
  193. mutex_lock(&bru->entity.lock);
  194. config = vsp1_entity_get_pad_config(&bru->entity, cfg, sel->which);
  195. if (!config) {
  196. ret = -EINVAL;
  197. goto done;
  198. }
  199. /*
  200. * The compose rectangle top left corner must be inside the output
  201. * frame.
  202. */
  203. format = vsp1_entity_get_pad_format(&bru->entity, config,
  204. bru->entity.source_pad);
  205. sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
  206. sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
  207. /* Scaling isn't supported, the compose rectangle size must be identical
  208. * to the sink format size.
  209. */
  210. format = vsp1_entity_get_pad_format(&bru->entity, config, sel->pad);
  211. sel->r.width = format->width;
  212. sel->r.height = format->height;
  213. compose = bru_get_compose(bru, config, sel->pad);
  214. *compose = sel->r;
  215. done:
  216. mutex_unlock(&bru->entity.lock);
  217. return ret;
  218. }
  219. static const struct v4l2_subdev_pad_ops bru_pad_ops = {
  220. .init_cfg = vsp1_entity_init_cfg,
  221. .enum_mbus_code = bru_enum_mbus_code,
  222. .enum_frame_size = bru_enum_frame_size,
  223. .get_fmt = vsp1_subdev_get_pad_format,
  224. .set_fmt = bru_set_format,
  225. .get_selection = bru_get_selection,
  226. .set_selection = bru_set_selection,
  227. };
  228. static const struct v4l2_subdev_ops bru_ops = {
  229. .pad = &bru_pad_ops,
  230. };
  231. /* -----------------------------------------------------------------------------
  232. * VSP1 Entity Operations
  233. */
  234. static void bru_configure(struct vsp1_entity *entity,
  235. struct vsp1_pipeline *pipe,
  236. struct vsp1_dl_list *dl,
  237. enum vsp1_entity_params params)
  238. {
  239. struct vsp1_bru *bru = to_bru(&entity->subdev);
  240. struct v4l2_mbus_framefmt *format;
  241. unsigned int flags;
  242. unsigned int i;
  243. if (params != VSP1_ENTITY_PARAMS_INIT)
  244. return;
  245. format = vsp1_entity_get_pad_format(&bru->entity, bru->entity.config,
  246. bru->entity.source_pad);
  247. /* The hardware is extremely flexible but we have no userspace API to
  248. * expose all the parameters, nor is it clear whether we would have use
  249. * cases for all the supported modes. Let's just harcode the parameters
  250. * to sane default values for now.
  251. */
  252. /* Disable dithering and enable color data normalization unless the
  253. * format at the pipeline output is premultiplied.
  254. */
  255. flags = pipe->output ? pipe->output->format.flags : 0;
  256. vsp1_bru_write(bru, dl, VI6_BRU_INCTRL,
  257. flags & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA ?
  258. 0 : VI6_BRU_INCTRL_NRM);
  259. /* Set the background position to cover the whole output image and
  260. * configure its color.
  261. */
  262. vsp1_bru_write(bru, dl, VI6_BRU_VIRRPF_SIZE,
  263. (format->width << VI6_BRU_VIRRPF_SIZE_HSIZE_SHIFT) |
  264. (format->height << VI6_BRU_VIRRPF_SIZE_VSIZE_SHIFT));
  265. vsp1_bru_write(bru, dl, VI6_BRU_VIRRPF_LOC, 0);
  266. vsp1_bru_write(bru, dl, VI6_BRU_VIRRPF_COL, bru->bgcolor |
  267. (0xff << VI6_BRU_VIRRPF_COL_A_SHIFT));
  268. /* Route BRU input 1 as SRC input to the ROP unit and configure the ROP
  269. * unit with a NOP operation to make BRU input 1 available as the
  270. * Blend/ROP unit B SRC input.
  271. */
  272. vsp1_bru_write(bru, dl, VI6_BRU_ROP, VI6_BRU_ROP_DSTSEL_BRUIN(1) |
  273. VI6_BRU_ROP_CROP(VI6_ROP_NOP) |
  274. VI6_BRU_ROP_AROP(VI6_ROP_NOP));
  275. for (i = 0; i < bru->entity.source_pad; ++i) {
  276. bool premultiplied = false;
  277. u32 ctrl = 0;
  278. /* Configure all Blend/ROP units corresponding to an enabled BRU
  279. * input for alpha blending. Blend/ROP units corresponding to
  280. * disabled BRU inputs are used in ROP NOP mode to ignore the
  281. * SRC input.
  282. */
  283. if (bru->inputs[i].rpf) {
  284. ctrl |= VI6_BRU_CTRL_RBC;
  285. premultiplied = bru->inputs[i].rpf->format.flags
  286. & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
  287. } else {
  288. ctrl |= VI6_BRU_CTRL_CROP(VI6_ROP_NOP)
  289. | VI6_BRU_CTRL_AROP(VI6_ROP_NOP);
  290. }
  291. /* Select the virtual RPF as the Blend/ROP unit A DST input to
  292. * serve as a background color.
  293. */
  294. if (i == 0)
  295. ctrl |= VI6_BRU_CTRL_DSTSEL_VRPF;
  296. /* Route BRU inputs 0 to 3 as SRC inputs to Blend/ROP units A to
  297. * D in that order. The Blend/ROP unit B SRC is hardwired to the
  298. * ROP unit output, the corresponding register bits must be set
  299. * to 0.
  300. */
  301. if (i != 1)
  302. ctrl |= VI6_BRU_CTRL_SRCSEL_BRUIN(i);
  303. vsp1_bru_write(bru, dl, VI6_BRU_CTRL(i), ctrl);
  304. /* Harcode the blending formula to
  305. *
  306. * DSTc = DSTc * (1 - SRCa) + SRCc * SRCa
  307. * DSTa = DSTa * (1 - SRCa) + SRCa
  308. *
  309. * when the SRC input isn't premultiplied, and to
  310. *
  311. * DSTc = DSTc * (1 - SRCa) + SRCc
  312. * DSTa = DSTa * (1 - SRCa) + SRCa
  313. *
  314. * otherwise.
  315. */
  316. vsp1_bru_write(bru, dl, VI6_BRU_BLD(i),
  317. VI6_BRU_BLD_CCMDX_255_SRC_A |
  318. (premultiplied ? VI6_BRU_BLD_CCMDY_COEFY :
  319. VI6_BRU_BLD_CCMDY_SRC_A) |
  320. VI6_BRU_BLD_ACMDX_255_SRC_A |
  321. VI6_BRU_BLD_ACMDY_COEFY |
  322. (0xff << VI6_BRU_BLD_COEFY_SHIFT));
  323. }
  324. }
  325. static const struct vsp1_entity_operations bru_entity_ops = {
  326. .configure = bru_configure,
  327. };
  328. /* -----------------------------------------------------------------------------
  329. * Initialization and Cleanup
  330. */
  331. struct vsp1_bru *vsp1_bru_create(struct vsp1_device *vsp1)
  332. {
  333. struct vsp1_bru *bru;
  334. int ret;
  335. bru = devm_kzalloc(vsp1->dev, sizeof(*bru), GFP_KERNEL);
  336. if (bru == NULL)
  337. return ERR_PTR(-ENOMEM);
  338. bru->entity.ops = &bru_entity_ops;
  339. bru->entity.type = VSP1_ENTITY_BRU;
  340. ret = vsp1_entity_init(vsp1, &bru->entity, "bru",
  341. vsp1->info->num_bru_inputs + 1, &bru_ops,
  342. MEDIA_ENT_F_PROC_VIDEO_COMPOSER);
  343. if (ret < 0)
  344. return ERR_PTR(ret);
  345. /* Initialize the control handler. */
  346. v4l2_ctrl_handler_init(&bru->ctrls, 1);
  347. v4l2_ctrl_new_std(&bru->ctrls, &bru_ctrl_ops, V4L2_CID_BG_COLOR,
  348. 0, 0xffffff, 1, 0);
  349. bru->bgcolor = 0;
  350. bru->entity.subdev.ctrl_handler = &bru->ctrls;
  351. if (bru->ctrls.error) {
  352. dev_err(vsp1->dev, "bru: failed to initialize controls\n");
  353. ret = bru->ctrls.error;
  354. vsp1_entity_destroy(&bru->entity);
  355. return ERR_PTR(ret);
  356. }
  357. return bru;
  358. }