vsp1_wpf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. /* Cropping. The partition algorithm can split the image into
  183. * multiple slices.
  184. */
  185. if (pipe->partitions > 1)
  186. width = pipe->partition.width;
  187. vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  188. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  189. (width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  190. vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  191. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  192. (height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  193. if (pipe->lif)
  194. return;
  195. /* Update the memory offsets based on flipping configuration.
  196. * The destination addresses point to the locations where the
  197. * VSP starts writing to memory, which can be different corners
  198. * of the image depending on vertical flipping.
  199. */
  200. if (pipe->partitions > 1) {
  201. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  202. /* Horizontal flipping is handled through a line buffer
  203. * and doesn't modify the start address, but still needs
  204. * to be handled when image partitioning is in effect to
  205. * order the partitions correctly.
  206. */
  207. if (flip & BIT(WPF_CTRL_HFLIP))
  208. offset = format->width - pipe->partition.left
  209. - pipe->partition.width;
  210. else
  211. offset = pipe->partition.left;
  212. mem.addr[0] += offset * fmtinfo->bpp[0] / 8;
  213. if (format->num_planes > 1) {
  214. mem.addr[1] += offset / fmtinfo->hsub
  215. * fmtinfo->bpp[1] / 8;
  216. mem.addr[2] += offset / fmtinfo->hsub
  217. * fmtinfo->bpp[2] / 8;
  218. }
  219. }
  220. if (flip & BIT(WPF_CTRL_VFLIP)) {
  221. mem.addr[0] += (format->height - 1)
  222. * format->plane_fmt[0].bytesperline;
  223. if (format->num_planes > 1) {
  224. offset = (format->height / wpf->fmtinfo->vsub - 1)
  225. * format->plane_fmt[1].bytesperline;
  226. mem.addr[1] += offset;
  227. mem.addr[2] += offset;
  228. }
  229. }
  230. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
  231. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
  232. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
  233. return;
  234. }
  235. /* Format */
  236. if (!pipe->lif) {
  237. const struct v4l2_pix_format_mplane *format = &wpf->format;
  238. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  239. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  240. if (fmtinfo->alpha)
  241. outfmt |= VI6_WPF_OUTFMT_PXA;
  242. if (fmtinfo->swap_yc)
  243. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  244. if (fmtinfo->swap_uv)
  245. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  246. /* Destination stride and byte swapping. */
  247. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
  248. format->plane_fmt[0].bytesperline);
  249. if (format->num_planes > 1)
  250. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
  251. format->plane_fmt[1].bytesperline);
  252. vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
  253. if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
  254. wpf->entity.index == 0)
  255. vsp1_wpf_write(wpf, dl, VI6_WPF_ROT_CTRL,
  256. VI6_WPF_ROT_CTRL_LN16 |
  257. (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
  258. }
  259. if (sink_format->code != source_format->code)
  260. outfmt |= VI6_WPF_OUTFMT_CSC;
  261. wpf->outfmt = outfmt;
  262. vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  263. VI6_DPR_WPF_FPORCH_FP_WPFN);
  264. vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
  265. /* Sources. If the pipeline has a single input and BRU is not used,
  266. * configure it as the master layer. Otherwise configure all
  267. * inputs as sub-layers and select the virtual RPF as the master
  268. * layer.
  269. */
  270. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  271. struct vsp1_rwpf *input = pipe->inputs[i];
  272. if (!input)
  273. continue;
  274. srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
  275. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  276. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  277. }
  278. if (pipe->bru || pipe->num_inputs > 1)
  279. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  280. vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
  281. /* Enable interrupts */
  282. vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  283. vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
  284. VI6_WFP_IRQ_ENB_DFEE);
  285. }
  286. static const struct vsp1_entity_operations wpf_entity_ops = {
  287. .destroy = vsp1_wpf_destroy,
  288. .configure = wpf_configure,
  289. };
  290. /* -----------------------------------------------------------------------------
  291. * Initialization and Cleanup
  292. */
  293. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  294. {
  295. struct vsp1_rwpf *wpf;
  296. char name[6];
  297. int ret;
  298. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  299. if (wpf == NULL)
  300. return ERR_PTR(-ENOMEM);
  301. if (vsp1->info->gen == 2) {
  302. wpf->max_width = WPF_GEN2_MAX_WIDTH;
  303. wpf->max_height = WPF_GEN2_MAX_HEIGHT;
  304. } else {
  305. wpf->max_width = WPF_GEN3_MAX_WIDTH;
  306. wpf->max_height = WPF_GEN3_MAX_HEIGHT;
  307. }
  308. wpf->entity.ops = &wpf_entity_ops;
  309. wpf->entity.type = VSP1_ENTITY_WPF;
  310. wpf->entity.index = index;
  311. sprintf(name, "wpf.%u", index);
  312. ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
  313. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  314. if (ret < 0)
  315. return ERR_PTR(ret);
  316. /* Initialize the display list manager. */
  317. wpf->dlm = vsp1_dlm_create(vsp1, index, 64);
  318. if (!wpf->dlm) {
  319. ret = -ENOMEM;
  320. goto error;
  321. }
  322. /* Initialize the control handler. */
  323. ret = wpf_init_controls(wpf);
  324. if (ret < 0) {
  325. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  326. index);
  327. goto error;
  328. }
  329. v4l2_ctrl_handler_setup(&wpf->ctrls);
  330. return wpf;
  331. error:
  332. vsp1_entity_destroy(&wpf->entity);
  333. return ERR_PTR(ret);
  334. }