vimc-debayer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * vimc-debayer.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_DEB_DRV_NAME "vimc-debayer"
  26. static unsigned int deb_mean_win_size = 3;
  27. module_param(deb_mean_win_size, uint, 0000);
  28. MODULE_PARM_DESC(deb_mean_win_size, " the window size to calculate the mean.\n"
  29. "NOTE: the window size need to be an odd number, as the main pixel "
  30. "stays in the center of the window, otherwise the next odd number "
  31. "is considered");
  32. #define IS_SINK(pad) (!pad)
  33. #define IS_SRC(pad) (pad)
  34. enum vimc_deb_rgb_colors {
  35. VIMC_DEB_RED = 0,
  36. VIMC_DEB_GREEN = 1,
  37. VIMC_DEB_BLUE = 2,
  38. };
  39. struct vimc_deb_pix_map {
  40. u32 code;
  41. enum vimc_deb_rgb_colors order[2][2];
  42. };
  43. struct vimc_deb_device {
  44. struct vimc_ent_device ved;
  45. struct v4l2_subdev sd;
  46. struct device *dev;
  47. /* The active format */
  48. struct v4l2_mbus_framefmt sink_fmt;
  49. u32 src_code;
  50. void (*set_rgb_src)(struct vimc_deb_device *vdeb, unsigned int lin,
  51. unsigned int col, unsigned int rgb[3]);
  52. /* Values calculated when the stream starts */
  53. u8 *src_frame;
  54. const struct vimc_deb_pix_map *sink_pix_map;
  55. unsigned int sink_bpp;
  56. };
  57. static const struct v4l2_mbus_framefmt sink_fmt_default = {
  58. .width = 640,
  59. .height = 480,
  60. .code = MEDIA_BUS_FMT_RGB888_1X24,
  61. .field = V4L2_FIELD_NONE,
  62. .colorspace = V4L2_COLORSPACE_DEFAULT,
  63. };
  64. static const struct vimc_deb_pix_map vimc_deb_pix_map_list[] = {
  65. {
  66. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  67. .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
  68. { VIMC_DEB_GREEN, VIMC_DEB_RED } }
  69. },
  70. {
  71. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  72. .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
  73. { VIMC_DEB_RED, VIMC_DEB_GREEN } }
  74. },
  75. {
  76. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  77. .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
  78. { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
  79. },
  80. {
  81. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  82. .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
  83. { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
  84. },
  85. {
  86. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  87. .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
  88. { VIMC_DEB_GREEN, VIMC_DEB_RED } }
  89. },
  90. {
  91. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  92. .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
  93. { VIMC_DEB_RED, VIMC_DEB_GREEN } }
  94. },
  95. {
  96. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  97. .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
  98. { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
  99. },
  100. {
  101. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  102. .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
  103. { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
  104. },
  105. {
  106. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  107. .order = { { VIMC_DEB_BLUE, VIMC_DEB_GREEN },
  108. { VIMC_DEB_GREEN, VIMC_DEB_RED } }
  109. },
  110. {
  111. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  112. .order = { { VIMC_DEB_GREEN, VIMC_DEB_BLUE },
  113. { VIMC_DEB_RED, VIMC_DEB_GREEN } }
  114. },
  115. {
  116. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  117. .order = { { VIMC_DEB_GREEN, VIMC_DEB_RED },
  118. { VIMC_DEB_BLUE, VIMC_DEB_GREEN } }
  119. },
  120. {
  121. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  122. .order = { { VIMC_DEB_RED, VIMC_DEB_GREEN },
  123. { VIMC_DEB_GREEN, VIMC_DEB_BLUE } }
  124. },
  125. };
  126. static const struct vimc_deb_pix_map *vimc_deb_pix_map_by_code(u32 code)
  127. {
  128. unsigned int i;
  129. for (i = 0; i < ARRAY_SIZE(vimc_deb_pix_map_list); i++)
  130. if (vimc_deb_pix_map_list[i].code == code)
  131. return &vimc_deb_pix_map_list[i];
  132. return NULL;
  133. }
  134. static int vimc_deb_init_cfg(struct v4l2_subdev *sd,
  135. struct v4l2_subdev_pad_config *cfg)
  136. {
  137. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  138. struct v4l2_mbus_framefmt *mf;
  139. unsigned int i;
  140. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  141. *mf = sink_fmt_default;
  142. for (i = 1; i < sd->entity.num_pads; i++) {
  143. mf = v4l2_subdev_get_try_format(sd, cfg, i);
  144. *mf = sink_fmt_default;
  145. mf->code = vdeb->src_code;
  146. }
  147. return 0;
  148. }
  149. static int vimc_deb_enum_mbus_code(struct v4l2_subdev *sd,
  150. struct v4l2_subdev_pad_config *cfg,
  151. struct v4l2_subdev_mbus_code_enum *code)
  152. {
  153. /* We only support one format for source pads */
  154. if (IS_SRC(code->pad)) {
  155. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  156. if (code->index)
  157. return -EINVAL;
  158. code->code = vdeb->src_code;
  159. } else {
  160. if (code->index >= ARRAY_SIZE(vimc_deb_pix_map_list))
  161. return -EINVAL;
  162. code->code = vimc_deb_pix_map_list[code->index].code;
  163. }
  164. return 0;
  165. }
  166. static int vimc_deb_enum_frame_size(struct v4l2_subdev *sd,
  167. struct v4l2_subdev_pad_config *cfg,
  168. struct v4l2_subdev_frame_size_enum *fse)
  169. {
  170. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  171. if (fse->index)
  172. return -EINVAL;
  173. if (IS_SINK(fse->pad)) {
  174. const struct vimc_deb_pix_map *vpix =
  175. vimc_deb_pix_map_by_code(fse->code);
  176. if (!vpix)
  177. return -EINVAL;
  178. } else if (fse->code != vdeb->src_code) {
  179. return -EINVAL;
  180. }
  181. fse->min_width = VIMC_FRAME_MIN_WIDTH;
  182. fse->max_width = VIMC_FRAME_MAX_WIDTH;
  183. fse->min_height = VIMC_FRAME_MIN_HEIGHT;
  184. fse->max_height = VIMC_FRAME_MAX_HEIGHT;
  185. return 0;
  186. }
  187. static int vimc_deb_get_fmt(struct v4l2_subdev *sd,
  188. struct v4l2_subdev_pad_config *cfg,
  189. struct v4l2_subdev_format *fmt)
  190. {
  191. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  192. /* Get the current sink format */
  193. fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
  194. *v4l2_subdev_get_try_format(sd, cfg, 0) :
  195. vdeb->sink_fmt;
  196. /* Set the right code for the source pad */
  197. if (IS_SRC(fmt->pad))
  198. fmt->format.code = vdeb->src_code;
  199. return 0;
  200. }
  201. static void vimc_deb_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
  202. {
  203. const struct vimc_deb_pix_map *vpix;
  204. /* Don't accept a code that is not on the debayer table */
  205. vpix = vimc_deb_pix_map_by_code(fmt->code);
  206. if (!vpix)
  207. fmt->code = sink_fmt_default.code;
  208. fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
  209. VIMC_FRAME_MAX_WIDTH) & ~1;
  210. fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
  211. VIMC_FRAME_MAX_HEIGHT) & ~1;
  212. if (fmt->field == V4L2_FIELD_ANY)
  213. fmt->field = sink_fmt_default.field;
  214. vimc_colorimetry_clamp(fmt);
  215. }
  216. static int vimc_deb_set_fmt(struct v4l2_subdev *sd,
  217. struct v4l2_subdev_pad_config *cfg,
  218. struct v4l2_subdev_format *fmt)
  219. {
  220. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  221. struct v4l2_mbus_framefmt *sink_fmt;
  222. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  223. /* Do not change the format while stream is on */
  224. if (vdeb->src_frame)
  225. return -EBUSY;
  226. sink_fmt = &vdeb->sink_fmt;
  227. } else {
  228. sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  229. }
  230. /*
  231. * Do not change the format of the source pad,
  232. * it is propagated from the sink
  233. */
  234. if (IS_SRC(fmt->pad)) {
  235. fmt->format = *sink_fmt;
  236. /* TODO: Add support for other formats */
  237. fmt->format.code = vdeb->src_code;
  238. } else {
  239. /* Set the new format in the sink pad */
  240. vimc_deb_adjust_sink_fmt(&fmt->format);
  241. dev_dbg(vdeb->dev, "%s: sink format update: "
  242. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  243. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vdeb->sd.name,
  244. /* old */
  245. sink_fmt->width, sink_fmt->height, sink_fmt->code,
  246. sink_fmt->colorspace, sink_fmt->quantization,
  247. sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
  248. /* new */
  249. fmt->format.width, fmt->format.height, fmt->format.code,
  250. fmt->format.colorspace, fmt->format.quantization,
  251. fmt->format.xfer_func, fmt->format.ycbcr_enc);
  252. *sink_fmt = fmt->format;
  253. }
  254. return 0;
  255. }
  256. static const struct v4l2_subdev_pad_ops vimc_deb_pad_ops = {
  257. .init_cfg = vimc_deb_init_cfg,
  258. .enum_mbus_code = vimc_deb_enum_mbus_code,
  259. .enum_frame_size = vimc_deb_enum_frame_size,
  260. .get_fmt = vimc_deb_get_fmt,
  261. .set_fmt = vimc_deb_set_fmt,
  262. };
  263. static void vimc_deb_set_rgb_mbus_fmt_rgb888_1x24(struct vimc_deb_device *vdeb,
  264. unsigned int lin,
  265. unsigned int col,
  266. unsigned int rgb[3])
  267. {
  268. unsigned int i, index;
  269. index = VIMC_FRAME_INDEX(lin, col, vdeb->sink_fmt.width, 3);
  270. for (i = 0; i < 3; i++)
  271. vdeb->src_frame[index + i] = rgb[i];
  272. }
  273. static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
  274. {
  275. struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
  276. if (enable) {
  277. const struct vimc_pix_map *vpix;
  278. unsigned int frame_size;
  279. if (vdeb->src_frame)
  280. return 0;
  281. /* Calculate the frame size of the source pad */
  282. vpix = vimc_pix_map_by_code(vdeb->src_code);
  283. frame_size = vdeb->sink_fmt.width * vdeb->sink_fmt.height *
  284. vpix->bpp;
  285. /* Save the bytes per pixel of the sink */
  286. vpix = vimc_pix_map_by_code(vdeb->sink_fmt.code);
  287. vdeb->sink_bpp = vpix->bpp;
  288. /* Get the corresponding pixel map from the table */
  289. vdeb->sink_pix_map =
  290. vimc_deb_pix_map_by_code(vdeb->sink_fmt.code);
  291. /*
  292. * Allocate the frame buffer. Use vmalloc to be able to
  293. * allocate a large amount of memory
  294. */
  295. vdeb->src_frame = vmalloc(frame_size);
  296. if (!vdeb->src_frame)
  297. return -ENOMEM;
  298. } else {
  299. if (!vdeb->src_frame)
  300. return 0;
  301. vfree(vdeb->src_frame);
  302. vdeb->src_frame = NULL;
  303. }
  304. return 0;
  305. }
  306. static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
  307. .s_stream = vimc_deb_s_stream,
  308. };
  309. static const struct v4l2_subdev_ops vimc_deb_ops = {
  310. .pad = &vimc_deb_pad_ops,
  311. .video = &vimc_deb_video_ops,
  312. };
  313. static unsigned int vimc_deb_get_val(const u8 *bytes,
  314. const unsigned int n_bytes)
  315. {
  316. unsigned int i;
  317. unsigned int acc = 0;
  318. for (i = 0; i < n_bytes; i++)
  319. acc = acc + (bytes[i] << (8 * i));
  320. return acc;
  321. }
  322. static void vimc_deb_calc_rgb_sink(struct vimc_deb_device *vdeb,
  323. const u8 *frame,
  324. const unsigned int lin,
  325. const unsigned int col,
  326. unsigned int rgb[3])
  327. {
  328. unsigned int i, seek, wlin, wcol;
  329. unsigned int n_rgb[3] = {0, 0, 0};
  330. for (i = 0; i < 3; i++)
  331. rgb[i] = 0;
  332. /*
  333. * Calculate how many we need to subtract to get to the pixel in
  334. * the top left corner of the mean window (considering the current
  335. * pixel as the center)
  336. */
  337. seek = deb_mean_win_size / 2;
  338. /* Sum the values of the colors in the mean window */
  339. dev_dbg(vdeb->dev,
  340. "deb: %s: --- Calc pixel %dx%d, window mean %d, seek %d ---\n",
  341. vdeb->sd.name, lin, col, vdeb->sink_fmt.height, seek);
  342. /*
  343. * Iterate through all the lines in the mean window, start
  344. * with zero if the pixel is outside the frame and don't pass
  345. * the height when the pixel is in the bottom border of the
  346. * frame
  347. */
  348. for (wlin = seek > lin ? 0 : lin - seek;
  349. wlin < lin + seek + 1 && wlin < vdeb->sink_fmt.height;
  350. wlin++) {
  351. /*
  352. * Iterate through all the columns in the mean window, start
  353. * with zero if the pixel is outside the frame and don't pass
  354. * the width when the pixel is in the right border of the
  355. * frame
  356. */
  357. for (wcol = seek > col ? 0 : col - seek;
  358. wcol < col + seek + 1 && wcol < vdeb->sink_fmt.width;
  359. wcol++) {
  360. enum vimc_deb_rgb_colors color;
  361. unsigned int index;
  362. /* Check which color this pixel is */
  363. color = vdeb->sink_pix_map->order[wlin % 2][wcol % 2];
  364. index = VIMC_FRAME_INDEX(wlin, wcol,
  365. vdeb->sink_fmt.width,
  366. vdeb->sink_bpp);
  367. dev_dbg(vdeb->dev,
  368. "deb: %s: RGB CALC: frame index %d, win pos %dx%d, color %d\n",
  369. vdeb->sd.name, index, wlin, wcol, color);
  370. /* Get its value */
  371. rgb[color] = rgb[color] +
  372. vimc_deb_get_val(&frame[index], vdeb->sink_bpp);
  373. /* Save how many values we already added */
  374. n_rgb[color]++;
  375. dev_dbg(vdeb->dev, "deb: %s: RGB CALC: val %d, n %d\n",
  376. vdeb->sd.name, rgb[color], n_rgb[color]);
  377. }
  378. }
  379. /* Calculate the mean */
  380. for (i = 0; i < 3; i++) {
  381. dev_dbg(vdeb->dev,
  382. "deb: %s: PRE CALC: %dx%d Color %d, val %d, n %d\n",
  383. vdeb->sd.name, lin, col, i, rgb[i], n_rgb[i]);
  384. if (n_rgb[i])
  385. rgb[i] = rgb[i] / n_rgb[i];
  386. dev_dbg(vdeb->dev,
  387. "deb: %s: FINAL CALC: %dx%d Color %d, val %d\n",
  388. vdeb->sd.name, lin, col, i, rgb[i]);
  389. }
  390. }
  391. static void *vimc_deb_process_frame(struct vimc_ent_device *ved,
  392. const void *sink_frame)
  393. {
  394. struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
  395. ved);
  396. unsigned int rgb[3];
  397. unsigned int i, j;
  398. /* If the stream in this node is not active, just return */
  399. if (!vdeb->src_frame)
  400. return ERR_PTR(-EINVAL);
  401. for (i = 0; i < vdeb->sink_fmt.height; i++)
  402. for (j = 0; j < vdeb->sink_fmt.width; j++) {
  403. vimc_deb_calc_rgb_sink(vdeb, sink_frame, i, j, rgb);
  404. vdeb->set_rgb_src(vdeb, i, j, rgb);
  405. }
  406. return vdeb->src_frame;
  407. }
  408. static void vimc_deb_comp_unbind(struct device *comp, struct device *master,
  409. void *master_data)
  410. {
  411. struct vimc_ent_device *ved = dev_get_drvdata(comp);
  412. struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
  413. ved);
  414. vimc_ent_sd_unregister(ved, &vdeb->sd);
  415. kfree(vdeb);
  416. }
  417. static int vimc_deb_comp_bind(struct device *comp, struct device *master,
  418. void *master_data)
  419. {
  420. struct v4l2_device *v4l2_dev = master_data;
  421. struct vimc_platform_data *pdata = comp->platform_data;
  422. struct vimc_deb_device *vdeb;
  423. int ret;
  424. /* Allocate the vdeb struct */
  425. vdeb = kzalloc(sizeof(*vdeb), GFP_KERNEL);
  426. if (!vdeb)
  427. return -ENOMEM;
  428. /* Initialize ved and sd */
  429. ret = vimc_ent_sd_register(&vdeb->ved, &vdeb->sd, v4l2_dev,
  430. pdata->entity_name,
  431. MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV, 2,
  432. (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
  433. MEDIA_PAD_FL_SOURCE},
  434. &vimc_deb_ops);
  435. if (ret) {
  436. kfree(vdeb);
  437. return ret;
  438. }
  439. vdeb->ved.process_frame = vimc_deb_process_frame;
  440. dev_set_drvdata(comp, &vdeb->ved);
  441. vdeb->dev = comp;
  442. /* Initialize the frame format */
  443. vdeb->sink_fmt = sink_fmt_default;
  444. /*
  445. * TODO: Add support for more output formats, we only support
  446. * RGB888 for now
  447. * NOTE: the src format is always the same as the sink, except
  448. * for the code
  449. */
  450. vdeb->src_code = MEDIA_BUS_FMT_RGB888_1X24;
  451. vdeb->set_rgb_src = vimc_deb_set_rgb_mbus_fmt_rgb888_1x24;
  452. return 0;
  453. }
  454. static const struct component_ops vimc_deb_comp_ops = {
  455. .bind = vimc_deb_comp_bind,
  456. .unbind = vimc_deb_comp_unbind,
  457. };
  458. static int vimc_deb_probe(struct platform_device *pdev)
  459. {
  460. return component_add(&pdev->dev, &vimc_deb_comp_ops);
  461. }
  462. static int vimc_deb_remove(struct platform_device *pdev)
  463. {
  464. component_del(&pdev->dev, &vimc_deb_comp_ops);
  465. return 0;
  466. }
  467. static const struct platform_device_id vimc_deb_driver_ids[] = {
  468. {
  469. .name = VIMC_DEB_DRV_NAME,
  470. },
  471. { }
  472. };
  473. static struct platform_driver vimc_deb_pdrv = {
  474. .probe = vimc_deb_probe,
  475. .remove = vimc_deb_remove,
  476. .id_table = vimc_deb_driver_ids,
  477. .driver = {
  478. .name = VIMC_DEB_DRV_NAME,
  479. },
  480. };
  481. module_platform_driver(vimc_deb_pdrv);
  482. MODULE_DEVICE_TABLE(platform, vimc_deb_driver_ids);
  483. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Debayer");
  484. MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
  485. MODULE_LICENSE("GPL");