vimc-debayer.c 16 KB

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