vsp1_wpf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter
  3. *
  4. * Copyright (C) 2013-2014 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 <media/v4l2-subdev.h>
  15. #include "vsp1.h"
  16. #include "vsp1_dl.h"
  17. #include "vsp1_pipe.h"
  18. #include "vsp1_rwpf.h"
  19. #include "vsp1_video.h"
  20. #define WPF_GEN2_MAX_WIDTH 2048U
  21. #define WPF_GEN2_MAX_HEIGHT 2048U
  22. #define WPF_GEN3_MAX_WIDTH 8190U
  23. #define WPF_GEN3_MAX_HEIGHT 8190U
  24. /* -----------------------------------------------------------------------------
  25. * Device Access
  26. */
  27. static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
  28. struct vsp1_dl_list *dl, u32 reg, u32 data)
  29. {
  30. vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * Controls
  34. */
  35. enum wpf_flip_ctrl {
  36. WPF_CTRL_VFLIP = 0,
  37. WPF_CTRL_HFLIP = 1,
  38. WPF_CTRL_MAX,
  39. };
  40. static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl)
  41. {
  42. struct vsp1_rwpf *wpf =
  43. container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  44. unsigned int i;
  45. u32 flip = 0;
  46. switch (ctrl->id) {
  47. case V4L2_CID_HFLIP:
  48. case V4L2_CID_VFLIP:
  49. for (i = 0; i < WPF_CTRL_MAX; ++i) {
  50. if (wpf->flip.ctrls[i])
  51. flip |= wpf->flip.ctrls[i]->val ? BIT(i) : 0;
  52. }
  53. spin_lock_irq(&wpf->flip.lock);
  54. wpf->flip.pending = flip;
  55. spin_unlock_irq(&wpf->flip.lock);
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. return 0;
  61. }
  62. static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = {
  63. .s_ctrl = vsp1_wpf_s_ctrl,
  64. };
  65. static int wpf_init_controls(struct vsp1_rwpf *wpf)
  66. {
  67. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  68. unsigned int num_flip_ctrls;
  69. spin_lock_init(&wpf->flip.lock);
  70. if (wpf->entity.index != 0) {
  71. /* Only WPF0 supports flipping. */
  72. num_flip_ctrls = 0;
  73. } else if (vsp1->info->features & VSP1_HAS_WPF_HFLIP) {
  74. /* When horizontal flip is supported the WPF implements two
  75. * controls (horizontal flip and vertical flip).
  76. */
  77. num_flip_ctrls = 2;
  78. } else if (vsp1->info->features & VSP1_HAS_WPF_VFLIP) {
  79. /* When only vertical flip is supported the WPF implements a
  80. * single control (vertical flip).
  81. */
  82. num_flip_ctrls = 1;
  83. } else {
  84. /* Otherwise flipping is not supported. */
  85. num_flip_ctrls = 0;
  86. }
  87. vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls);
  88. if (num_flip_ctrls >= 1) {
  89. wpf->flip.ctrls[WPF_CTRL_VFLIP] =
  90. v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
  91. V4L2_CID_VFLIP, 0, 1, 1, 0);
  92. }
  93. if (num_flip_ctrls == 2) {
  94. wpf->flip.ctrls[WPF_CTRL_HFLIP] =
  95. v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
  96. V4L2_CID_HFLIP, 0, 1, 1, 0);
  97. v4l2_ctrl_cluster(2, wpf->flip.ctrls);
  98. }
  99. if (wpf->ctrls.error) {
  100. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  101. wpf->entity.index);
  102. return wpf->ctrls.error;
  103. }
  104. return 0;
  105. }
  106. /* -----------------------------------------------------------------------------
  107. * V4L2 Subdevice Core Operations
  108. */
  109. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  110. {
  111. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  112. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  113. if (enable)
  114. return 0;
  115. /* Write to registers directly when stopping the stream as there will be
  116. * no pipeline run to apply the display list.
  117. */
  118. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  119. vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
  120. VI6_WPF_SRCRPF, 0);
  121. return 0;
  122. }
  123. /* -----------------------------------------------------------------------------
  124. * V4L2 Subdevice Operations
  125. */
  126. static const struct v4l2_subdev_video_ops wpf_video_ops = {
  127. .s_stream = wpf_s_stream,
  128. };
  129. static const struct v4l2_subdev_ops wpf_ops = {
  130. .video = &wpf_video_ops,
  131. .pad = &vsp1_rwpf_pad_ops,
  132. };
  133. /* -----------------------------------------------------------------------------
  134. * VSP1 Entity Operations
  135. */
  136. static void vsp1_wpf_destroy(struct vsp1_entity *entity)
  137. {
  138. struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
  139. vsp1_dlm_destroy(wpf->dlm);
  140. }
  141. static void wpf_configure(struct vsp1_entity *entity,
  142. struct vsp1_pipeline *pipe,
  143. struct vsp1_dl_list *dl,
  144. enum vsp1_entity_params params)
  145. {
  146. struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
  147. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  148. const struct v4l2_mbus_framefmt *source_format;
  149. const struct v4l2_mbus_framefmt *sink_format;
  150. unsigned int i;
  151. u32 outfmt = 0;
  152. u32 srcrpf = 0;
  153. if (params == VSP1_ENTITY_PARAMS_RUNTIME) {
  154. const unsigned int mask = BIT(WPF_CTRL_VFLIP)
  155. | BIT(WPF_CTRL_HFLIP);
  156. unsigned long flags;
  157. spin_lock_irqsave(&wpf->flip.lock, flags);
  158. wpf->flip.active = (wpf->flip.active & ~mask)
  159. | (wpf->flip.pending & mask);
  160. spin_unlock_irqrestore(&wpf->flip.lock, flags);
  161. outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt;
  162. if (wpf->flip.active & BIT(WPF_CTRL_VFLIP))
  163. outfmt |= VI6_WPF_OUTFMT_FLP;
  164. if (wpf->flip.active & BIT(WPF_CTRL_HFLIP))
  165. outfmt |= VI6_WPF_OUTFMT_HFLP;
  166. vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
  167. return;
  168. }
  169. sink_format = vsp1_entity_get_pad_format(&wpf->entity,
  170. wpf->entity.config,
  171. RWPF_PAD_SINK);
  172. source_format = vsp1_entity_get_pad_format(&wpf->entity,
  173. wpf->entity.config,
  174. RWPF_PAD_SOURCE);
  175. if (params == VSP1_ENTITY_PARAMS_PARTITION) {
  176. const struct v4l2_pix_format_mplane *format = &wpf->format;
  177. struct vsp1_rwpf_memory mem = wpf->mem;
  178. unsigned int flip = wpf->flip.active;
  179. unsigned int width = source_format->width;
  180. unsigned int height = source_format->height;
  181. unsigned int offset;
  182. /*
  183. * Cropping. The partition algorithm can split the image into
  184. * multiple slices.
  185. */
  186. if (pipe->partitions > 1)
  187. width = pipe->partition.width;
  188. vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  189. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  190. (width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  191. vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  192. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  193. (height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  194. if (pipe->lif)
  195. return;
  196. /*
  197. * Update the memory offsets based on flipping configuration.
  198. * The destination addresses point to the locations where the
  199. * VSP starts writing to memory, which can be different corners
  200. * of the image depending on vertical flipping.
  201. */
  202. if (pipe->partitions > 1) {
  203. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  204. /*
  205. * Horizontal flipping is handled through a line buffer
  206. * and doesn't modify the start address, but still needs
  207. * to be handled when image partitioning is in effect to
  208. * order the partitions correctly.
  209. */
  210. if (flip & BIT(WPF_CTRL_HFLIP))
  211. offset = format->width - pipe->partition.left
  212. - pipe->partition.width;
  213. else
  214. offset = pipe->partition.left;
  215. mem.addr[0] += offset * fmtinfo->bpp[0] / 8;
  216. if (format->num_planes > 1) {
  217. mem.addr[1] += offset / fmtinfo->hsub
  218. * fmtinfo->bpp[1] / 8;
  219. mem.addr[2] += offset / fmtinfo->hsub
  220. * fmtinfo->bpp[2] / 8;
  221. }
  222. }
  223. if (flip & BIT(WPF_CTRL_VFLIP)) {
  224. mem.addr[0] += (format->height - 1)
  225. * format->plane_fmt[0].bytesperline;
  226. if (format->num_planes > 1) {
  227. offset = (format->height / wpf->fmtinfo->vsub - 1)
  228. * format->plane_fmt[1].bytesperline;
  229. mem.addr[1] += offset;
  230. mem.addr[2] += offset;
  231. }
  232. }
  233. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
  234. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
  235. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
  236. return;
  237. }
  238. /* Format */
  239. if (!pipe->lif) {
  240. const struct v4l2_pix_format_mplane *format = &wpf->format;
  241. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  242. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  243. if (fmtinfo->alpha)
  244. outfmt |= VI6_WPF_OUTFMT_PXA;
  245. if (fmtinfo->swap_yc)
  246. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  247. if (fmtinfo->swap_uv)
  248. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  249. /* Destination stride and byte swapping. */
  250. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
  251. format->plane_fmt[0].bytesperline);
  252. if (format->num_planes > 1)
  253. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
  254. format->plane_fmt[1].bytesperline);
  255. vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
  256. if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
  257. wpf->entity.index == 0)
  258. vsp1_wpf_write(wpf, dl, VI6_WPF_ROT_CTRL,
  259. VI6_WPF_ROT_CTRL_LN16 |
  260. (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
  261. }
  262. if (sink_format->code != source_format->code)
  263. outfmt |= VI6_WPF_OUTFMT_CSC;
  264. wpf->outfmt = outfmt;
  265. vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  266. VI6_DPR_WPF_FPORCH_FP_WPFN);
  267. vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
  268. /* Sources. If the pipeline has a single input and BRU is not used,
  269. * configure it as the master layer. Otherwise configure all
  270. * inputs as sub-layers and select the virtual RPF as the master
  271. * layer.
  272. */
  273. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  274. struct vsp1_rwpf *input = pipe->inputs[i];
  275. if (!input)
  276. continue;
  277. srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
  278. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  279. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  280. }
  281. if (pipe->bru || pipe->num_inputs > 1)
  282. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  283. vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
  284. /* Enable interrupts */
  285. vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  286. vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
  287. VI6_WFP_IRQ_ENB_DFEE);
  288. }
  289. static const struct vsp1_entity_operations wpf_entity_ops = {
  290. .destroy = vsp1_wpf_destroy,
  291. .configure = wpf_configure,
  292. };
  293. /* -----------------------------------------------------------------------------
  294. * Initialization and Cleanup
  295. */
  296. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  297. {
  298. struct vsp1_rwpf *wpf;
  299. char name[6];
  300. int ret;
  301. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  302. if (wpf == NULL)
  303. return ERR_PTR(-ENOMEM);
  304. if (vsp1->info->gen == 2) {
  305. wpf->max_width = WPF_GEN2_MAX_WIDTH;
  306. wpf->max_height = WPF_GEN2_MAX_HEIGHT;
  307. } else {
  308. wpf->max_width = WPF_GEN3_MAX_WIDTH;
  309. wpf->max_height = WPF_GEN3_MAX_HEIGHT;
  310. }
  311. wpf->entity.ops = &wpf_entity_ops;
  312. wpf->entity.type = VSP1_ENTITY_WPF;
  313. wpf->entity.index = index;
  314. sprintf(name, "wpf.%u", index);
  315. ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
  316. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  317. if (ret < 0)
  318. return ERR_PTR(ret);
  319. /* Initialize the display list manager. */
  320. wpf->dlm = vsp1_dlm_create(vsp1, index, 64);
  321. if (!wpf->dlm) {
  322. ret = -ENOMEM;
  323. goto error;
  324. }
  325. /* Initialize the control handler. */
  326. ret = wpf_init_controls(wpf);
  327. if (ret < 0) {
  328. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  329. index);
  330. goto error;
  331. }
  332. v4l2_ctrl_handler_setup(&wpf->ctrls);
  333. return wpf;
  334. error:
  335. vsp1_entity_destroy(&wpf->entity);
  336. return ERR_PTR(ret);
  337. }