vimc-common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "vimc-common.h"
  18. static const struct vimc_pix_map vimc_pix_map_list[] = {
  19. /* TODO: add all missing formats */
  20. /* RGB formats */
  21. {
  22. .code = MEDIA_BUS_FMT_BGR888_1X24,
  23. .pixelformat = V4L2_PIX_FMT_BGR24,
  24. .bpp = 3,
  25. },
  26. {
  27. .code = MEDIA_BUS_FMT_RGB888_1X24,
  28. .pixelformat = V4L2_PIX_FMT_RGB24,
  29. .bpp = 3,
  30. },
  31. {
  32. .code = MEDIA_BUS_FMT_ARGB8888_1X32,
  33. .pixelformat = V4L2_PIX_FMT_ARGB32,
  34. .bpp = 4,
  35. },
  36. /* Bayer formats */
  37. {
  38. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  39. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  40. .bpp = 1,
  41. },
  42. {
  43. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  44. .pixelformat = V4L2_PIX_FMT_SGBRG8,
  45. .bpp = 1,
  46. },
  47. {
  48. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  49. .pixelformat = V4L2_PIX_FMT_SGRBG8,
  50. .bpp = 1,
  51. },
  52. {
  53. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  54. .pixelformat = V4L2_PIX_FMT_SRGGB8,
  55. .bpp = 1,
  56. },
  57. {
  58. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  59. .pixelformat = V4L2_PIX_FMT_SBGGR10,
  60. .bpp = 2,
  61. },
  62. {
  63. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  64. .pixelformat = V4L2_PIX_FMT_SGBRG10,
  65. .bpp = 2,
  66. },
  67. {
  68. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  69. .pixelformat = V4L2_PIX_FMT_SGRBG10,
  70. .bpp = 2,
  71. },
  72. {
  73. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  74. .pixelformat = V4L2_PIX_FMT_SRGGB10,
  75. .bpp = 2,
  76. },
  77. /* 10bit raw bayer a-law compressed to 8 bits */
  78. {
  79. .code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
  80. .pixelformat = V4L2_PIX_FMT_SBGGR10ALAW8,
  81. .bpp = 1,
  82. },
  83. {
  84. .code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
  85. .pixelformat = V4L2_PIX_FMT_SGBRG10ALAW8,
  86. .bpp = 1,
  87. },
  88. {
  89. .code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
  90. .pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8,
  91. .bpp = 1,
  92. },
  93. {
  94. .code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
  95. .pixelformat = V4L2_PIX_FMT_SRGGB10ALAW8,
  96. .bpp = 1,
  97. },
  98. /* 10bit raw bayer DPCM compressed to 8 bits */
  99. {
  100. .code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
  101. .pixelformat = V4L2_PIX_FMT_SBGGR10DPCM8,
  102. .bpp = 1,
  103. },
  104. {
  105. .code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
  106. .pixelformat = V4L2_PIX_FMT_SGBRG10DPCM8,
  107. .bpp = 1,
  108. },
  109. {
  110. .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
  111. .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
  112. .bpp = 1,
  113. },
  114. {
  115. .code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
  116. .pixelformat = V4L2_PIX_FMT_SRGGB10DPCM8,
  117. .bpp = 1,
  118. },
  119. {
  120. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  121. .pixelformat = V4L2_PIX_FMT_SBGGR12,
  122. .bpp = 2,
  123. },
  124. {
  125. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  126. .pixelformat = V4L2_PIX_FMT_SGBRG12,
  127. .bpp = 2,
  128. },
  129. {
  130. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  131. .pixelformat = V4L2_PIX_FMT_SGRBG12,
  132. .bpp = 2,
  133. },
  134. {
  135. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  136. .pixelformat = V4L2_PIX_FMT_SRGGB12,
  137. .bpp = 2,
  138. },
  139. };
  140. const struct vimc_pix_map *vimc_pix_map_by_code(u32 code)
  141. {
  142. unsigned int i;
  143. for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
  144. if (vimc_pix_map_list[i].code == code)
  145. return &vimc_pix_map_list[i];
  146. }
  147. return NULL;
  148. }
  149. const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat)
  150. {
  151. unsigned int i;
  152. for (i = 0; i < ARRAY_SIZE(vimc_pix_map_list); i++) {
  153. if (vimc_pix_map_list[i].pixelformat == pixelformat)
  154. return &vimc_pix_map_list[i];
  155. }
  156. return NULL;
  157. }
  158. int vimc_propagate_frame(struct media_pad *src, const void *frame)
  159. {
  160. struct media_link *link;
  161. if (!(src->flags & MEDIA_PAD_FL_SOURCE))
  162. return -EINVAL;
  163. /* Send this frame to all sink pads that are direct linked */
  164. list_for_each_entry(link, &src->entity->links, list) {
  165. if (link->source == src &&
  166. (link->flags & MEDIA_LNK_FL_ENABLED)) {
  167. struct vimc_ent_device *ved = NULL;
  168. struct media_entity *entity = link->sink->entity;
  169. if (is_media_entity_v4l2_subdev(entity)) {
  170. struct v4l2_subdev *sd =
  171. container_of(entity, struct v4l2_subdev,
  172. entity);
  173. ved = v4l2_get_subdevdata(sd);
  174. } else if (is_media_entity_v4l2_video_device(entity)) {
  175. struct video_device *vdev =
  176. container_of(entity,
  177. struct video_device,
  178. entity);
  179. ved = video_get_drvdata(vdev);
  180. }
  181. if (ved && ved->process_frame)
  182. ved->process_frame(ved, link->sink, frame);
  183. }
  184. }
  185. return 0;
  186. }
  187. /* Helper function to allocate and initialize pads */
  188. struct media_pad *vimc_pads_init(u16 num_pads, const unsigned long *pads_flag)
  189. {
  190. struct media_pad *pads;
  191. unsigned int i;
  192. /* Allocate memory for the pads */
  193. pads = kcalloc(num_pads, sizeof(*pads), GFP_KERNEL);
  194. if (!pads)
  195. return ERR_PTR(-ENOMEM);
  196. /* Initialize the pads */
  197. for (i = 0; i < num_pads; i++) {
  198. pads[i].index = i;
  199. pads[i].flags = pads_flag[i];
  200. }
  201. return pads;
  202. }
  203. int vimc_pipeline_s_stream(struct media_entity *ent, int enable)
  204. {
  205. struct v4l2_subdev *sd;
  206. struct media_pad *pad;
  207. unsigned int i;
  208. int ret;
  209. for (i = 0; i < ent->num_pads; i++) {
  210. if (ent->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  211. continue;
  212. /* Start the stream in the subdevice direct connected */
  213. pad = media_entity_remote_pad(&ent->pads[i]);
  214. /*
  215. * if this is a raw node from vimc-core, then there is
  216. * nothing to activate
  217. * TODO: remove this when there are no more raw nodes in the
  218. * core and return error instead
  219. */
  220. if (pad->entity->obj_type == MEDIA_ENTITY_TYPE_BASE)
  221. continue;
  222. sd = media_entity_to_v4l2_subdev(pad->entity);
  223. ret = v4l2_subdev_call(sd, video, s_stream, enable);
  224. if (ret && ret != -ENOIOCTLCMD)
  225. return ret;
  226. }
  227. return 0;
  228. }
  229. static const struct media_entity_operations vimc_ent_sd_mops = {
  230. .link_validate = v4l2_subdev_link_validate,
  231. };
  232. int vimc_ent_sd_register(struct vimc_ent_device *ved,
  233. struct v4l2_subdev *sd,
  234. struct v4l2_device *v4l2_dev,
  235. const char *const name,
  236. u32 function,
  237. u16 num_pads,
  238. const unsigned long *pads_flag,
  239. const struct v4l2_subdev_ops *sd_ops,
  240. void (*sd_destroy)(struct vimc_ent_device *))
  241. {
  242. int ret;
  243. /* Allocate the pads */
  244. ved->pads = vimc_pads_init(num_pads, pads_flag);
  245. if (IS_ERR(ved->pads))
  246. return PTR_ERR(ved->pads);
  247. /* Fill the vimc_ent_device struct */
  248. ved->destroy = sd_destroy;
  249. ved->ent = &sd->entity;
  250. /* Initialize the subdev */
  251. v4l2_subdev_init(sd, sd_ops);
  252. sd->entity.function = function;
  253. sd->entity.ops = &vimc_ent_sd_mops;
  254. sd->owner = THIS_MODULE;
  255. strlcpy(sd->name, name, sizeof(sd->name));
  256. v4l2_set_subdevdata(sd, ved);
  257. /* Expose this subdev to user space */
  258. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  259. /* Initialize the media entity */
  260. ret = media_entity_pads_init(&sd->entity, num_pads, ved->pads);
  261. if (ret)
  262. goto err_clean_pads;
  263. /* Register the subdev with the v4l2 and the media framework */
  264. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  265. if (ret) {
  266. dev_err(v4l2_dev->dev,
  267. "%s: subdev register failed (err=%d)\n",
  268. name, ret);
  269. goto err_clean_m_ent;
  270. }
  271. return 0;
  272. err_clean_m_ent:
  273. media_entity_cleanup(&sd->entity);
  274. err_clean_pads:
  275. vimc_pads_cleanup(ved->pads);
  276. return ret;
  277. }
  278. void vimc_ent_sd_unregister(struct vimc_ent_device *ved, struct v4l2_subdev *sd)
  279. {
  280. v4l2_device_unregister_subdev(sd);
  281. media_entity_cleanup(ved->ent);
  282. vimc_pads_cleanup(ved->pads);
  283. }