mixer_vp_layer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Samsung TV Mixer driver
  3. *
  4. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  5. *
  6. * Tomasz Stanislawski, <t.stanislaws@samsung.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
  10. * by the Free Software Foundiation. either version 2 of the License,
  11. * or (at your option) any later version
  12. */
  13. #include "mixer.h"
  14. #include "regs-vp.h"
  15. #include <media/videobuf2-dma-contig.h>
  16. /* FORMAT DEFINITIONS */
  17. static const struct mxr_format mxr_fmt_nv12 = {
  18. .name = "NV12",
  19. .fourcc = V4L2_PIX_FMT_NV12,
  20. .colorspace = V4L2_COLORSPACE_JPEG,
  21. .num_planes = 2,
  22. .plane = {
  23. { .width = 1, .height = 1, .size = 1 },
  24. { .width = 2, .height = 2, .size = 2 },
  25. },
  26. .num_subframes = 1,
  27. .cookie = VP_MODE_NV12 | VP_MODE_MEM_LINEAR,
  28. };
  29. static const struct mxr_format mxr_fmt_nv21 = {
  30. .name = "NV21",
  31. .fourcc = V4L2_PIX_FMT_NV21,
  32. .colorspace = V4L2_COLORSPACE_JPEG,
  33. .num_planes = 2,
  34. .plane = {
  35. { .width = 1, .height = 1, .size = 1 },
  36. { .width = 2, .height = 2, .size = 2 },
  37. },
  38. .num_subframes = 1,
  39. .cookie = VP_MODE_NV21 | VP_MODE_MEM_LINEAR,
  40. };
  41. static const struct mxr_format mxr_fmt_nv12m = {
  42. .name = "NV12 (mplane)",
  43. .fourcc = V4L2_PIX_FMT_NV12M,
  44. .colorspace = V4L2_COLORSPACE_JPEG,
  45. .num_planes = 2,
  46. .plane = {
  47. { .width = 1, .height = 1, .size = 1 },
  48. { .width = 2, .height = 2, .size = 2 },
  49. },
  50. .num_subframes = 2,
  51. .plane2subframe = {0, 1},
  52. .cookie = VP_MODE_NV12 | VP_MODE_MEM_LINEAR,
  53. };
  54. static const struct mxr_format mxr_fmt_nv12mt = {
  55. .name = "NV12 tiled (mplane)",
  56. .fourcc = V4L2_PIX_FMT_NV12MT,
  57. .colorspace = V4L2_COLORSPACE_JPEG,
  58. .num_planes = 2,
  59. .plane = {
  60. { .width = 128, .height = 32, .size = 4096 },
  61. { .width = 128, .height = 32, .size = 2048 },
  62. },
  63. .num_subframes = 2,
  64. .plane2subframe = {0, 1},
  65. .cookie = VP_MODE_NV12 | VP_MODE_MEM_TILED,
  66. };
  67. static const struct mxr_format *mxr_video_format[] = {
  68. &mxr_fmt_nv12,
  69. &mxr_fmt_nv21,
  70. &mxr_fmt_nv12m,
  71. &mxr_fmt_nv12mt,
  72. };
  73. /* AUXILIARY CALLBACKS */
  74. static void mxr_vp_layer_release(struct mxr_layer *layer)
  75. {
  76. mxr_base_layer_unregister(layer);
  77. mxr_base_layer_release(layer);
  78. }
  79. static void mxr_vp_buffer_set(struct mxr_layer *layer,
  80. struct mxr_buffer *buf)
  81. {
  82. dma_addr_t luma_addr[2] = {0, 0};
  83. dma_addr_t chroma_addr[2] = {0, 0};
  84. if (buf == NULL) {
  85. mxr_reg_vp_buffer(layer->mdev, luma_addr, chroma_addr);
  86. return;
  87. }
  88. luma_addr[0] = vb2_dma_contig_plane_paddr(&buf->vb, 0);
  89. if (layer->fmt->num_subframes == 2) {
  90. chroma_addr[0] = vb2_dma_contig_plane_paddr(&buf->vb, 1);
  91. } else {
  92. /* FIXME: mxr_get_plane_size compute integer division,
  93. * which is slow and should not be performed in interrupt */
  94. chroma_addr[0] = luma_addr[0] + mxr_get_plane_size(
  95. &layer->fmt->plane[0], layer->geo.src.full_width,
  96. layer->geo.src.full_height);
  97. }
  98. if (layer->fmt->cookie & VP_MODE_MEM_TILED) {
  99. luma_addr[1] = luma_addr[0] + 0x40;
  100. chroma_addr[1] = chroma_addr[0] + 0x40;
  101. } else {
  102. luma_addr[1] = luma_addr[0] + layer->geo.src.full_width;
  103. chroma_addr[1] = chroma_addr[0];
  104. }
  105. mxr_reg_vp_buffer(layer->mdev, luma_addr, chroma_addr);
  106. }
  107. static void mxr_vp_stream_set(struct mxr_layer *layer, int en)
  108. {
  109. mxr_reg_vp_layer_stream(layer->mdev, en);
  110. }
  111. static void mxr_vp_format_set(struct mxr_layer *layer)
  112. {
  113. mxr_reg_vp_format(layer->mdev, layer->fmt, &layer->geo);
  114. }
  115. static void mxr_vp_fix_geometry(struct mxr_layer *layer)
  116. {
  117. struct mxr_geometry *geo = &layer->geo;
  118. /* align horizontal size to 8 pixels */
  119. geo->src.full_width = ALIGN(geo->src.full_width, 8);
  120. /* limit to boundary size */
  121. geo->src.full_width = clamp_val(geo->src.full_width, 8, 8192);
  122. geo->src.full_height = clamp_val(geo->src.full_height, 1, 8192);
  123. geo->src.width = clamp_val(geo->src.width, 32, geo->src.full_width);
  124. geo->src.width = min(geo->src.width, 2047U);
  125. geo->src.height = clamp_val(geo->src.height, 4, geo->src.full_height);
  126. geo->src.height = min(geo->src.height, 2047U);
  127. /* setting size of output window */
  128. geo->dst.width = clamp_val(geo->dst.width, 8, geo->dst.full_width);
  129. geo->dst.height = clamp_val(geo->dst.height, 1, geo->dst.full_height);
  130. /* ensure that scaling is in range 1/4x to 16x */
  131. if (geo->src.width >= 4 * geo->dst.width)
  132. geo->src.width = 4 * geo->dst.width;
  133. if (geo->dst.width >= 16 * geo->src.width)
  134. geo->dst.width = 16 * geo->src.width;
  135. if (geo->src.height >= 4 * geo->dst.height)
  136. geo->src.height = 4 * geo->dst.height;
  137. if (geo->dst.height >= 16 * geo->src.height)
  138. geo->dst.height = 16 * geo->src.height;
  139. /* setting scaling ratio */
  140. geo->x_ratio = (geo->src.width << 16) / geo->dst.width;
  141. geo->y_ratio = (geo->src.height << 16) / geo->dst.height;
  142. /* adjust offsets */
  143. geo->src.x_offset = min(geo->src.x_offset,
  144. geo->src.full_width - geo->src.width);
  145. geo->src.y_offset = min(geo->src.y_offset,
  146. geo->src.full_height - geo->src.height);
  147. geo->dst.x_offset = min(geo->dst.x_offset,
  148. geo->dst.full_width - geo->dst.width);
  149. geo->dst.y_offset = min(geo->dst.y_offset,
  150. geo->dst.full_height - geo->dst.height);
  151. }
  152. /* PUBLIC API */
  153. struct mxr_layer *mxr_vp_layer_create(struct mxr_device *mdev, int idx)
  154. {
  155. struct mxr_layer *layer;
  156. int ret;
  157. struct mxr_layer_ops ops = {
  158. .release = mxr_vp_layer_release,
  159. .buffer_set = mxr_vp_buffer_set,
  160. .stream_set = mxr_vp_stream_set,
  161. .format_set = mxr_vp_format_set,
  162. .fix_geometry = mxr_vp_fix_geometry,
  163. };
  164. char name[32];
  165. sprintf(name, "video%d", idx);
  166. layer = mxr_base_layer_create(mdev, idx, name, &ops);
  167. if (layer == NULL) {
  168. mxr_err(mdev, "failed to initialize layer(%d) base\n", idx);
  169. goto fail;
  170. }
  171. layer->fmt_array = mxr_video_format;
  172. layer->fmt_array_size = ARRAY_SIZE(mxr_video_format);
  173. ret = mxr_base_layer_register(layer);
  174. if (ret)
  175. goto fail_layer;
  176. return layer;
  177. fail_layer:
  178. mxr_base_layer_release(layer);
  179. fail:
  180. return NULL;
  181. }