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