vsp1_sru.c 9.9 KB

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