vsp1_sru.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * vsp1_sru.c -- R-Car VSP1 Super Resolution Unit
  3. *
  4. * Copyright (C) 2013 Renesas 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 <linux/gfp.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_sru.h"
  18. #define SRU_MIN_SIZE 4U
  19. #define SRU_MAX_SIZE 8190U
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_sru_read(struct vsp1_sru *sru, u32 reg)
  24. {
  25. return vsp1_read(sru->entity.vsp1, reg);
  26. }
  27. static inline void vsp1_sru_write(struct vsp1_sru *sru, u32 reg, u32 data)
  28. {
  29. vsp1_write(sru->entity.vsp1, reg, data);
  30. }
  31. /* -----------------------------------------------------------------------------
  32. * Controls
  33. */
  34. #define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE + 1)
  35. static int sru_s_ctrl(struct v4l2_ctrl *ctrl)
  36. {
  37. struct vsp1_sru *sru =
  38. container_of(ctrl->handler, struct vsp1_sru, ctrls);
  39. switch (ctrl->id) {
  40. case V4L2_CID_VSP1_SRU_INTENSITY:
  41. sru->intensity = ctrl->val;
  42. break;
  43. }
  44. return 0;
  45. }
  46. static const struct v4l2_ctrl_ops sru_ctrl_ops = {
  47. .s_ctrl = sru_s_ctrl,
  48. };
  49. static const struct v4l2_ctrl_config sru_intensity_control = {
  50. .ops = &sru_ctrl_ops,
  51. .id = V4L2_CID_VSP1_SRU_INTENSITY,
  52. .name = "Intensity",
  53. .type = V4L2_CTRL_TYPE_INTEGER,
  54. .min = 1,
  55. .max = 6,
  56. .step = 1,
  57. };
  58. /* -----------------------------------------------------------------------------
  59. * V4L2 Subdevice Core Operations
  60. */
  61. struct vsp1_sru_param {
  62. u32 ctrl0;
  63. u32 ctrl2;
  64. };
  65. #define VI6_SRU_CTRL0_PARAMS(p0, p1) \
  66. (((p0) << VI6_SRU_CTRL0_PARAM0_SHIFT) | \
  67. ((p1) << VI6_SRU_CTRL0_PARAM1_SHIFT))
  68. #define VI6_SRU_CTRL2_PARAMS(p6, p7, p8) \
  69. (((p6) << VI6_SRU_CTRL2_PARAM6_SHIFT) | \
  70. ((p7) << VI6_SRU_CTRL2_PARAM7_SHIFT) | \
  71. ((p8) << VI6_SRU_CTRL2_PARAM8_SHIFT))
  72. static const struct vsp1_sru_param vsp1_sru_params[] = {
  73. {
  74. .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
  75. .ctrl2 = VI6_SRU_CTRL2_PARAMS(24, 40, 255),
  76. }, {
  77. .ctrl0 = VI6_SRU_CTRL0_PARAMS(256, 4) | VI6_SRU_CTRL0_EN,
  78. .ctrl2 = VI6_SRU_CTRL2_PARAMS(8, 16, 255),
  79. }, {
  80. .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
  81. .ctrl2 = VI6_SRU_CTRL2_PARAMS(36, 60, 255),
  82. }, {
  83. .ctrl0 = VI6_SRU_CTRL0_PARAMS(384, 5) | VI6_SRU_CTRL0_EN,
  84. .ctrl2 = VI6_SRU_CTRL2_PARAMS(12, 27, 255),
  85. }, {
  86. .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
  87. .ctrl2 = VI6_SRU_CTRL2_PARAMS(48, 80, 255),
  88. }, {
  89. .ctrl0 = VI6_SRU_CTRL0_PARAMS(511, 6) | VI6_SRU_CTRL0_EN,
  90. .ctrl2 = VI6_SRU_CTRL2_PARAMS(16, 36, 255),
  91. },
  92. };
  93. static int sru_s_stream(struct v4l2_subdev *subdev, int enable)
  94. {
  95. struct vsp1_sru *sru = to_sru(subdev);
  96. const struct vsp1_sru_param *param;
  97. struct v4l2_mbus_framefmt *input;
  98. struct v4l2_mbus_framefmt *output;
  99. bool upscale;
  100. u32 ctrl0;
  101. if (!enable)
  102. return 0;
  103. input = &sru->entity.formats[SRU_PAD_SINK];
  104. output = &sru->entity.formats[SRU_PAD_SOURCE];
  105. upscale = input->width != output->width;
  106. param = &vsp1_sru_params[sru->intensity];
  107. if (input->code == V4L2_MBUS_FMT_ARGB8888_1X32)
  108. ctrl0 = VI6_SRU_CTRL0_PARAM2 | VI6_SRU_CTRL0_PARAM3
  109. | VI6_SRU_CTRL0_PARAM4;
  110. else
  111. ctrl0 = VI6_SRU_CTRL0_PARAM3;
  112. vsp1_sru_write(sru, VI6_SRU_CTRL0, param->ctrl0 | ctrl0 |
  113. (upscale ? VI6_SRU_CTRL0_MODE_UPSCALE : 0));
  114. vsp1_sru_write(sru, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5);
  115. vsp1_sru_write(sru, VI6_SRU_CTRL2, param->ctrl2);
  116. return 0;
  117. }
  118. /* -----------------------------------------------------------------------------
  119. * V4L2 Subdevice Pad Operations
  120. */
  121. static int sru_enum_mbus_code(struct v4l2_subdev *subdev,
  122. struct v4l2_subdev_fh *fh,
  123. struct v4l2_subdev_mbus_code_enum *code)
  124. {
  125. static const unsigned int codes[] = {
  126. V4L2_MBUS_FMT_ARGB8888_1X32,
  127. V4L2_MBUS_FMT_AYUV8_1X32,
  128. };
  129. struct v4l2_mbus_framefmt *format;
  130. if (code->pad == SRU_PAD_SINK) {
  131. if (code->index >= ARRAY_SIZE(codes))
  132. return -EINVAL;
  133. code->code = codes[code->index];
  134. } else {
  135. /* The SRU can't perform format conversion, the sink format is
  136. * always identical to the source format.
  137. */
  138. if (code->index)
  139. return -EINVAL;
  140. format = v4l2_subdev_get_try_format(fh, SRU_PAD_SINK);
  141. code->code = format->code;
  142. }
  143. return 0;
  144. }
  145. static int sru_enum_frame_size(struct v4l2_subdev *subdev,
  146. struct v4l2_subdev_fh *fh,
  147. struct v4l2_subdev_frame_size_enum *fse)
  148. {
  149. struct v4l2_mbus_framefmt *format;
  150. format = v4l2_subdev_get_try_format(fh, SRU_PAD_SINK);
  151. if (fse->index || fse->code != format->code)
  152. return -EINVAL;
  153. if (fse->pad == SRU_PAD_SINK) {
  154. fse->min_width = SRU_MIN_SIZE;
  155. fse->max_width = SRU_MAX_SIZE;
  156. fse->min_height = SRU_MIN_SIZE;
  157. fse->max_height = SRU_MAX_SIZE;
  158. } else {
  159. fse->min_width = format->width;
  160. fse->min_height = format->height;
  161. if (format->width <= SRU_MAX_SIZE / 2 &&
  162. format->height <= SRU_MAX_SIZE / 2) {
  163. fse->max_width = format->width * 2;
  164. fse->max_height = format->height * 2;
  165. } else {
  166. fse->max_width = format->width;
  167. fse->max_height = format->height;
  168. }
  169. }
  170. return 0;
  171. }
  172. static int sru_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  173. struct v4l2_subdev_format *fmt)
  174. {
  175. struct vsp1_sru *sru = to_sru(subdev);
  176. fmt->format = *vsp1_entity_get_pad_format(&sru->entity, fh, fmt->pad,
  177. fmt->which);
  178. return 0;
  179. }
  180. static void sru_try_format(struct vsp1_sru *sru, struct v4l2_subdev_fh *fh,
  181. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  182. enum v4l2_subdev_format_whence which)
  183. {
  184. struct v4l2_mbus_framefmt *format;
  185. unsigned int input_area;
  186. unsigned int output_area;
  187. switch (pad) {
  188. case SRU_PAD_SINK:
  189. /* Default to YUV if the requested format is not supported. */
  190. if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  191. fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
  192. fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
  193. fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
  194. fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
  195. break;
  196. case SRU_PAD_SOURCE:
  197. /* The SRU can't perform format conversion. */
  198. format = vsp1_entity_get_pad_format(&sru->entity, fh,
  199. SRU_PAD_SINK, which);
  200. fmt->code = format->code;
  201. /* We can upscale by 2 in both direction, but not independently.
  202. * Compare the input and output rectangles areas (avoiding
  203. * integer overflows on the output): if the requested output
  204. * area is larger than 1.5^2 the input area upscale by two,
  205. * otherwise don't scale.
  206. */
  207. input_area = format->width * format->height;
  208. output_area = min(fmt->width, SRU_MAX_SIZE)
  209. * min(fmt->height, SRU_MAX_SIZE);
  210. if (fmt->width <= SRU_MAX_SIZE / 2 &&
  211. fmt->height <= SRU_MAX_SIZE / 2 &&
  212. output_area > input_area * 9 / 4) {
  213. fmt->width = format->width * 2;
  214. fmt->height = format->height * 2;
  215. } else {
  216. fmt->width = format->width;
  217. fmt->height = format->height;
  218. }
  219. break;
  220. }
  221. fmt->field = V4L2_FIELD_NONE;
  222. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  223. }
  224. static int sru_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  225. struct v4l2_subdev_format *fmt)
  226. {
  227. struct vsp1_sru *sru = to_sru(subdev);
  228. struct v4l2_mbus_framefmt *format;
  229. sru_try_format(sru, fh, fmt->pad, &fmt->format, fmt->which);
  230. format = vsp1_entity_get_pad_format(&sru->entity, fh, fmt->pad,
  231. fmt->which);
  232. *format = fmt->format;
  233. if (fmt->pad == SRU_PAD_SINK) {
  234. /* Propagate the format to the source pad. */
  235. format = vsp1_entity_get_pad_format(&sru->entity, fh,
  236. SRU_PAD_SOURCE, fmt->which);
  237. *format = fmt->format;
  238. sru_try_format(sru, fh, SRU_PAD_SOURCE, format, fmt->which);
  239. }
  240. return 0;
  241. }
  242. /* -----------------------------------------------------------------------------
  243. * V4L2 Subdevice Operations
  244. */
  245. static struct v4l2_subdev_video_ops sru_video_ops = {
  246. .s_stream = sru_s_stream,
  247. };
  248. static struct v4l2_subdev_pad_ops sru_pad_ops = {
  249. .enum_mbus_code = sru_enum_mbus_code,
  250. .enum_frame_size = sru_enum_frame_size,
  251. .get_fmt = sru_get_format,
  252. .set_fmt = sru_set_format,
  253. };
  254. static struct v4l2_subdev_ops sru_ops = {
  255. .video = &sru_video_ops,
  256. .pad = &sru_pad_ops,
  257. };
  258. /* -----------------------------------------------------------------------------
  259. * Initialization and Cleanup
  260. */
  261. struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
  262. {
  263. struct v4l2_subdev *subdev;
  264. struct vsp1_sru *sru;
  265. int ret;
  266. sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
  267. if (sru == NULL)
  268. return ERR_PTR(-ENOMEM);
  269. sru->entity.type = VSP1_ENTITY_SRU;
  270. ret = vsp1_entity_init(vsp1, &sru->entity, 2);
  271. if (ret < 0)
  272. return ERR_PTR(ret);
  273. /* Initialize the V4L2 subdev. */
  274. subdev = &sru->entity.subdev;
  275. v4l2_subdev_init(subdev, &sru_ops);
  276. subdev->entity.ops = &vsp1_media_ops;
  277. subdev->internal_ops = &vsp1_subdev_internal_ops;
  278. snprintf(subdev->name, sizeof(subdev->name), "%s sru",
  279. dev_name(vsp1->dev));
  280. v4l2_set_subdevdata(subdev, sru);
  281. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  282. vsp1_entity_init_formats(subdev, NULL);
  283. /* Initialize the control handler. */
  284. v4l2_ctrl_handler_init(&sru->ctrls, 1);
  285. v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
  286. v4l2_ctrl_handler_setup(&sru->ctrls);
  287. sru->entity.subdev.ctrl_handler = &sru->ctrls;
  288. return sru;
  289. }