vimc-debayer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vimc-debayer.c Virtual Media Controller Driver
  4. *
  5. * Copyright (C) 2015-2017 Helen Koike <[email protected]>
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/v4l2-mediabus.h>
  11. #include <media/v4l2-ctrls.h>
  12. #include <media/v4l2-event.h>
  13. #include <media/v4l2-subdev.h>
  14. #include "vimc-common.h"
  15. enum vimc_debayer_rgb_colors {
  16. VIMC_DEBAYER_RED = 0,
  17. VIMC_DEBAYER_GREEN = 1,
  18. VIMC_DEBAYER_BLUE = 2,
  19. };
  20. struct vimc_debayer_pix_map {
  21. u32 code;
  22. enum vimc_debayer_rgb_colors order[2][2];
  23. };
  24. struct vimc_debayer_device {
  25. struct vimc_ent_device ved;
  26. struct v4l2_subdev sd;
  27. /* The active format */
  28. struct v4l2_mbus_framefmt sink_fmt;
  29. u32 src_code;
  30. void (*set_rgb_src)(struct vimc_debayer_device *vdebayer,
  31. unsigned int lin, unsigned int col,
  32. unsigned int rgb[3]);
  33. /* Values calculated when the stream starts */
  34. u8 *src_frame;
  35. const struct vimc_debayer_pix_map *sink_pix_map;
  36. unsigned int sink_bpp;
  37. unsigned int mean_win_size;
  38. struct v4l2_ctrl_handler hdl;
  39. struct media_pad pads[2];
  40. };
  41. static const struct v4l2_mbus_framefmt sink_fmt_default = {
  42. .width = 640,
  43. .height = 480,
  44. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  45. .field = V4L2_FIELD_NONE,
  46. .colorspace = V4L2_COLORSPACE_SRGB,
  47. };
  48. static const u32 vimc_debayer_src_mbus_codes[] = {
  49. MEDIA_BUS_FMT_GBR888_1X24,
  50. MEDIA_BUS_FMT_BGR888_1X24,
  51. MEDIA_BUS_FMT_BGR888_3X8,
  52. MEDIA_BUS_FMT_RGB888_1X24,
  53. MEDIA_BUS_FMT_RGB888_2X12_BE,
  54. MEDIA_BUS_FMT_RGB888_2X12_LE,
  55. MEDIA_BUS_FMT_RGB888_3X8,
  56. MEDIA_BUS_FMT_RGB888_1X7X4_SPWG,
  57. MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA,
  58. MEDIA_BUS_FMT_RGB888_1X32_PADHI,
  59. };
  60. static const struct vimc_debayer_pix_map vimc_debayer_pix_map_list[] = {
  61. {
  62. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  63. .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
  64. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
  65. },
  66. {
  67. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  68. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
  69. { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
  70. },
  71. {
  72. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  73. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
  74. { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
  75. },
  76. {
  77. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  78. .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
  79. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
  80. },
  81. {
  82. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  83. .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
  84. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
  85. },
  86. {
  87. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  88. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
  89. { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
  90. },
  91. {
  92. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  93. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
  94. { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
  95. },
  96. {
  97. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  98. .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
  99. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
  100. },
  101. {
  102. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  103. .order = { { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN },
  104. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED } }
  105. },
  106. {
  107. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  108. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE },
  109. { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN } }
  110. },
  111. {
  112. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  113. .order = { { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_RED },
  114. { VIMC_DEBAYER_BLUE, VIMC_DEBAYER_GREEN } }
  115. },
  116. {
  117. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  118. .order = { { VIMC_DEBAYER_RED, VIMC_DEBAYER_GREEN },
  119. { VIMC_DEBAYER_GREEN, VIMC_DEBAYER_BLUE } }
  120. },
  121. };
  122. static const struct vimc_debayer_pix_map *vimc_debayer_pix_map_by_code(u32 code)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < ARRAY_SIZE(vimc_debayer_pix_map_list); i++)
  126. if (vimc_debayer_pix_map_list[i].code == code)
  127. return &vimc_debayer_pix_map_list[i];
  128. return NULL;
  129. }
  130. static bool vimc_debayer_src_code_is_valid(u32 code)
  131. {
  132. unsigned int i;
  133. for (i = 0; i < ARRAY_SIZE(vimc_debayer_src_mbus_codes); i++)
  134. if (vimc_debayer_src_mbus_codes[i] == code)
  135. return true;
  136. return false;
  137. }
  138. static int vimc_debayer_init_cfg(struct v4l2_subdev *sd,
  139. struct v4l2_subdev_state *sd_state)
  140. {
  141. struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
  142. struct v4l2_mbus_framefmt *mf;
  143. unsigned int i;
  144. mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
  145. *mf = sink_fmt_default;
  146. for (i = 1; i < sd->entity.num_pads; i++) {
  147. mf = v4l2_subdev_get_try_format(sd, sd_state, i);
  148. *mf = sink_fmt_default;
  149. mf->code = vdebayer->src_code;
  150. }
  151. return 0;
  152. }
  153. static int vimc_debayer_enum_mbus_code(struct v4l2_subdev *sd,
  154. struct v4l2_subdev_state *sd_state,
  155. struct v4l2_subdev_mbus_code_enum *code)
  156. {
  157. if (VIMC_IS_SRC(code->pad)) {
  158. if (code->index >= ARRAY_SIZE(vimc_debayer_src_mbus_codes))
  159. return -EINVAL;
  160. code->code = vimc_debayer_src_mbus_codes[code->index];
  161. } else {
  162. if (code->index >= ARRAY_SIZE(vimc_debayer_pix_map_list))
  163. return -EINVAL;
  164. code->code = vimc_debayer_pix_map_list[code->index].code;
  165. }
  166. return 0;
  167. }
  168. static int vimc_debayer_enum_frame_size(struct v4l2_subdev *sd,
  169. struct v4l2_subdev_state *sd_state,
  170. struct v4l2_subdev_frame_size_enum *fse)
  171. {
  172. if (fse->index)
  173. return -EINVAL;
  174. if (VIMC_IS_SINK(fse->pad)) {
  175. const struct vimc_debayer_pix_map *vpix =
  176. vimc_debayer_pix_map_by_code(fse->code);
  177. if (!vpix)
  178. return -EINVAL;
  179. } else if (!vimc_debayer_src_code_is_valid(fse->code)) {
  180. return -EINVAL;
  181. }
  182. fse->min_width = VIMC_FRAME_MIN_WIDTH;
  183. fse->max_width = VIMC_FRAME_MAX_WIDTH;
  184. fse->min_height = VIMC_FRAME_MIN_HEIGHT;
  185. fse->max_height = VIMC_FRAME_MAX_HEIGHT;
  186. return 0;
  187. }
  188. static int vimc_debayer_get_fmt(struct v4l2_subdev *sd,
  189. struct v4l2_subdev_state *sd_state,
  190. struct v4l2_subdev_format *fmt)
  191. {
  192. struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
  193. /* Get the current sink format */
  194. fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
  195. *v4l2_subdev_get_try_format(sd, sd_state, 0) :
  196. vdebayer->sink_fmt;
  197. /* Set the right code for the source pad */
  198. if (VIMC_IS_SRC(fmt->pad))
  199. fmt->format.code = vdebayer->src_code;
  200. return 0;
  201. }
  202. static void vimc_debayer_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
  203. {
  204. const struct vimc_debayer_pix_map *vpix;
  205. /* Don't accept a code that is not on the debayer table */
  206. vpix = vimc_debayer_pix_map_by_code(fmt->code);
  207. if (!vpix)
  208. fmt->code = sink_fmt_default.code;
  209. fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
  210. VIMC_FRAME_MAX_WIDTH) & ~1;
  211. fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
  212. VIMC_FRAME_MAX_HEIGHT) & ~1;
  213. if (fmt->field == V4L2_FIELD_ANY)
  214. fmt->field = sink_fmt_default.field;
  215. vimc_colorimetry_clamp(fmt);
  216. }
  217. static int vimc_debayer_set_fmt(struct v4l2_subdev *sd,
  218. struct v4l2_subdev_state *sd_state,
  219. struct v4l2_subdev_format *fmt)
  220. {
  221. struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
  222. struct v4l2_mbus_framefmt *sink_fmt;
  223. u32 *src_code;
  224. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  225. /* Do not change the format while stream is on */
  226. if (vdebayer->src_frame)
  227. return -EBUSY;
  228. sink_fmt = &vdebayer->sink_fmt;
  229. src_code = &vdebayer->src_code;
  230. } else {
  231. sink_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
  232. src_code = &v4l2_subdev_get_try_format(sd, sd_state, 1)->code;
  233. }
  234. /*
  235. * Do not change the format of the source pad,
  236. * it is propagated from the sink
  237. */
  238. if (VIMC_IS_SRC(fmt->pad)) {
  239. u32 code = fmt->format.code;
  240. fmt->format = *sink_fmt;
  241. if (vimc_debayer_src_code_is_valid(code))
  242. *src_code = code;
  243. fmt->format.code = *src_code;
  244. } else {
  245. /* Set the new format in the sink pad */
  246. vimc_debayer_adjust_sink_fmt(&fmt->format);
  247. dev_dbg(vdebayer->ved.dev, "%s: sink format update: "
  248. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  249. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vdebayer->sd.name,
  250. /* old */
  251. sink_fmt->width, sink_fmt->height, sink_fmt->code,
  252. sink_fmt->colorspace, sink_fmt->quantization,
  253. sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
  254. /* new */
  255. fmt->format.width, fmt->format.height, fmt->format.code,
  256. fmt->format.colorspace, fmt->format.quantization,
  257. fmt->format.xfer_func, fmt->format.ycbcr_enc);
  258. *sink_fmt = fmt->format;
  259. }
  260. return 0;
  261. }
  262. static const struct v4l2_subdev_pad_ops vimc_debayer_pad_ops = {
  263. .init_cfg = vimc_debayer_init_cfg,
  264. .enum_mbus_code = vimc_debayer_enum_mbus_code,
  265. .enum_frame_size = vimc_debayer_enum_frame_size,
  266. .get_fmt = vimc_debayer_get_fmt,
  267. .set_fmt = vimc_debayer_set_fmt,
  268. };
  269. static void vimc_debayer_process_rgb_frame(struct vimc_debayer_device *vdebayer,
  270. unsigned int lin,
  271. unsigned int col,
  272. unsigned int rgb[3])
  273. {
  274. const struct vimc_pix_map *vpix;
  275. unsigned int i, index;
  276. vpix = vimc_pix_map_by_code(vdebayer->src_code);
  277. index = VIMC_FRAME_INDEX(lin, col, vdebayer->sink_fmt.width, 3);
  278. for (i = 0; i < 3; i++) {
  279. switch (vpix->pixelformat) {
  280. case V4L2_PIX_FMT_RGB24:
  281. vdebayer->src_frame[index + i] = rgb[i];
  282. break;
  283. case V4L2_PIX_FMT_BGR24:
  284. vdebayer->src_frame[index + i] = rgb[2 - i];
  285. break;
  286. }
  287. }
  288. }
  289. static int vimc_debayer_s_stream(struct v4l2_subdev *sd, int enable)
  290. {
  291. struct vimc_debayer_device *vdebayer = v4l2_get_subdevdata(sd);
  292. if (enable) {
  293. const struct vimc_pix_map *vpix;
  294. unsigned int frame_size;
  295. if (vdebayer->src_frame)
  296. return 0;
  297. /* Calculate the frame size of the source pad */
  298. vpix = vimc_pix_map_by_code(vdebayer->src_code);
  299. frame_size = vdebayer->sink_fmt.width * vdebayer->sink_fmt.height *
  300. vpix->bpp;
  301. /* Save the bytes per pixel of the sink */
  302. vpix = vimc_pix_map_by_code(vdebayer->sink_fmt.code);
  303. vdebayer->sink_bpp = vpix->bpp;
  304. /* Get the corresponding pixel map from the table */
  305. vdebayer->sink_pix_map =
  306. vimc_debayer_pix_map_by_code(vdebayer->sink_fmt.code);
  307. /*
  308. * Allocate the frame buffer. Use vmalloc to be able to
  309. * allocate a large amount of memory
  310. */
  311. vdebayer->src_frame = vmalloc(frame_size);
  312. if (!vdebayer->src_frame)
  313. return -ENOMEM;
  314. } else {
  315. if (!vdebayer->src_frame)
  316. return 0;
  317. vfree(vdebayer->src_frame);
  318. vdebayer->src_frame = NULL;
  319. }
  320. return 0;
  321. }
  322. static const struct v4l2_subdev_core_ops vimc_debayer_core_ops = {
  323. .log_status = v4l2_ctrl_subdev_log_status,
  324. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  325. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  326. };
  327. static const struct v4l2_subdev_video_ops vimc_debayer_video_ops = {
  328. .s_stream = vimc_debayer_s_stream,
  329. };
  330. static const struct v4l2_subdev_ops vimc_debayer_ops = {
  331. .core = &vimc_debayer_core_ops,
  332. .pad = &vimc_debayer_pad_ops,
  333. .video = &vimc_debayer_video_ops,
  334. };
  335. static unsigned int vimc_debayer_get_val(const u8 *bytes,
  336. const unsigned int n_bytes)
  337. {
  338. unsigned int i;
  339. unsigned int acc = 0;
  340. for (i = 0; i < n_bytes; i++)
  341. acc = acc + (bytes[i] << (8 * i));
  342. return acc;
  343. }
  344. static void vimc_debayer_calc_rgb_sink(struct vimc_debayer_device *vdebayer,
  345. const u8 *frame,
  346. const unsigned int lin,
  347. const unsigned int col,
  348. unsigned int rgb[3])
  349. {
  350. unsigned int i, seek, wlin, wcol;
  351. unsigned int n_rgb[3] = {0, 0, 0};
  352. for (i = 0; i < 3; i++)
  353. rgb[i] = 0;
  354. /*
  355. * Calculate how many we need to subtract to get to the pixel in
  356. * the top left corner of the mean window (considering the current
  357. * pixel as the center)
  358. */
  359. seek = vdebayer->mean_win_size / 2;
  360. /* Sum the values of the colors in the mean window */
  361. dev_dbg(vdebayer->ved.dev,
  362. "deb: %s: --- Calc pixel %dx%d, window mean %d, seek %d ---\n",
  363. vdebayer->sd.name, lin, col, vdebayer->sink_fmt.height, seek);
  364. /*
  365. * Iterate through all the lines in the mean window, start
  366. * with zero if the pixel is outside the frame and don't pass
  367. * the height when the pixel is in the bottom border of the
  368. * frame
  369. */
  370. for (wlin = seek > lin ? 0 : lin - seek;
  371. wlin < lin + seek + 1 && wlin < vdebayer->sink_fmt.height;
  372. wlin++) {
  373. /*
  374. * Iterate through all the columns in the mean window, start
  375. * with zero if the pixel is outside the frame and don't pass
  376. * the width when the pixel is in the right border of the
  377. * frame
  378. */
  379. for (wcol = seek > col ? 0 : col - seek;
  380. wcol < col + seek + 1 && wcol < vdebayer->sink_fmt.width;
  381. wcol++) {
  382. enum vimc_debayer_rgb_colors color;
  383. unsigned int index;
  384. /* Check which color this pixel is */
  385. color = vdebayer->sink_pix_map->order[wlin % 2][wcol % 2];
  386. index = VIMC_FRAME_INDEX(wlin, wcol,
  387. vdebayer->sink_fmt.width,
  388. vdebayer->sink_bpp);
  389. dev_dbg(vdebayer->ved.dev,
  390. "deb: %s: RGB CALC: frame index %d, win pos %dx%d, color %d\n",
  391. vdebayer->sd.name, index, wlin, wcol, color);
  392. /* Get its value */
  393. rgb[color] = rgb[color] +
  394. vimc_debayer_get_val(&frame[index],
  395. vdebayer->sink_bpp);
  396. /* Save how many values we already added */
  397. n_rgb[color]++;
  398. dev_dbg(vdebayer->ved.dev, "deb: %s: RGB CALC: val %d, n %d\n",
  399. vdebayer->sd.name, rgb[color], n_rgb[color]);
  400. }
  401. }
  402. /* Calculate the mean */
  403. for (i = 0; i < 3; i++) {
  404. dev_dbg(vdebayer->ved.dev,
  405. "deb: %s: PRE CALC: %dx%d Color %d, val %d, n %d\n",
  406. vdebayer->sd.name, lin, col, i, rgb[i], n_rgb[i]);
  407. if (n_rgb[i])
  408. rgb[i] = rgb[i] / n_rgb[i];
  409. dev_dbg(vdebayer->ved.dev,
  410. "deb: %s: FINAL CALC: %dx%d Color %d, val %d\n",
  411. vdebayer->sd.name, lin, col, i, rgb[i]);
  412. }
  413. }
  414. static void *vimc_debayer_process_frame(struct vimc_ent_device *ved,
  415. const void *sink_frame)
  416. {
  417. struct vimc_debayer_device *vdebayer =
  418. container_of(ved, struct vimc_debayer_device, ved);
  419. unsigned int rgb[3];
  420. unsigned int i, j;
  421. /* If the stream in this node is not active, just return */
  422. if (!vdebayer->src_frame)
  423. return ERR_PTR(-EINVAL);
  424. for (i = 0; i < vdebayer->sink_fmt.height; i++)
  425. for (j = 0; j < vdebayer->sink_fmt.width; j++) {
  426. vimc_debayer_calc_rgb_sink(vdebayer, sink_frame, i, j, rgb);
  427. vdebayer->set_rgb_src(vdebayer, i, j, rgb);
  428. }
  429. return vdebayer->src_frame;
  430. }
  431. static int vimc_debayer_s_ctrl(struct v4l2_ctrl *ctrl)
  432. {
  433. struct vimc_debayer_device *vdebayer =
  434. container_of(ctrl->handler, struct vimc_debayer_device, hdl);
  435. switch (ctrl->id) {
  436. case VIMC_CID_MEAN_WIN_SIZE:
  437. vdebayer->mean_win_size = ctrl->val;
  438. break;
  439. default:
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. static const struct v4l2_ctrl_ops vimc_debayer_ctrl_ops = {
  445. .s_ctrl = vimc_debayer_s_ctrl,
  446. };
  447. static void vimc_debayer_release(struct vimc_ent_device *ved)
  448. {
  449. struct vimc_debayer_device *vdebayer =
  450. container_of(ved, struct vimc_debayer_device, ved);
  451. v4l2_ctrl_handler_free(&vdebayer->hdl);
  452. media_entity_cleanup(vdebayer->ved.ent);
  453. kfree(vdebayer);
  454. }
  455. static const struct v4l2_ctrl_config vimc_debayer_ctrl_class = {
  456. .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
  457. .id = VIMC_CID_VIMC_CLASS,
  458. .name = "VIMC Controls",
  459. .type = V4L2_CTRL_TYPE_CTRL_CLASS,
  460. };
  461. static const struct v4l2_ctrl_config vimc_debayer_ctrl_mean_win_size = {
  462. .ops = &vimc_debayer_ctrl_ops,
  463. .id = VIMC_CID_MEAN_WIN_SIZE,
  464. .name = "Debayer Mean Window Size",
  465. .type = V4L2_CTRL_TYPE_INTEGER,
  466. .min = 1,
  467. .max = 25,
  468. .step = 2,
  469. .def = 3,
  470. };
  471. static struct vimc_ent_device *vimc_debayer_add(struct vimc_device *vimc,
  472. const char *vcfg_name)
  473. {
  474. struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
  475. struct vimc_debayer_device *vdebayer;
  476. int ret;
  477. /* Allocate the vdebayer struct */
  478. vdebayer = kzalloc(sizeof(*vdebayer), GFP_KERNEL);
  479. if (!vdebayer)
  480. return ERR_PTR(-ENOMEM);
  481. /* Create controls: */
  482. v4l2_ctrl_handler_init(&vdebayer->hdl, 2);
  483. v4l2_ctrl_new_custom(&vdebayer->hdl, &vimc_debayer_ctrl_class, NULL);
  484. v4l2_ctrl_new_custom(&vdebayer->hdl, &vimc_debayer_ctrl_mean_win_size, NULL);
  485. vdebayer->sd.ctrl_handler = &vdebayer->hdl;
  486. if (vdebayer->hdl.error) {
  487. ret = vdebayer->hdl.error;
  488. goto err_free_vdebayer;
  489. }
  490. /* Initialize ved and sd */
  491. vdebayer->pads[0].flags = MEDIA_PAD_FL_SINK;
  492. vdebayer->pads[1].flags = MEDIA_PAD_FL_SOURCE;
  493. ret = vimc_ent_sd_register(&vdebayer->ved, &vdebayer->sd, v4l2_dev,
  494. vcfg_name,
  495. MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV, 2,
  496. vdebayer->pads, &vimc_debayer_ops);
  497. if (ret)
  498. goto err_free_hdl;
  499. vdebayer->ved.process_frame = vimc_debayer_process_frame;
  500. vdebayer->ved.dev = vimc->mdev.dev;
  501. vdebayer->mean_win_size = vimc_debayer_ctrl_mean_win_size.def;
  502. /* Initialize the frame format */
  503. vdebayer->sink_fmt = sink_fmt_default;
  504. /*
  505. * TODO: Add support for more output formats, we only support
  506. * RGB888 for now
  507. * NOTE: the src format is always the same as the sink, except
  508. * for the code
  509. */
  510. vdebayer->src_code = MEDIA_BUS_FMT_RGB888_1X24;
  511. vdebayer->set_rgb_src = vimc_debayer_process_rgb_frame;
  512. return &vdebayer->ved;
  513. err_free_hdl:
  514. v4l2_ctrl_handler_free(&vdebayer->hdl);
  515. err_free_vdebayer:
  516. kfree(vdebayer);
  517. return ERR_PTR(ret);
  518. }
  519. struct vimc_ent_type vimc_debayer_type = {
  520. .add = vimc_debayer_add,
  521. .release = vimc_debayer_release
  522. };