vimc-scaler.c 12 KB

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