vimc-sensor.c 6.9 KB

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