vsp1_bru.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #define BRU_MIN_SIZE 4U
  19. #define BRU_MAX_SIZE 8190U
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_bru_read(struct vsp1_bru *bru, u32 reg)
  24. {
  25. return vsp1_read(bru->entity.vsp1, reg);
  26. }
  27. static inline void vsp1_bru_write(struct vsp1_bru *bru, u32 reg, u32 data)
  28. {
  29. vsp1_write(bru->entity.vsp1, reg, data);
  30. }
  31. /* -----------------------------------------------------------------------------
  32. * V4L2 Subdevice Core Operations
  33. */
  34. static bool bru_is_input_enabled(struct vsp1_bru *bru, unsigned int input)
  35. {
  36. return media_entity_remote_pad(&bru->entity.pads[input]) != NULL;
  37. }
  38. static int bru_s_stream(struct v4l2_subdev *subdev, int enable)
  39. {
  40. struct vsp1_bru *bru = to_bru(subdev);
  41. struct v4l2_mbus_framefmt *format;
  42. unsigned int i;
  43. if (!enable)
  44. return 0;
  45. format = &bru->entity.formats[BRU_PAD_SOURCE];
  46. /* The hardware is extremely flexible but we have no userspace API to
  47. * expose all the parameters, nor is it clear whether we would have use
  48. * cases for all the supported modes. Let's just harcode the parameters
  49. * to sane default values for now.
  50. */
  51. /* Disable both color data normalization and dithering. */
  52. vsp1_bru_write(bru, VI6_BRU_INCTRL, 0);
  53. /* Set the background position to cover the whole output image and
  54. * set its color to opaque black.
  55. */
  56. vsp1_bru_write(bru, VI6_BRU_VIRRPF_SIZE,
  57. (format->width << VI6_BRU_VIRRPF_SIZE_HSIZE_SHIFT) |
  58. (format->height << VI6_BRU_VIRRPF_SIZE_VSIZE_SHIFT));
  59. vsp1_bru_write(bru, VI6_BRU_VIRRPF_LOC, 0);
  60. vsp1_bru_write(bru, VI6_BRU_VIRRPF_COL,
  61. 0xff << VI6_BRU_VIRRPF_COL_A_SHIFT);
  62. /* Route BRU input 1 as SRC input to the ROP unit and configure the ROP
  63. * unit with a NOP operation to make BRU input 1 available as the
  64. * Blend/ROP unit B SRC input.
  65. */
  66. vsp1_bru_write(bru, VI6_BRU_ROP, VI6_BRU_ROP_DSTSEL_BRUIN(1) |
  67. VI6_BRU_ROP_CROP(VI6_ROP_NOP) |
  68. VI6_BRU_ROP_AROP(VI6_ROP_NOP));
  69. for (i = 0; i < 4; ++i) {
  70. u32 ctrl = 0;
  71. /* Configure all Blend/ROP units corresponding to an enabled BRU
  72. * input for alpha blending. Blend/ROP units corresponding to
  73. * disabled BRU inputs are used in ROP NOP mode to ignore the
  74. * SRC input.
  75. */
  76. if (bru_is_input_enabled(bru, i))
  77. ctrl |= VI6_BRU_CTRL_RBC;
  78. else
  79. ctrl |= VI6_BRU_CTRL_CROP(VI6_ROP_NOP)
  80. | VI6_BRU_CTRL_AROP(VI6_ROP_NOP);
  81. /* Select the virtual RPF as the Blend/ROP unit A DST input to
  82. * serve as a background color.
  83. */
  84. if (i == 0)
  85. ctrl |= VI6_BRU_CTRL_DSTSEL_VRPF;
  86. /* Route BRU inputs 0 to 3 as SRC inputs to Blend/ROP units A to
  87. * D in that order. The Blend/ROP unit B SRC is hardwired to the
  88. * ROP unit output, the corresponding register bits must be set
  89. * to 0.
  90. */
  91. if (i != 1)
  92. ctrl |= VI6_BRU_CTRL_SRCSEL_BRUIN(i);
  93. vsp1_bru_write(bru, VI6_BRU_CTRL(i), ctrl);
  94. /* Harcode the blending formula to
  95. *
  96. * DSTc = DSTc * (1 - SRCa) + SRCc * SRCa
  97. * DSTa = DSTa * (1 - SRCa) + SRCa
  98. */
  99. vsp1_bru_write(bru, VI6_BRU_BLD(i),
  100. VI6_BRU_BLD_CCMDX_255_SRC_A |
  101. VI6_BRU_BLD_CCMDY_SRC_A |
  102. VI6_BRU_BLD_ACMDX_255_SRC_A |
  103. VI6_BRU_BLD_ACMDY_COEFY |
  104. (0xff << VI6_BRU_BLD_COEFY_SHIFT));
  105. }
  106. return 0;
  107. }
  108. /* -----------------------------------------------------------------------------
  109. * V4L2 Subdevice Pad Operations
  110. */
  111. /*
  112. * The BRU can't perform format conversion, all sink and source formats must be
  113. * identical. We pick the format on the first sink pad (pad 0) and propagate it
  114. * to all other pads.
  115. */
  116. static int bru_enum_mbus_code(struct v4l2_subdev *subdev,
  117. struct v4l2_subdev_fh *fh,
  118. struct v4l2_subdev_mbus_code_enum *code)
  119. {
  120. static const unsigned int codes[] = {
  121. V4L2_MBUS_FMT_ARGB8888_1X32,
  122. V4L2_MBUS_FMT_AYUV8_1X32,
  123. };
  124. struct v4l2_mbus_framefmt *format;
  125. if (code->pad == BRU_PAD_SINK(0)) {
  126. if (code->index >= ARRAY_SIZE(codes))
  127. return -EINVAL;
  128. code->code = codes[code->index];
  129. } else {
  130. if (code->index)
  131. return -EINVAL;
  132. format = v4l2_subdev_get_try_format(fh, BRU_PAD_SINK(0));
  133. code->code = format->code;
  134. }
  135. return 0;
  136. }
  137. static int bru_enum_frame_size(struct v4l2_subdev *subdev,
  138. struct v4l2_subdev_fh *fh,
  139. struct v4l2_subdev_frame_size_enum *fse)
  140. {
  141. if (fse->index)
  142. return -EINVAL;
  143. if (fse->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  144. fse->code != V4L2_MBUS_FMT_AYUV8_1X32)
  145. return -EINVAL;
  146. fse->min_width = BRU_MIN_SIZE;
  147. fse->max_width = BRU_MAX_SIZE;
  148. fse->min_height = BRU_MIN_SIZE;
  149. fse->max_height = BRU_MAX_SIZE;
  150. return 0;
  151. }
  152. static struct v4l2_rect *bru_get_compose(struct vsp1_bru *bru,
  153. struct v4l2_subdev_fh *fh,
  154. unsigned int pad, u32 which)
  155. {
  156. switch (which) {
  157. case V4L2_SUBDEV_FORMAT_TRY:
  158. return v4l2_subdev_get_try_crop(fh, pad);
  159. case V4L2_SUBDEV_FORMAT_ACTIVE:
  160. return &bru->compose[pad];
  161. default:
  162. return NULL;
  163. }
  164. }
  165. static int bru_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  166. struct v4l2_subdev_format *fmt)
  167. {
  168. struct vsp1_bru *bru = to_bru(subdev);
  169. fmt->format = *vsp1_entity_get_pad_format(&bru->entity, fh, fmt->pad,
  170. fmt->which);
  171. return 0;
  172. }
  173. static void bru_try_format(struct vsp1_bru *bru, struct v4l2_subdev_fh *fh,
  174. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  175. enum v4l2_subdev_format_whence which)
  176. {
  177. struct v4l2_mbus_framefmt *format;
  178. switch (pad) {
  179. case BRU_PAD_SINK(0):
  180. /* Default to YUV if the requested format is not supported. */
  181. if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  182. fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
  183. fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
  184. break;
  185. default:
  186. /* The BRU can't perform format conversion. */
  187. format = vsp1_entity_get_pad_format(&bru->entity, fh,
  188. BRU_PAD_SINK(0), which);
  189. fmt->code = format->code;
  190. break;
  191. }
  192. fmt->width = clamp(fmt->width, BRU_MIN_SIZE, BRU_MAX_SIZE);
  193. fmt->height = clamp(fmt->height, BRU_MIN_SIZE, BRU_MAX_SIZE);
  194. fmt->field = V4L2_FIELD_NONE;
  195. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  196. }
  197. static int bru_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  198. struct v4l2_subdev_format *fmt)
  199. {
  200. struct vsp1_bru *bru = to_bru(subdev);
  201. struct v4l2_mbus_framefmt *format;
  202. bru_try_format(bru, fh, fmt->pad, &fmt->format, fmt->which);
  203. format = vsp1_entity_get_pad_format(&bru->entity, fh, fmt->pad,
  204. fmt->which);
  205. *format = fmt->format;
  206. /* Reset the compose rectangle */
  207. if (fmt->pad != BRU_PAD_SOURCE) {
  208. struct v4l2_rect *compose;
  209. compose = bru_get_compose(bru, fh, fmt->pad, fmt->which);
  210. compose->left = 0;
  211. compose->top = 0;
  212. compose->width = format->width;
  213. compose->height = format->height;
  214. }
  215. /* Propagate the format code to all pads */
  216. if (fmt->pad == BRU_PAD_SINK(0)) {
  217. unsigned int i;
  218. for (i = 0; i <= BRU_PAD_SOURCE; ++i) {
  219. format = vsp1_entity_get_pad_format(&bru->entity, fh,
  220. i, fmt->which);
  221. format->code = fmt->format.code;
  222. }
  223. }
  224. return 0;
  225. }
  226. static int bru_get_selection(struct v4l2_subdev *subdev,
  227. struct v4l2_subdev_fh *fh,
  228. struct v4l2_subdev_selection *sel)
  229. {
  230. struct vsp1_bru *bru = to_bru(subdev);
  231. if (sel->pad == BRU_PAD_SOURCE)
  232. return -EINVAL;
  233. switch (sel->target) {
  234. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  235. sel->r.left = 0;
  236. sel->r.top = 0;
  237. sel->r.width = BRU_MAX_SIZE;
  238. sel->r.height = BRU_MAX_SIZE;
  239. return 0;
  240. case V4L2_SEL_TGT_COMPOSE:
  241. sel->r = *bru_get_compose(bru, fh, sel->pad, sel->which);
  242. return 0;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. static int bru_set_selection(struct v4l2_subdev *subdev,
  248. struct v4l2_subdev_fh *fh,
  249. struct v4l2_subdev_selection *sel)
  250. {
  251. struct vsp1_bru *bru = to_bru(subdev);
  252. struct v4l2_mbus_framefmt *format;
  253. struct v4l2_rect *compose;
  254. if (sel->pad == BRU_PAD_SOURCE)
  255. return -EINVAL;
  256. if (sel->target != V4L2_SEL_TGT_COMPOSE)
  257. return -EINVAL;
  258. /* The compose rectangle top left corner must be inside the output
  259. * frame.
  260. */
  261. format = vsp1_entity_get_pad_format(&bru->entity, fh, BRU_PAD_SOURCE,
  262. sel->which);
  263. sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
  264. sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
  265. /* Scaling isn't supported, the compose rectangle size must be identical
  266. * to the sink format size.
  267. */
  268. format = vsp1_entity_get_pad_format(&bru->entity, fh, sel->pad,
  269. sel->which);
  270. sel->r.width = format->width;
  271. sel->r.height = format->height;
  272. compose = bru_get_compose(bru, fh, sel->pad, sel->which);
  273. *compose = sel->r;
  274. return 0;
  275. }
  276. /* -----------------------------------------------------------------------------
  277. * V4L2 Subdevice Operations
  278. */
  279. static struct v4l2_subdev_video_ops bru_video_ops = {
  280. .s_stream = bru_s_stream,
  281. };
  282. static struct v4l2_subdev_pad_ops bru_pad_ops = {
  283. .enum_mbus_code = bru_enum_mbus_code,
  284. .enum_frame_size = bru_enum_frame_size,
  285. .get_fmt = bru_get_format,
  286. .set_fmt = bru_set_format,
  287. .get_selection = bru_get_selection,
  288. .set_selection = bru_set_selection,
  289. };
  290. static struct v4l2_subdev_ops bru_ops = {
  291. .video = &bru_video_ops,
  292. .pad = &bru_pad_ops,
  293. };
  294. /* -----------------------------------------------------------------------------
  295. * Initialization and Cleanup
  296. */
  297. struct vsp1_bru *vsp1_bru_create(struct vsp1_device *vsp1)
  298. {
  299. struct v4l2_subdev *subdev;
  300. struct vsp1_bru *bru;
  301. int ret;
  302. bru = devm_kzalloc(vsp1->dev, sizeof(*bru), GFP_KERNEL);
  303. if (bru == NULL)
  304. return ERR_PTR(-ENOMEM);
  305. bru->entity.type = VSP1_ENTITY_BRU;
  306. ret = vsp1_entity_init(vsp1, &bru->entity, 5);
  307. if (ret < 0)
  308. return ERR_PTR(ret);
  309. /* Initialize the V4L2 subdev. */
  310. subdev = &bru->entity.subdev;
  311. v4l2_subdev_init(subdev, &bru_ops);
  312. subdev->entity.ops = &vsp1_media_ops;
  313. subdev->internal_ops = &vsp1_subdev_internal_ops;
  314. snprintf(subdev->name, sizeof(subdev->name), "%s bru",
  315. dev_name(vsp1->dev));
  316. v4l2_set_subdevdata(subdev, bru);
  317. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  318. vsp1_entity_init_formats(subdev, NULL);
  319. return bru;
  320. }