vimc-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * vimc-common.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/init.h>
  18. #include <linux/module.h>
  19. #include "vimc-common.h"
  20. /*
  21. * NOTE: non-bayer formats need to come first (necessary for enum_mbus_code
  22. * in the scaler)
  23. */
  24. static const struct vimc_pix_map vimc_pix_map_list[] = {
  25. /* TODO: add all missing formats */
  26. /* RGB formats */
  27. {
  28. .code = MEDIA_BUS_FMT_BGR888_1X24,
  29. .pixelformat = V4L2_PIX_FMT_BGR24,
  30. .bpp = 3,
  31. .bayer = false,
  32. },
  33. {
  34. .code = MEDIA_BUS_FMT_RGB888_1X24,
  35. .pixelformat = V4L2_PIX_FMT_RGB24,
  36. .bpp = 3,
  37. .bayer = false,
  38. },
  39. {
  40. .code = MEDIA_BUS_FMT_ARGB8888_1X32,
  41. .pixelformat = V4L2_PIX_FMT_ARGB32,
  42. .bpp = 4,
  43. .bayer = false,
  44. },
  45. /* Bayer formats */
  46. {
  47. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  48. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  49. .bpp = 1,
  50. .bayer = true,
  51. },
  52. {
  53. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  54. .pixelformat = V4L2_PIX_FMT_SGBRG8,
  55. .bpp = 1,
  56. .bayer = true,
  57. },
  58. {
  59. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  60. .pixelformat = V4L2_PIX_FMT_SGRBG8,
  61. .bpp = 1,
  62. .bayer = true,
  63. },
  64. {
  65. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  66. .pixelformat = V4L2_PIX_FMT_SRGGB8,
  67. .bpp = 1,
  68. .bayer = true,
  69. },
  70. {
  71. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  72. .pixelformat = V4L2_PIX_FMT_SBGGR10,
  73. .bpp = 2,
  74. .bayer = true,
  75. },
  76. {
  77. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  78. .pixelformat = V4L2_PIX_FMT_SGBRG10,
  79. .bpp = 2,
  80. .bayer = true,
  81. },
  82. {
  83. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  84. .pixelformat = V4L2_PIX_FMT_SGRBG10,
  85. .bpp = 2,
  86. .bayer = true,
  87. },
  88. {
  89. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  90. .pixelformat = V4L2_PIX_FMT_SRGGB10,
  91. .bpp = 2,
  92. .bayer = true,
  93. },
  94. /* 10bit raw bayer a-law compressed to 8 bits */
  95. {
  96. .code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
  97. .pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8,
  98. .bpp = 1,
  99. .bayer = true,
  100. },
  101. {
  102. .code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
  103. .pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8,
  104. .bpp = 1,
  105. .bayer = true,
  106. },
  107. {
  108. .code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
  109. .pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8,
  110. .bpp = 1,
  111. .bayer = true,
  112. },
  113. {
  114. .code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
  115. .pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8,
  116. .bpp = 1,
  117. .bayer = true,
  118. },
  119. /* 10bit raw bayer DPCM compressed to 8 bits */
  120. {
  121. .code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
  122. .pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8,
  123. .bpp = 1,
  124. .bayer = true,
  125. },
  126. {
  127. .code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
  128. .pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8,
  129. .bpp = 1,
  130. .bayer = true,
  131. },
  132. {
  133. .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
  134. .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
  135. .bpp = 1,
  136. .bayer = true,
  137. },
  138. {
  139. .code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
  140. .pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8,
  141. .bpp = 1,
  142. .bayer = true,
  143. },
  144. {
  145. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  146. .pixelformat = V4L2_PIX_FMT_SBGGR12,
  147. .bpp = 2,
  148. .bayer = true,
  149. },
  150. {
  151. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  152. .pixelformat = V4L2_PIX_FMT_SGBRG12,
  153. .bpp = 2,
  154. .bayer = true,
  155. },
  156. {
  157. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  158. .pixelformat = V4L2_PIX_FMT_SGRBG12,
  159. .bpp = 2,
  160. .bayer = true,
  161. },
  162. {
  163. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  164. .pixelformat = V4L2_PIX_FMT_SRGGB12,
  165. .bpp = 2,
  166. .bayer = true,
  167. },
  168. };
  169. const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i)
  170. {
  171. if (i >= ARRAY_SIZE(vimc_pix_map_list))
  172. return NULL;
  173. return &vimc_pix_map_list[i];
  174. }
  175. EXPORT_SYMBOL_GPL(vimc_pix_map_by_index);
  176. const struct vimc_pix_map *vimc_pix_map_by_code(u32 code)
  177. {
  178. unsigned int i;
  179. for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
  180. if (vimc_pix_map_list[i].code == code)
  181. return &vimc_pix_map_list[i];
  182. }
  183. return NULL;
  184. }
  185. EXPORT_SYMBOL_GPL(vimc_pix_map_by_code);
  186. const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat)
  187. {
  188. unsigned int i;
  189. for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
  190. if (vimc_pix_map_list[i].pixelformat == pixelformat)
  191. return &vimc_pix_map_list[i];
  192. }
  193. return NULL;
  194. }
  195. EXPORT_SYMBOL_GPL(vimc_pix_map_by_pixelformat);
  196. int vimc_propagate_frame(struct media_pad *src, const void *frame)
  197. {
  198. struct media_link *link;
  199. if (!(src->flags & MEDIA_PAD_FL_SOURCE))
  200. return -EINVAL;
  201. /* Send this frame to all sink pads that are direct linked */
  202. list_for_each_entry(link, &src->entity->links, list) {
  203. if (link->source == src &&
  204. (link->flags & MEDIA_LNK_FL_ENABLED)) {
  205. struct vimc_ent_device *ved = NULL;
  206. struct media_entity *entity = link->sink->entity;
  207. if (is_media_entity_v4l2_subdev(entity)) {
  208. struct v4l2_subdev *sd =
  209. container_of(entity, struct v4l2_subdev,
  210. entity);
  211. ved = v4l2_get_subdevdata(sd);
  212. } else if (is_media_entity_v4l2_video_device(entity)) {
  213. struct video_device *vdev =
  214. container_of(entity,
  215. struct video_device,
  216. entity);
  217. ved = video_get_drvdata(vdev);
  218. }
  219. if (ved && ved->process_frame)
  220. ved->process_frame(ved, link->sink, frame);
  221. }
  222. }
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(vimc_propagate_frame);
  226. /* Helper function to allocate and initialize pads */
  227. struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
  228. {
  229. struct media_pad *pads;
  230. unsigned int i;
  231. /* Allocate memory for the pads */
  232. pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
  233. if (!pads)
  234. return ERR_PTR(-ENOMEM);
  235. /* Initialize the pads */
  236. for (i = 0; i < num_pads; i++) {
  237. pads[i].index = i;
  238. pads[i].flags = pads_flag[i];
  239. }
  240. return pads;
  241. }
  242. EXPORT_SYMBOL_GPL(vimc_pads_init);
  243. int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
  244. {
  245. struct v4l2_subdev *sd;
  246. struct media_pad *pad;
  247. unsigned int i;
  248. int ret;
  249. for (i = 0; i < ent->num_pads; i++) {
  250. if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  251. continue;
  252. /* Start the stream in the subdevice direct connected */
  253. pad = media_entity_remote_pad(&ent->pads[i]);
  254. if (!is_media_entity_v4l2_subdev(pad->entity))
  255. return -EINVAL;
  256. sd = media_entity_to_v4l2_subdev(pad->entity);
  257. ret = v4l2_subdev_call(sd, video, s_stream, enable);
  258. if (ret && ret != -ENOIOCTLCMD)
  259. return ret;
  260. }
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(vimc_pipeline_s_stream);
  264. static int vimc_get_mbus_format(struct media_pad *pad,
  265. struct v4l2_subdev_format *fmt)
  266. {
  267. if (is_media_entity_v4l2_subdev(pad->entity)) {
  268. struct v4l2_subdev *sd =
  269. media_entity_to_v4l2_subdev(pad->entity);
  270. int ret;
  271. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  272. fmt->pad = pad->index;
  273. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  274. if (ret)
  275. return ret;
  276. } else if (is_media_entity_v4l2_video_device(pad->entity)) {
  277. struct video_device *vdev = container_of(pad->entity,
  278. struct video_device,
  279. entity);
  280. struct vimc_ent_device *ved = video_get_drvdata(vdev);
  281. const struct vimc_pix_map *vpix;
  282. struct v4l2_pix_format vdev_fmt;
  283. if (!ved->vdev_get_format)
  284. return -ENOIOCTLCMD;
  285. ved->vdev_get_format(ved, &vdev_fmt);
  286. vpix = vimc_pix_map_by_pixelformat(vdev_fmt.pixelformat);
  287. v4l2_fill_mbus_format(&fmt->format, &vdev_fmt, vpix->code);
  288. } else {
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. int vimc_link_validate(struct media_link *link)
  294. {
  295. struct v4l2_subdev_format source_fmt, sink_fmt;
  296. int ret;
  297. ret = vimc_get_mbus_format(link->source, &source_fmt);
  298. if (ret)
  299. return ret;
  300. ret = vimc_get_mbus_format(link->sink, &sink_fmt);
  301. if (ret)
  302. return ret;
  303. pr_info("vimc link validate: "
  304. "%s:src:%dx%d (0x%x, %d, %d, %d, %d) "
  305. "%s:snk:%dx%d (0x%x, %d, %d, %d, %d)\n",
  306. /* src */
  307. link->source->entity->name,
  308. source_fmt.format.width, source_fmt.format.height,
  309. source_fmt.format.code, source_fmt.format.colorspace,
  310. source_fmt.format.quantization, source_fmt.format.xfer_func,
  311. source_fmt.format.ycbcr_enc,
  312. /* sink */
  313. link->sink->entity->name,
  314. sink_fmt.format.width, sink_fmt.format.height,
  315. sink_fmt.format.code, sink_fmt.format.colorspace,
  316. sink_fmt.format.quantization, sink_fmt.format.xfer_func,
  317. sink_fmt.format.ycbcr_enc);
  318. /* The width, height and code must match. */
  319. if (source_fmt.format.width != sink_fmt.format.width
  320. || source_fmt.format.height != sink_fmt.format.height
  321. || source_fmt.format.code != sink_fmt.format.code)
  322. return -EPIPE;
  323. /*
  324. * The field order must match, or the sink field order must be NONE
  325. * to support interlaced hardware connected to bridges that support
  326. * progressive formats only.
  327. */
  328. if (source_fmt.format.field != sink_fmt.format.field &&
  329. sink_fmt.format.field != V4L2_FIELD_NONE)
  330. return -EPIPE;
  331. /*
  332. * If colorspace is DEFAULT, then assume all the colorimetry is also
  333. * DEFAULT, return 0 to skip comparing the other colorimetry parameters
  334. */
  335. if (source_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT
  336. || sink_fmt.format.colorspace == V4L2_COLORSPACE_DEFAULT)
  337. return 0;
  338. /* Colorspace must match. */
  339. if (source_fmt.format.colorspace != sink_fmt.format.colorspace)
  340. return -EPIPE;
  341. /* Colorimetry must match if they are not set to DEFAULT */
  342. if (source_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
  343. && sink_fmt.format.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT
  344. && source_fmt.format.ycbcr_enc != sink_fmt.format.ycbcr_enc)
  345. return -EPIPE;
  346. if (source_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
  347. && sink_fmt.format.quantization != V4L2_QUANTIZATION_DEFAULT
  348. && source_fmt.format.quantization != sink_fmt.format.quantization)
  349. return -EPIPE;
  350. if (source_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
  351. && sink_fmt.format.xfer_func != V4L2_XFER_FUNC_DEFAULT
  352. && source_fmt.format.xfer_func != sink_fmt.format.xfer_func)
  353. return -EPIPE;
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(vimc_link_validate);
  357. static const struct media_entity_operations vimc_ent_sd_mops = {
  358. .link_validate = vimc_link_validate,
  359. };
  360. int vimc_ent_sd_register(struct vimc_ent_device *ved,
  361. struct v4l2_subdev *sd,
  362. struct v4l2_device *v4l2_dev,
  363. const char *const name,
  364. u32 function,
  365. u16 num_pads,
  366. const unsigned long *pads_flag,
  367. const struct v4l2_subdev_ops *sd_ops)
  368. {
  369. int ret;
  370. /* Allocate the pads */
  371. ved->pads = vimc_pads_init(num_pads, pads_flag);
  372. if (IS_ERR(ved->pads))
  373. return PTR_ERR(ved->pads);
  374. /* Fill the vimc_ent_device struct */
  375. ved->ent = &sd->entity;
  376. /* Initialize the subdev */
  377. v4l2_subdev_init(sd, sd_ops);
  378. sd->entity.function = function;
  379. sd->entity.ops = &vimc_ent_sd_mops;
  380. sd->owner = THIS_MODULE;
  381. strlcpy(sd->name, name, sizeof(sd->name));
  382. v4l2_set_subdevdata(sd, ved);
  383. /* Expose this subdev to user space */
  384. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  385. if (sd->ctrl_handler)
  386. sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
  387. /* Initialize the media entity */
  388. ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
  389. if (ret)
  390. goto err_clean_pads;
  391. /* Register the subdev with the v4l2 and the media framework */
  392. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  393. if (ret) {
  394. dev_err(v4l2_dev->dev,
  395. "%s: subdev register failed (err=%d)\n",
  396. name, ret);
  397. goto err_clean_m_ent;
  398. }
  399. return 0;
  400. err_clean_m_ent:
  401. media_entity_cleanup(&sd->entity);
  402. err_clean_pads:
  403. vimc_pads_cleanup(ved->pads);
  404. return ret;
  405. }
  406. EXPORT_SYMBOL_GPL(vimc_ent_sd_register);
  407. void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
  408. {
  409. v4l2_device_unregister_subdev(sd);
  410. media_entity_cleanup(ved->ent);
  411. vimc_pads_cleanup(ved->pads);
  412. }
  413. EXPORT_SYMBOL_GPL(vimc_ent_sd_unregister);
  414. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Common");
  415. MODULE_AUTHOR("Helen Koike <helen.fornazier@gmail.com>");
  416. MODULE_LICENSE("GPL");