vsp1_sru.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. vsp1_sru_write(sru, VI6_SRU_CTRL0, ctrl0);
  130. mutex_unlock(sru->ctrls.lock);
  131. vsp1_sru_write(sru, VI6_SRU_CTRL1, VI6_SRU_CTRL1_PARAM5);
  132. return 0;
  133. }
  134. /* -----------------------------------------------------------------------------
  135. * V4L2 Subdevice Pad Operations
  136. */
  137. static int sru_enum_mbus_code(struct v4l2_subdev *subdev,
  138. struct v4l2_subdev_pad_config *cfg,
  139. struct v4l2_subdev_mbus_code_enum *code)
  140. {
  141. static const unsigned int codes[] = {
  142. MEDIA_BUS_FMT_ARGB8888_1X32,
  143. MEDIA_BUS_FMT_AYUV8_1X32,
  144. };
  145. struct vsp1_sru *sru = to_sru(subdev);
  146. struct v4l2_mbus_framefmt *format;
  147. if (code->pad == SRU_PAD_SINK) {
  148. if (code->index >= ARRAY_SIZE(codes))
  149. return -EINVAL;
  150. code->code = codes[code->index];
  151. } else {
  152. /* The SRU can't perform format conversion, the sink format is
  153. * always identical to the source format.
  154. */
  155. if (code->index)
  156. return -EINVAL;
  157. format = vsp1_entity_get_pad_format(&sru->entity, cfg,
  158. SRU_PAD_SINK, code->which);
  159. code->code = format->code;
  160. }
  161. return 0;
  162. }
  163. static int sru_enum_frame_size(struct v4l2_subdev *subdev,
  164. struct v4l2_subdev_pad_config *cfg,
  165. struct v4l2_subdev_frame_size_enum *fse)
  166. {
  167. struct vsp1_sru *sru = to_sru(subdev);
  168. struct v4l2_mbus_framefmt *format;
  169. format = vsp1_entity_get_pad_format(&sru->entity, cfg,
  170. SRU_PAD_SINK, fse->which);
  171. if (fse->index || fse->code != format->code)
  172. return -EINVAL;
  173. if (fse->pad == SRU_PAD_SINK) {
  174. fse->min_width = SRU_MIN_SIZE;
  175. fse->max_width = SRU_MAX_SIZE;
  176. fse->min_height = SRU_MIN_SIZE;
  177. fse->max_height = SRU_MAX_SIZE;
  178. } else {
  179. fse->min_width = format->width;
  180. fse->min_height = format->height;
  181. if (format->width <= SRU_MAX_SIZE / 2 &&
  182. format->height <= SRU_MAX_SIZE / 2) {
  183. fse->max_width = format->width * 2;
  184. fse->max_height = format->height * 2;
  185. } else {
  186. fse->max_width = format->width;
  187. fse->max_height = format->height;
  188. }
  189. }
  190. return 0;
  191. }
  192. static int sru_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  193. struct v4l2_subdev_format *fmt)
  194. {
  195. struct vsp1_sru *sru = to_sru(subdev);
  196. fmt->format = *vsp1_entity_get_pad_format(&sru->entity, cfg, fmt->pad,
  197. fmt->which);
  198. return 0;
  199. }
  200. static void sru_try_format(struct vsp1_sru *sru, struct v4l2_subdev_pad_config *cfg,
  201. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  202. enum v4l2_subdev_format_whence which)
  203. {
  204. struct v4l2_mbus_framefmt *format;
  205. unsigned int input_area;
  206. unsigned int output_area;
  207. switch (pad) {
  208. case SRU_PAD_SINK:
  209. /* Default to YUV if the requested format is not supported. */
  210. if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  211. fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
  212. fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
  213. fmt->width = clamp(fmt->width, SRU_MIN_SIZE, SRU_MAX_SIZE);
  214. fmt->height = clamp(fmt->height, SRU_MIN_SIZE, SRU_MAX_SIZE);
  215. break;
  216. case SRU_PAD_SOURCE:
  217. /* The SRU can't perform format conversion. */
  218. format = vsp1_entity_get_pad_format(&sru->entity, cfg,
  219. SRU_PAD_SINK, which);
  220. fmt->code = format->code;
  221. /* We can upscale by 2 in both direction, but not independently.
  222. * Compare the input and output rectangles areas (avoiding
  223. * integer overflows on the output): if the requested output
  224. * area is larger than 1.5^2 the input area upscale by two,
  225. * otherwise don't scale.
  226. */
  227. input_area = format->width * format->height;
  228. output_area = min(fmt->width, SRU_MAX_SIZE)
  229. * min(fmt->height, SRU_MAX_SIZE);
  230. if (fmt->width <= SRU_MAX_SIZE / 2 &&
  231. fmt->height <= SRU_MAX_SIZE / 2 &&
  232. output_area > input_area * 9 / 4) {
  233. fmt->width = format->width * 2;
  234. fmt->height = format->height * 2;
  235. } else {
  236. fmt->width = format->width;
  237. fmt->height = format->height;
  238. }
  239. break;
  240. }
  241. fmt->field = V4L2_FIELD_NONE;
  242. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  243. }
  244. static int sru_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  245. struct v4l2_subdev_format *fmt)
  246. {
  247. struct vsp1_sru *sru = to_sru(subdev);
  248. struct v4l2_mbus_framefmt *format;
  249. sru_try_format(sru, cfg, fmt->pad, &fmt->format, fmt->which);
  250. format = vsp1_entity_get_pad_format(&sru->entity, cfg, fmt->pad,
  251. fmt->which);
  252. *format = fmt->format;
  253. if (fmt->pad == SRU_PAD_SINK) {
  254. /* Propagate the format to the source pad. */
  255. format = vsp1_entity_get_pad_format(&sru->entity, cfg,
  256. SRU_PAD_SOURCE, fmt->which);
  257. *format = fmt->format;
  258. sru_try_format(sru, cfg, SRU_PAD_SOURCE, format, fmt->which);
  259. }
  260. return 0;
  261. }
  262. /* -----------------------------------------------------------------------------
  263. * V4L2 Subdevice Operations
  264. */
  265. static struct v4l2_subdev_video_ops sru_video_ops = {
  266. .s_stream = sru_s_stream,
  267. };
  268. static struct v4l2_subdev_pad_ops sru_pad_ops = {
  269. .enum_mbus_code = sru_enum_mbus_code,
  270. .enum_frame_size = sru_enum_frame_size,
  271. .get_fmt = sru_get_format,
  272. .set_fmt = sru_set_format,
  273. };
  274. static struct v4l2_subdev_ops sru_ops = {
  275. .video = &sru_video_ops,
  276. .pad = &sru_pad_ops,
  277. };
  278. /* -----------------------------------------------------------------------------
  279. * Initialization and Cleanup
  280. */
  281. struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
  282. {
  283. struct v4l2_subdev *subdev;
  284. struct vsp1_sru *sru;
  285. int ret;
  286. sru = devm_kzalloc(vsp1->dev, sizeof(*sru), GFP_KERNEL);
  287. if (sru == NULL)
  288. return ERR_PTR(-ENOMEM);
  289. sru->entity.type = VSP1_ENTITY_SRU;
  290. ret = vsp1_entity_init(vsp1, &sru->entity, 2);
  291. if (ret < 0)
  292. return ERR_PTR(ret);
  293. /* Initialize the V4L2 subdev. */
  294. subdev = &sru->entity.subdev;
  295. v4l2_subdev_init(subdev, &sru_ops);
  296. subdev->entity.ops = &vsp1_media_ops;
  297. subdev->internal_ops = &vsp1_subdev_internal_ops;
  298. snprintf(subdev->name, sizeof(subdev->name), "%s sru",
  299. dev_name(vsp1->dev));
  300. v4l2_set_subdevdata(subdev, sru);
  301. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  302. vsp1_entity_init_formats(subdev, NULL);
  303. /* Initialize the control handler. */
  304. v4l2_ctrl_handler_init(&sru->ctrls, 1);
  305. v4l2_ctrl_new_custom(&sru->ctrls, &sru_intensity_control, NULL);
  306. sru->entity.subdev.ctrl_handler = &sru->ctrls;
  307. if (sru->ctrls.error) {
  308. dev_err(vsp1->dev, "sru: failed to initialize controls\n");
  309. ret = sru->ctrls.error;
  310. vsp1_entity_destroy(&sru->entity);
  311. return ERR_PTR(ret);
  312. }
  313. return sru;
  314. }