vimc-scaler.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * vimc-scaler.c Virtual Media Controller Driver
  3. *
  4. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/component.h>
  18. #include <linux/module.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/v4l2-mediabus.h>
  23. #include <media/v4l2-subdev.h>
  24. #include "vimc-common.h"
  25. #define VIMC_SCA_DRV_NAME "vimc-scaler"
  26. static unsigned int sca_mult = 3;
  27. module_param(sca_mult, uint, 0000);
  28. MODULE_PARM_DESC(sca_mult, " the image size multiplier");
  29. #define IS_SINK(pad) (!pad)
  30. #define IS_SRC(pad) (pad)
  31. #define MAX_ZOOM 8
  32. struct vimc_sca_device {
  33. struct vimc_ent_device ved;
  34. struct v4l2_subdev sd;
  35. struct device *dev;
  36. /* NOTE: the source fmt is the same as the sink
  37. * with the width and hight multiplied by mult
  38. */
  39. struct v4l2_mbus_framefmt sink_fmt;
  40. /* Values calculated when the stream starts */
  41. u8 *src_frame;
  42. unsigned int src_line_size;
  43. unsigned int bpp;
  44. };
  45. static const struct v4l2_mbus_framefmt sink_fmt_default = {
  46. .width = 640,
  47. .height = 480,
  48. .code = MEDIA_BUS_FMT_RGB888_1X24,
  49. .field = V4L2_FIELD_NONE,
  50. .colorspace = V4L2_COLORSPACE_DEFAULT,
  51. };
  52. static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
  53. struct v4l2_subdev_pad_config *cfg)
  54. {
  55. struct v4l2_mbus_framefmt *mf;
  56. unsigned int i;
  57. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  58. *mf = sink_fmt_default;
  59. for (i = 1; i < sd->entity.num_pads; i++) {
  60. mf = v4l2_subdev_get_try_format(sd, cfg, i);
  61. *mf = sink_fmt_default;
  62. mf->width = mf->width * sca_mult;
  63. mf->height = mf->height * sca_mult;
  64. }
  65. return 0;
  66. }
  67. static int vimc_sca_enum_mbus_code(struct v4l2_subdev *sd,
  68. struct v4l2_subdev_pad_config *cfg,
  69. struct v4l2_subdev_mbus_code_enum *code)
  70. {
  71. const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
  72. /* We don't support bayer format */
  73. if (!vpix || vpix->bayer)
  74. return -EINVAL;
  75. code->code = vpix->code;
  76. return 0;
  77. }
  78. static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
  79. struct v4l2_subdev_pad_config *cfg,
  80. struct v4l2_subdev_frame_size_enum *fse)
  81. {
  82. const struct vimc_pix_map *vpix;
  83. if (fse->index)
  84. return -EINVAL;
  85. /* Only accept code in the pix map table in non bayer format */
  86. vpix = vimc_pix_map_by_code(fse->code);
  87. if (!vpix || vpix->bayer)
  88. return -EINVAL;
  89. fse->min_width = VIMC_FRAME_MIN_WIDTH;
  90. fse->min_height = VIMC_FRAME_MIN_HEIGHT;
  91. if (IS_SINK(fse->pad)) {
  92. fse->max_width = VIMC_FRAME_MAX_WIDTH;
  93. fse->max_height = VIMC_FRAME_MAX_HEIGHT;
  94. } else {
  95. fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
  96. fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
  97. }
  98. return 0;
  99. }
  100. static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
  101. struct v4l2_subdev_pad_config *cfg,
  102. struct v4l2_subdev_format *format)
  103. {
  104. struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
  105. /* Get the current sink format */
  106. format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
  107. *v4l2_subdev_get_try_format(sd, cfg, 0) :
  108. vsca->sink_fmt;
  109. /* Scale the frame size for the source pad */
  110. if (IS_SRC(format->pad)) {
  111. format->format.width = vsca->sink_fmt.width * sca_mult;
  112. format->format.height = vsca->sink_fmt.height * sca_mult;
  113. }
  114. return 0;
  115. }
  116. static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
  117. {
  118. const struct vimc_pix_map *vpix;
  119. /* Only accept code in the pix map table in non bayer format */
  120. vpix = vimc_pix_map_by_code(fmt->code);
  121. if (!vpix || vpix->bayer)
  122. fmt->code = sink_fmt_default.code;
  123. fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
  124. VIMC_FRAME_MAX_WIDTH) & ~1;
  125. fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
  126. VIMC_FRAME_MAX_HEIGHT) & ~1;
  127. if (fmt->field == V4L2_FIELD_ANY)
  128. fmt->field = sink_fmt_default.field;
  129. vimc_colorimetry_clamp(fmt);
  130. }
  131. static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
  132. struct v4l2_subdev_pad_config *cfg,
  133. struct v4l2_subdev_format *fmt)
  134. {
  135. struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
  136. struct v4l2_mbus_framefmt *sink_fmt;
  137. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  138. /* Do not change the format while stream is on */
  139. if (vsca->src_frame)
  140. return -EBUSY;
  141. sink_fmt = &vsca->sink_fmt;
  142. } else {
  143. sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  144. }
  145. /*
  146. * Do not change the format of the source pad,
  147. * it is propagated from the sink
  148. */
  149. if (IS_SRC(fmt->pad)) {
  150. fmt->format = *sink_fmt;
  151. fmt->format.width = sink_fmt->width * sca_mult;
  152. fmt->format.height = sink_fmt->height * sca_mult;
  153. } else {
  154. /* Set the new format in the sink pad */
  155. vimc_sca_adjust_sink_fmt(&fmt->format);
  156. dev_dbg(vsca->dev, "%s: sink format update: "
  157. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  158. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
  159. /* old */
  160. sink_fmt->width, sink_fmt->height, sink_fmt->code,
  161. sink_fmt->colorspace, sink_fmt->quantization,
  162. sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
  163. /* new */
  164. fmt->format.width, fmt->format.height, fmt->format.code,
  165. fmt->format.colorspace, fmt->format.quantization,
  166. fmt->format.xfer_func, fmt->format.ycbcr_enc);
  167. *sink_fmt = fmt->format;
  168. }
  169. return 0;
  170. }
  171. static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
  172. .init_cfg = vimc_sca_init_cfg,
  173. .enum_mbus_code = vimc_sca_enum_mbus_code,
  174. .enum_frame_size = vimc_sca_enum_frame_size,
  175. .get_fmt = vimc_sca_get_fmt,
  176. .set_fmt = vimc_sca_set_fmt,
  177. };
  178. static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
  179. {
  180. struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
  181. if (enable) {
  182. const struct vimc_pix_map *vpix;
  183. unsigned int frame_size;
  184. if (vsca->src_frame)
  185. return 0;
  186. /* Save the bytes per pixel of the sink */
  187. vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
  188. vsca->bpp = vpix->bpp;
  189. /* Calculate the width in bytes of the src frame */
  190. vsca->src_line_size = vsca->sink_fmt.width *
  191. sca_mult * vsca->bpp;
  192. /* Calculate the frame size of the source pad */
  193. frame_size = vsca->src_line_size * vsca->sink_fmt.height *
  194. sca_mult;
  195. /* Allocate the frame buffer. Use vmalloc to be able to
  196. * allocate a large amount of memory
  197. */
  198. vsca->src_frame = vmalloc(frame_size);
  199. if (!vsca->src_frame)
  200. return -ENOMEM;
  201. } else {
  202. if (!vsca->src_frame)
  203. return 0;
  204. vfree(vsca->src_frame);
  205. vsca->src_frame = NULL;
  206. }
  207. return 0;
  208. }
  209. static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
  210. .s_stream = vimc_sca_s_stream,
  211. };
  212. static const struct v4l2_subdev_ops vimc_sca_ops = {
  213. .pad = &vimc_sca_pad_ops,
  214. .video = &vimc_sca_video_ops,
  215. };
  216. static void vimc_sca_fill_pix(u8 *const ptr,
  217. const u8 *const pixel,
  218. const unsigned int bpp)
  219. {
  220. unsigned int i;
  221. /* copy the pixel to the pointer */
  222. for (i = 0; i < bpp; i++)
  223. ptr[i] = pixel[i];
  224. }
  225. static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
  226. const unsigned int lin, const unsigned int col,
  227. const u8 *const sink_frame)
  228. {
  229. unsigned int i, j, index;
  230. const u8 *pixel;
  231. /* Point to the pixel value in position (lin, col) in the sink frame */
  232. index = VIMC_FRAME_INDEX(lin, col,
  233. vsca->sink_fmt.width,
  234. vsca->bpp);
  235. pixel = &sink_frame[index];
  236. dev_dbg(vsca->dev,
  237. "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
  238. vsca->sd.name, lin, col, index);
  239. /* point to the place we are going to put the first pixel
  240. * in the scaled src frame
  241. */
  242. index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
  243. vsca->sink_fmt.width * sca_mult, vsca->bpp);
  244. dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
  245. vsca->sd.name, lin * sca_mult, col * sca_mult, index);
  246. /* Repeat this pixel mult times */
  247. for (i = 0; i < sca_mult; i++) {
  248. /* Iterate through each beginning of a
  249. * pixel repetition in a line
  250. */
  251. for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
  252. dev_dbg(vsca->dev,
  253. "sca: %s: sca: scale_pix src pos %d\n",
  254. vsca->sd.name, index + j);
  255. /* copy the pixel to the position index + j */
  256. vimc_sca_fill_pix(&vsca->src_frame[index + j],
  257. pixel, vsca->bpp);
  258. }
  259. /* move the index to the next line */
  260. index += vsca->src_line_size;
  261. }
  262. }
  263. static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
  264. const u8 *const sink_frame)
  265. {
  266. unsigned int i, j;
  267. /* Scale each pixel from the original sink frame */
  268. /* TODO: implement scale down, only scale up is supported for now */
  269. for (i = 0; i < vsca->sink_fmt.height; i++)
  270. for (j = 0; j < vsca->sink_fmt.width; j++)
  271. vimc_sca_scale_pix(vsca, i, j, sink_frame);
  272. }
  273. static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
  274. const void *sink_frame)
  275. {
  276. struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
  277. ved);
  278. /* If the stream in this node is not active, just return */
  279. if (!vsca->src_frame)
  280. return ERR_PTR(-EINVAL);
  281. vimc_sca_fill_src_frame(vsca, sink_frame);
  282. return vsca->src_frame;
  283. };
  284. static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
  285. void *master_data)
  286. {
  287. struct vimc_ent_device *ved = dev_get_drvdata(comp);
  288. struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
  289. ved);
  290. vimc_ent_sd_unregister(ved, &vsca->sd);
  291. kfree(vsca);
  292. }
  293. static int vimc_sca_comp_bind(struct device *comp, struct device *master,
  294. void *master_data)
  295. {
  296. struct v4l2_device *v4l2_dev = master_data;
  297. struct vimc_platform_data *pdata = comp->platform_data;
  298. struct vimc_sca_device *vsca;
  299. int ret;
  300. /* Allocate the vsca struct */
  301. vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
  302. if (!vsca)
  303. return -ENOMEM;
  304. /* Initialize ved and sd */
  305. ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
  306. pdata->entity_name,
  307. MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
  308. (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
  309. MEDIA_PAD_FL_SOURCE},
  310. &vimc_sca_ops);
  311. if (ret) {
  312. kfree(vsca);
  313. return ret;
  314. }
  315. vsca->ved.process_frame = vimc_sca_process_frame;
  316. dev_set_drvdata(comp, &vsca->ved);
  317. vsca->dev = comp;
  318. /* Initialize the frame format */
  319. vsca->sink_fmt = sink_fmt_default;
  320. return 0;
  321. }
  322. static const struct component_ops vimc_sca_comp_ops = {
  323. .bind = vimc_sca_comp_bind,
  324. .unbind = vimc_sca_comp_unbind,
  325. };
  326. static int vimc_sca_probe(struct platform_device *pdev)
  327. {
  328. return component_add(&pdev->dev, &vimc_sca_comp_ops);
  329. }
  330. static int vimc_sca_remove(struct platform_device *pdev)
  331. {
  332. component_del(&pdev->dev, &vimc_sca_comp_ops);
  333. return 0;
  334. }
  335. static const struct platform_device_id vimc_sca_driver_ids[] = {
  336. {
  337. .name = VIMC_SCA_DRV_NAME,
  338. },
  339. { }
  340. };
  341. static struct platform_driver vimc_sca_pdrv = {
  342. .probe = vimc_sca_probe,
  343. .remove = vimc_sca_remove,
  344. .id_table = vimc_sca_driver_ids,
  345. .driver = {
  346. .name = VIMC_SCA_DRV_NAME,
  347. },
  348. };
  349. module_platform_driver(vimc_sca_pdrv);
  350. MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
  351. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
  352. MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
  353. MODULE_LICENSE("GPL");