vimc-sensor.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. static int vimc_sen_tpg_thread(void *data)
  97. {
  98. struct vimc_sen_device *vsen = data;
  99. unsigned int i;
  100. set_freezable();
  101. set_current_state(TASK_UNINTERRUPTIBLE);
  102. for (;;) {
  103. try_to_freeze();
  104. if (kthread_should_stop())
  105. break;
  106. tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
  107. /* Send the frame to all source pads */
  108. for (i = 0; i < vsen->sd.entity.num_pads; i++)
  109. vimc_propagate_frame(&vsen->sd.entity.pads[i],
  110. vsen->frame);
  111. /* 60 frames per second */
  112. schedule_timeout(HZ/60);
  113. }
  114. return 0;
  115. }
  116. static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
  117. {
  118. struct vimc_sen_device *vsen =
  119. container_of(sd, struct vimc_sen_device, sd);
  120. int ret;
  121. if (enable) {
  122. const struct vimc_pix_map *vpix;
  123. unsigned int frame_size;
  124. if (vsen->kthread_sen)
  125. /* tpg is already executing */
  126. return 0;
  127. /* Calculate the frame size */
  128. vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
  129. frame_size = vsen->mbus_format.width * vpix->bpp *
  130. vsen->mbus_format.height;
  131. /*
  132. * Allocate the frame buffer. Use vmalloc to be able to
  133. * allocate a large amount of memory
  134. */
  135. vsen->frame = vmalloc(frame_size);
  136. if (!vsen->frame)
  137. return -ENOMEM;
  138. /* configure the test pattern generator */
  139. vimc_sen_tpg_s_format(vsen);
  140. /* Initialize the image generator thread */
  141. vsen->kthread_sen = kthread_run(vimc_sen_tpg_thread, vsen,
  142. "%s-sen", vsen->sd.v4l2_dev->name);
  143. if (IS_ERR(vsen->kthread_sen)) {
  144. dev_err(vsen->sd.v4l2_dev->dev,
  145. "%s: kernel_thread() failed\n", vsen->sd.name);
  146. vfree(vsen->frame);
  147. vsen->frame = NULL;
  148. return PTR_ERR(vsen->kthread_sen);
  149. }
  150. } else {
  151. if (!vsen->kthread_sen)
  152. return 0;
  153. /* Stop image generator */
  154. ret = kthread_stop(vsen->kthread_sen);
  155. if (ret)
  156. return ret;
  157. vsen->kthread_sen = NULL;
  158. vfree(vsen->frame);
  159. vsen->frame = NULL;
  160. return 0;
  161. }
  162. return 0;
  163. }
  164. struct v4l2_subdev_video_ops vimc_sen_video_ops = {
  165. .s_stream = vimc_sen_s_stream,
  166. };
  167. static const struct v4l2_subdev_ops vimc_sen_ops = {
  168. .pad = &vimc_sen_pad_ops,
  169. .video = &vimc_sen_video_ops,
  170. };
  171. static void vimc_sen_destroy(struct vimc_ent_device *ved)
  172. {
  173. struct vimc_sen_device *vsen =
  174. container_of(ved, struct vimc_sen_device, ved);
  175. vimc_ent_sd_unregister(ved, &vsen->sd);
  176. tpg_free(&vsen->tpg);
  177. kfree(vsen);
  178. }
  179. struct vimc_ent_device *vimc_sen_create(struct v4l2_device *v4l2_dev,
  180. const char *const name,
  181. u16 num_pads,
  182. const unsigned long *pads_flag)
  183. {
  184. struct vimc_sen_device *vsen;
  185. unsigned int i;
  186. int ret;
  187. /* NOTE: a sensor node may be created with more then one pad */
  188. if (!name || !num_pads || !pads_flag)
  189. return ERR_PTR(-EINVAL);
  190. /* check if all pads are sources */
  191. for (i = 0; i < num_pads; i++)
  192. if (!(pads_flag[i] & MEDIA_PAD_FL_SOURCE))
  193. return ERR_PTR(-EINVAL);
  194. /* Allocate the vsen struct */
  195. vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
  196. if (!vsen)
  197. return ERR_PTR(-ENOMEM);
  198. /* Initialize ved and sd */
  199. ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev, name,
  200. MEDIA_ENT_F_CAM_SENSOR, num_pads, pads_flag,
  201. &vimc_sen_ops, vimc_sen_destroy);
  202. if (ret)
  203. goto err_free_vsen;
  204. /* Set the active frame format (this is hardcoded for now) */
  205. vsen->mbus_format.width = 640;
  206. vsen->mbus_format.height = 480;
  207. vsen->mbus_format.code = MEDIA_BUS_FMT_RGB888_1X24;
  208. vsen->mbus_format.field = V4L2_FIELD_NONE;
  209. vsen->mbus_format.colorspace = V4L2_COLORSPACE_SRGB;
  210. vsen->mbus_format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
  211. vsen->mbus_format.xfer_func = V4L2_XFER_FUNC_SRGB;
  212. /* Initialize the test pattern generator */
  213. tpg_init(&vsen->tpg, vsen->mbus_format.width,
  214. vsen->mbus_format.height);
  215. ret = tpg_alloc(&vsen->tpg, VIMC_SEN_FRAME_MAX_WIDTH);
  216. if (ret)
  217. goto err_unregister_ent_sd;
  218. return &vsen->ved;
  219. err_unregister_ent_sd:
  220. vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
  221. err_free_vsen:
  222. kfree(vsen);
  223. return ERR_PTR(ret);
  224. }