vimc-sensor.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * vimc-sensor.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/freezer.h>
  18. #include <linux/kthread.h>
  19. #include <linux/v4l2-mediabus.h>
  20. #include <linux/vmalloc.h>
  21. #include <media/v4l2-subdev.h>
  22. #include <media/v4l2-tpg.h>
  23. #include "vimc-sensor.h"
  24. #define VIMC_SEN_FRAME_MAX_WIDTH 4096
  25. struct vimc_sen_device {
  26. struct vimc_ent_device ved;
  27. struct v4l2_subdev sd;
  28. struct tpg_data tpg;
  29. struct task_struct *kthread_sen;
  30. u8 *frame;
  31. /* The active format */
  32. struct v4l2_mbus_framefmt mbus_format;
  33. };
  34. static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd,
  35. struct v4l2_subdev_pad_config *cfg,
  36. struct v4l2_subdev_mbus_code_enum *code)
  37. {
  38. struct vimc_sen_device *vsen =
  39. container_of(sd, struct vimc_sen_device, sd);
  40. /* TODO: Add support for other codes */
  41. if (code->index)
  42. return -EINVAL;
  43. code->code = vsen->mbus_format.code;
  44. return 0;
  45. }
  46. static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
  47. struct v4l2_subdev_pad_config *cfg,
  48. struct v4l2_subdev_frame_size_enum *fse)
  49. {
  50. struct vimc_sen_device *vsen =
  51. container_of(sd, struct vimc_sen_device, sd);
  52. /* TODO: Add support to other formats */
  53. if (fse->index)
  54. return -EINVAL;
  55. /* TODO: Add support for other codes */
  56. if (fse->code != vsen->mbus_format.code)
  57. return -EINVAL;
  58. fse->min_width = vsen->mbus_format.width;
  59. fse->max_width = vsen->mbus_format.width;
  60. fse->min_height = vsen->mbus_format.height;
  61. fse->max_height = vsen->mbus_format.height;
  62. return 0;
  63. }
  64. static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
  65. struct v4l2_subdev_pad_config *cfg,
  66. struct v4l2_subdev_format *format)
  67. {
  68. struct vimc_sen_device *vsen =
  69. container_of(sd, struct vimc_sen_device, sd);
  70. format->format = vsen->mbus_format;
  71. return 0;
  72. }
  73. static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
  74. {
  75. const struct vimc_pix_map *vpix =
  76. vimc_pix_map_by_code(vsen->mbus_format.code);
  77. tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
  78. vsen->mbus_format.height, vsen->mbus_format.field);
  79. tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
  80. tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
  81. tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
  82. /* TODO: add support for V4L2_FIELD_ALTERNATE */
  83. tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
  84. tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
  85. tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
  86. tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
  87. tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
  88. }
  89. static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
  90. .enum_mbus_code = vimc_sen_enum_mbus_code,
  91. .enum_frame_size = vimc_sen_enum_frame_size,
  92. .get_fmt = vimc_sen_get_fmt,
  93. /* TODO: Add support to other formats */
  94. .set_fmt = vimc_sen_get_fmt,
  95. };
  96. /* media operations */
  97. static const struct media_entity_operations vimc_sen_mops = {
  98. .link_validate = v4l2_subdev_link_validate,
  99. };
  100. static int vimc_sen_tpg_thread(void *data)
  101. {
  102. struct vimc_sen_device *vsen = data;
  103. unsigned int i;
  104. set_freezable();
  105. set_current_state(TASK_UNINTERRUPTIBLE);
  106. for (;;) {
  107. try_to_freeze();
  108. if (kthread_should_stop())
  109. break;
  110. tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
  111. /* Send the frame to all source pads */
  112. for (i = 0; i < vsen->sd.entity.num_pads; i++)
  113. vimc_propagate_frame(&vsen->sd.entity.pads[i],
  114. vsen->frame);
  115. /* 60 frames per second */
  116. schedule_timeout(HZ/60);
  117. }
  118. return 0;
  119. }
  120. static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
  121. {
  122. struct vimc_sen_device *vsen =
  123. container_of(sd, struct vimc_sen_device, sd);
  124. int ret;
  125. if (enable) {
  126. const struct vimc_pix_map *vpix;
  127. unsigned int frame_size;
  128. if (vsen->kthread_sen)
  129. /* tpg is already executing */
  130. return 0;
  131. /* Calculate the frame size */
  132. vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
  133. frame_size = vsen->mbus_format.width * vpix->bpp *
  134. vsen->mbus_format.height;
  135. /*
  136. * Allocate the frame buffer. Use vmalloc to be able to
  137. * allocate a large amount of memory
  138. */
  139. vsen->frame = vmalloc(frame_size);
  140. if (!vsen->frame)
  141. return -ENOMEM;
  142. /* configure the test pattern generator */
  143. vimc_sen_tpg_s_format(vsen);
  144. /* Initialize the image generator thread */
  145. vsen->kthread_sen = kthread_run(vimc_sen_tpg_thread, vsen,
  146. "%s-sen", vsen->sd.v4l2_dev->name);
  147. if (IS_ERR(vsen->kthread_sen)) {
  148. dev_err(vsen->sd.v4l2_dev->dev,
  149. "%s: kernel_thread() failed\n", vsen->sd.name);
  150. vfree(vsen->frame);
  151. vsen->frame = NULL;
  152. return PTR_ERR(vsen->kthread_sen);
  153. }
  154. } else {
  155. if (!vsen->kthread_sen)
  156. return 0;
  157. /* Stop image generator */
  158. ret = kthread_stop(vsen->kthread_sen);
  159. if (ret)
  160. return ret;
  161. vsen->kthread_sen = NULL;
  162. vfree(vsen->frame);
  163. vsen->frame = NULL;
  164. return 0;
  165. }
  166. return 0;
  167. }
  168. struct v4l2_subdev_video_ops vimc_sen_video_ops = {
  169. .s_stream = vimc_sen_s_stream,
  170. };
  171. static const struct v4l2_subdev_ops vimc_sen_ops = {
  172. .pad = &vimc_sen_pad_ops,
  173. .video = &vimc_sen_video_ops,
  174. };
  175. static void vimc_sen_destroy(struct vimc_ent_device *ved)
  176. {
  177. struct vimc_sen_device *vsen =
  178. container_of(ved, struct vimc_sen_device, ved);
  179. tpg_free(&vsen->tpg);
  180. v4l2_device_unregister_subdev(&vsen->sd);
  181. media_entity_cleanup(ved->ent);
  182. kfree(vsen);
  183. }
  184. struct vimc_ent_device *vimc_sen_create(struct v4l2_device *v4l2_dev,
  185. const char *const name,
  186. u16 num_pads,
  187. const unsigned long *pads_flag)
  188. {
  189. struct vimc_sen_device *vsen;
  190. unsigned int i;
  191. int ret;
  192. /* NOTE: a sensor node may be created with more then one pad */
  193. if (!name || !num_pads || !pads_flag)
  194. return ERR_PTR(-EINVAL);
  195. /* check if all pads are sources */
  196. for (i = 0; i < num_pads; i++)
  197. if (!(pads_flag[i] & MEDIA_PAD_FL_SOURCE))
  198. return ERR_PTR(-EINVAL);
  199. /* Allocate the vsen struct */
  200. vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
  201. if (!vsen)
  202. return ERR_PTR(-ENOMEM);
  203. /* Allocate the pads */
  204. vsen->ved.pads = vimc_pads_init(num_pads, pads_flag);
  205. if (IS_ERR(vsen->ved.pads)) {
  206. ret = PTR_ERR(vsen->ved.pads);
  207. goto err_free_vsen;
  208. }
  209. /* Fill the vimc_ent_device struct */
  210. vsen->ved.destroy = vimc_sen_destroy;
  211. vsen->ved.ent = &vsen->sd.entity;
  212. /* Initialize the subdev */
  213. v4l2_subdev_init(&vsen->sd, &vimc_sen_ops);
  214. vsen->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  215. vsen->sd.entity.ops = &vimc_sen_mops;
  216. vsen->sd.owner = THIS_MODULE;
  217. strlcpy(vsen->sd.name, name, sizeof(vsen->sd.name));
  218. v4l2_set_subdevdata(&vsen->sd, &vsen->ved);
  219. /* Expose this subdev to user space */
  220. vsen->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  221. /* Initialize the media entity */
  222. ret = media_entity_pads_init(&vsen->sd.entity,
  223. num_pads, vsen->ved.pads);
  224. if (ret)
  225. goto err_clean_pads;
  226. /* Set the active frame format (this is hardcoded for now) */
  227. vsen->mbus_format.width = 640;
  228. vsen->mbus_format.height = 480;
  229. vsen->mbus_format.code = MEDIA_BUS_FMT_RGB888_1X24;
  230. vsen->mbus_format.field = V4L2_FIELD_NONE;
  231. vsen->mbus_format.colorspace = V4L2_COLORSPACE_SRGB;
  232. vsen->mbus_format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  233. vsen->mbus_format.xfer_func = V4L2_XFER_FUNC_SRGB;
  234. /* Initialize the test pattern generator */
  235. tpg_init(&vsen->tpg, vsen->mbus_format.width,
  236. vsen->mbus_format.height);
  237. ret = tpg_alloc(&vsen->tpg, VIMC_SEN_FRAME_MAX_WIDTH);
  238. if (ret)
  239. goto err_clean_m_ent;
  240. /* Register the subdev with the v4l2 and the media framework */
  241. ret = v4l2_device_register_subdev(v4l2_dev, &vsen->sd);
  242. if (ret) {
  243. dev_err(vsen->sd.v4l2_dev->dev,
  244. "%s: subdev register failed (err=%d)\n",
  245. vsen->sd.name, ret);
  246. goto err_free_tpg;
  247. }
  248. return &vsen->ved;
  249. err_free_tpg:
  250. tpg_free(&vsen->tpg);
  251. err_clean_m_ent:
  252. media_entity_cleanup(&vsen->sd.entity);
  253. err_clean_pads:
  254. vimc_pads_cleanup(vsen->ved.pads);
  255. err_free_vsen:
  256. kfree(vsen);
  257. return ERR_PTR(ret);
  258. }