mixer_vp_layer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_dma_addr(&buf->vb.vb2_buf, 0);
  89. if (layer->fmt->num_subframes == 2) {
  90. chroma_addr[0] =
  91. vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 1);
  92. } else {
  93. /* FIXME: mxr_get_plane_size compute integer division,
  94. * which is slow and should not be performed in interrupt */
  95. chroma_addr[0] = luma_addr[0] + mxr_get_plane_size(
  96. &layer->fmt->plane[0], layer->geo.src.full_width,
  97. layer->geo.src.full_height);
  98. }
  99. if (layer->fmt->cookie & VP_MODE_MEM_TILED) {
  100. luma_addr[1] = luma_addr[0] + 0x40;
  101. chroma_addr[1] = chroma_addr[0] + 0x40;
  102. } else {
  103. luma_addr[1] = luma_addr[0] + layer->geo.src.full_width;
  104. chroma_addr[1] = chroma_addr[0];
  105. }
  106. mxr_reg_vp_buffer(layer->mdev, luma_addr, chroma_addr);
  107. }
  108. static void mxr_vp_stream_set(struct mxr_layer *layer, int en)
  109. {
  110. mxr_reg_vp_layer_stream(layer->mdev, en);
  111. }
  112. static void mxr_vp_format_set(struct mxr_layer *layer)
  113. {
  114. mxr_reg_vp_format(layer->mdev, layer->fmt, &layer->geo);
  115. }
  116. static inline unsigned int do_center(unsigned int center,
  117. unsigned int size, unsigned int upper, unsigned int flags)
  118. {
  119. unsigned int lower;
  120. if (flags & MXR_NO_OFFSET)
  121. return 0;
  122. lower = center - min(center, size / 2);
  123. return min(lower, upper - size);
  124. }
  125. static void mxr_vp_fix_geometry(struct mxr_layer *layer,
  126. enum mxr_geometry_stage stage, unsigned long flags)
  127. {
  128. struct mxr_geometry *geo = &layer->geo;
  129. struct mxr_crop *src = &geo->src;
  130. struct mxr_crop *dst = &geo->dst;
  131. unsigned long x_center, y_center;
  132. switch (stage) {
  133. case MXR_GEOMETRY_SINK: /* nothing to be fixed here */
  134. case MXR_GEOMETRY_COMPOSE:
  135. /* remember center of the area */
  136. x_center = dst->x_offset + dst->width / 2;
  137. y_center = dst->y_offset + dst->height / 2;
  138. /* ensure that compose is reachable using 16x scaling */
  139. dst->width = clamp(dst->width, 8U, 16 * src->full_width);
  140. dst->height = clamp(dst->height, 1U, 16 * src->full_height);
  141. /* setup offsets */
  142. dst->x_offset = do_center(x_center, dst->width,
  143. dst->full_width, flags);
  144. dst->y_offset = do_center(y_center, dst->height,
  145. dst->full_height, flags);
  146. flags = 0; /* remove possible MXR_NO_OFFSET flag */
  147. /* fall through */
  148. case MXR_GEOMETRY_CROP:
  149. /* remember center of the area */
  150. x_center = src->x_offset + src->width / 2;
  151. y_center = src->y_offset + src->height / 2;
  152. /* ensure scaling is between 0.25x .. 16x */
  153. src->width = clamp(src->width, round_up(dst->width / 16, 4),
  154. dst->width * 4);
  155. src->height = clamp(src->height, round_up(dst->height / 16, 4),
  156. dst->height * 4);
  157. /* hardware limits */
  158. src->width = clamp(src->width, 32U, 2047U);
  159. src->height = clamp(src->height, 4U, 2047U);
  160. /* setup offsets */
  161. src->x_offset = do_center(x_center, src->width,
  162. src->full_width, flags);
  163. src->y_offset = do_center(y_center, src->height,
  164. src->full_height, flags);
  165. /* setting scaling ratio */
  166. geo->x_ratio = (src->width << 16) / dst->width;
  167. geo->y_ratio = (src->height << 16) / dst->height;
  168. /* fall through */
  169. case MXR_GEOMETRY_SOURCE:
  170. src->full_width = clamp(src->full_width,
  171. ALIGN(src->width + src->x_offset, 8), 8192U);
  172. src->full_height = clamp(src->full_height,
  173. src->height + src->y_offset, 8192U);
  174. }
  175. }
  176. /* PUBLIC API */
  177. struct mxr_layer *mxr_vp_layer_create(struct mxr_device *mdev, int idx)
  178. {
  179. struct mxr_layer *layer;
  180. int ret;
  181. struct mxr_layer_ops ops = {
  182. .release = mxr_vp_layer_release,
  183. .buffer_set = mxr_vp_buffer_set,
  184. .stream_set = mxr_vp_stream_set,
  185. .format_set = mxr_vp_format_set,
  186. .fix_geometry = mxr_vp_fix_geometry,
  187. };
  188. char name[32];
  189. sprintf(name, "video%d", idx);
  190. layer = mxr_base_layer_create(mdev, idx, name, &ops);
  191. if (layer == NULL) {
  192. mxr_err(mdev, "failed to initialize layer(%d) base\n", idx);
  193. goto fail;
  194. }
  195. layer->fmt_array = mxr_video_format;
  196. layer->fmt_array_size = ARRAY_SIZE(mxr_video_format);
  197. ret = mxr_base_layer_register(layer);
  198. if (ret)
  199. goto fail_layer;
  200. return layer;
  201. fail_layer:
  202. mxr_base_layer_release(layer);
  203. fail:
  204. return NULL;
  205. }