vsp1_wpf.c 10 KB

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