vimc-sensor.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/component.h>
  18. #include <linux/freezer.h>
  19. #include <linux/kthread.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/v4l2-mediabus.h>
  23. #include <linux/vmalloc.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/v4l2-tpg.h>
  26. #include "vimc-common.h"
  27. #define VIMC_SEN_DRV_NAME "vimc-sensor"
  28. struct vimc_sen_device {
  29. struct vimc_ent_device ved;
  30. struct v4l2_subdev sd;
  31. struct device *dev;
  32. struct tpg_data tpg;
  33. struct task_struct *kthread_sen;
  34. u8 *frame;
  35. /* The active format */
  36. struct v4l2_mbus_framefmt mbus_format;
  37. };
  38. static const struct v4l2_mbus_framefmt fmt_default = {
  39. .width = 640,
  40. .height = 480,
  41. .code = MEDIA_BUS_FMT_RGB888_1X24,
  42. .field = V4L2_FIELD_NONE,
  43. .colorspace = V4L2_COLORSPACE_DEFAULT,
  44. };
  45. static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
  46. struct v4l2_subdev_pad_config *cfg)
  47. {
  48. unsigned int i;
  49. for (i = 0; i < sd->entity.num_pads; i++) {
  50. struct v4l2_mbus_framefmt *mf;
  51. mf = v4l2_subdev_get_try_format(sd, cfg, i);
  52. *mf = fmt_default;
  53. }
  54. return 0;
  55. }
  56. static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd,
  57. struct v4l2_subdev_pad_config *cfg,
  58. struct v4l2_subdev_mbus_code_enum *code)
  59. {
  60. const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
  61. if (!vpix)
  62. return -EINVAL;
  63. code->code = vpix->code;
  64. return 0;
  65. }
  66. static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
  67. struct v4l2_subdev_pad_config *cfg,
  68. struct v4l2_subdev_frame_size_enum *fse)
  69. {
  70. const struct vimc_pix_map *vpix;
  71. if (fse->index)
  72. return -EINVAL;
  73. /* Only accept code in the pix map table */
  74. vpix = vimc_pix_map_by_code(fse->code);
  75. if (!vpix)
  76. return -EINVAL;
  77. fse->min_width = VIMC_FRAME_MIN_WIDTH;
  78. fse->max_width = VIMC_FRAME_MAX_WIDTH;
  79. fse->min_height = VIMC_FRAME_MIN_HEIGHT;
  80. fse->max_height = VIMC_FRAME_MAX_HEIGHT;
  81. return 0;
  82. }
  83. static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
  84. struct v4l2_subdev_pad_config *cfg,
  85. struct v4l2_subdev_format *fmt)
  86. {
  87. struct vimc_sen_device *vsen =
  88. container_of(sd, struct vimc_sen_device, sd);
  89. fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
  90. *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
  91. vsen->mbus_format;
  92. return 0;
  93. }
  94. static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
  95. {
  96. const struct vimc_pix_map *vpix =
  97. vimc_pix_map_by_code(vsen->mbus_format.code);
  98. tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
  99. vsen->mbus_format.height, vsen->mbus_format.field);
  100. tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
  101. tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
  102. tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
  103. /* TODO: add support for V4L2_FIELD_ALTERNATE */
  104. tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
  105. tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
  106. tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
  107. tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
  108. tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
  109. }
  110. static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
  111. {
  112. const struct vimc_pix_map *vpix;
  113. /* Only accept code in the pix map table */
  114. vpix = vimc_pix_map_by_code(fmt->code);
  115. if (!vpix)
  116. fmt->code = fmt_default.code;
  117. fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
  118. VIMC_FRAME_MAX_WIDTH) & ~1;
  119. fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
  120. VIMC_FRAME_MAX_HEIGHT) & ~1;
  121. /* TODO: add support for V4L2_FIELD_ALTERNATE */
  122. if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
  123. fmt->field = fmt_default.field;
  124. vimc_colorimetry_clamp(fmt);
  125. }
  126. static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
  127. struct v4l2_subdev_pad_config *cfg,
  128. struct v4l2_subdev_format *fmt)
  129. {
  130. struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
  131. struct v4l2_mbus_framefmt *mf;
  132. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  133. /* Do not change the format while stream is on */
  134. if (vsen->frame)
  135. return -EBUSY;
  136. mf = &vsen->mbus_format;
  137. } else {
  138. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  139. }
  140. /* Set the new format */
  141. vimc_sen_adjust_fmt(&fmt->format);
  142. dev_dbg(vsen->dev, "%s: format update: "
  143. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  144. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
  145. /* old */
  146. mf->width, mf->height, mf->code,
  147. mf->colorspace, mf->quantization,
  148. mf->xfer_func, mf->ycbcr_enc,
  149. /* new */
  150. fmt->format.width, fmt->format.height, fmt->format.code,
  151. fmt->format.colorspace, fmt->format.quantization,
  152. fmt->format.xfer_func, fmt->format.ycbcr_enc);
  153. *mf = fmt->format;
  154. return 0;
  155. }
  156. static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
  157. .init_cfg = vimc_sen_init_cfg,
  158. .enum_mbus_code = vimc_sen_enum_mbus_code,
  159. .enum_frame_size = vimc_sen_enum_frame_size,
  160. .get_fmt = vimc_sen_get_fmt,
  161. .set_fmt = vimc_sen_set_fmt,
  162. };
  163. static int vimc_sen_tpg_thread(void *data)
  164. {
  165. struct vimc_sen_device *vsen = data;
  166. unsigned int i;
  167. set_freezable();
  168. set_current_state(TASK_UNINTERRUPTIBLE);
  169. for (;;) {
  170. try_to_freeze();
  171. if (kthread_should_stop())
  172. break;
  173. tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
  174. /* Send the frame to all source pads */
  175. for (i = 0; i < vsen->sd.entity.num_pads; i++)
  176. vimc_propagate_frame(&vsen->sd.entity.pads[i],
  177. vsen->frame);
  178. /* 60 frames per second */
  179. schedule_timeout(HZ/60);
  180. }
  181. return 0;
  182. }
  183. static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
  184. {
  185. struct vimc_sen_device *vsen =
  186. container_of(sd, struct vimc_sen_device, sd);
  187. int ret;
  188. if (enable) {
  189. const struct vimc_pix_map *vpix;
  190. unsigned int frame_size;
  191. if (vsen->kthread_sen)
  192. /* tpg is already executing */
  193. return 0;
  194. /* Calculate the frame size */
  195. vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
  196. frame_size = vsen->mbus_format.width * vpix->bpp *
  197. vsen->mbus_format.height;
  198. /*
  199. * Allocate the frame buffer. Use vmalloc to be able to
  200. * allocate a large amount of memory
  201. */
  202. vsen->frame = vmalloc(frame_size);
  203. if (!vsen->frame)
  204. return -ENOMEM;
  205. /* configure the test pattern generator */
  206. vimc_sen_tpg_s_format(vsen);
  207. /* Initialize the image generator thread */
  208. vsen->kthread_sen = kthread_run(vimc_sen_tpg_thread, vsen,
  209. "%s-sen", vsen->sd.v4l2_dev->name);
  210. if (IS_ERR(vsen->kthread_sen)) {
  211. dev_err(vsen->dev, "%s: kernel_thread() failed\n",
  212. vsen->sd.name);
  213. vfree(vsen->frame);
  214. vsen->frame = NULL;
  215. return PTR_ERR(vsen->kthread_sen);
  216. }
  217. } else {
  218. if (!vsen->kthread_sen)
  219. return 0;
  220. /* Stop image generator */
  221. ret = kthread_stop(vsen->kthread_sen);
  222. if (ret)
  223. return ret;
  224. vsen->kthread_sen = NULL;
  225. vfree(vsen->frame);
  226. vsen->frame = NULL;
  227. return 0;
  228. }
  229. return 0;
  230. }
  231. struct v4l2_subdev_video_ops vimc_sen_video_ops = {
  232. .s_stream = vimc_sen_s_stream,
  233. };
  234. static const struct v4l2_subdev_ops vimc_sen_ops = {
  235. .pad = &vimc_sen_pad_ops,
  236. .video = &vimc_sen_video_ops,
  237. };
  238. static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
  239. void *master_data)
  240. {
  241. struct vimc_ent_device *ved = dev_get_drvdata(comp);
  242. struct vimc_sen_device *vsen =
  243. container_of(ved, struct vimc_sen_device, ved);
  244. vimc_ent_sd_unregister(ved, &vsen->sd);
  245. tpg_free(&vsen->tpg);
  246. kfree(vsen);
  247. }
  248. static int vimc_sen_comp_bind(struct device *comp, struct device *master,
  249. void *master_data)
  250. {
  251. struct v4l2_device *v4l2_dev = master_data;
  252. struct vimc_platform_data *pdata = comp->platform_data;
  253. struct vimc_sen_device *vsen;
  254. int ret;
  255. /* Allocate the vsen struct */
  256. vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
  257. if (!vsen)
  258. return -ENOMEM;
  259. /* Initialize ved and sd */
  260. ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
  261. pdata->entity_name,
  262. MEDIA_ENT_F_ATV_DECODER, 1,
  263. (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
  264. &vimc_sen_ops);
  265. if (ret)
  266. goto err_free_vsen;
  267. dev_set_drvdata(comp, &vsen->ved);
  268. vsen->dev = comp;
  269. /* Initialize the frame format */
  270. vsen->mbus_format = fmt_default;
  271. /* Initialize the test pattern generator */
  272. tpg_init(&vsen->tpg, vsen->mbus_format.width,
  273. vsen->mbus_format.height);
  274. ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
  275. if (ret)
  276. goto err_unregister_ent_sd;
  277. return 0;
  278. err_unregister_ent_sd:
  279. vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
  280. err_free_vsen:
  281. kfree(vsen);
  282. return ret;
  283. }
  284. static const struct component_ops vimc_sen_comp_ops = {
  285. .bind = vimc_sen_comp_bind,
  286. .unbind = vimc_sen_comp_unbind,
  287. };
  288. static int vimc_sen_probe(struct platform_device *pdev)
  289. {
  290. return component_add(&pdev->dev, &vimc_sen_comp_ops);
  291. }
  292. static int vimc_sen_remove(struct platform_device *pdev)
  293. {
  294. component_del(&pdev->dev, &vimc_sen_comp_ops);
  295. return 0;
  296. }
  297. static struct platform_driver vimc_sen_pdrv = {
  298. .probe = vimc_sen_probe,
  299. .remove = vimc_sen_remove,
  300. .driver = {
  301. .name = VIMC_SEN_DRV_NAME,
  302. },
  303. };
  304. static const struct platform_device_id vimc_sen_driver_ids[] = {
  305. {
  306. .name = VIMC_SEN_DRV_NAME,
  307. },
  308. { }
  309. };
  310. module_platform_driver(vimc_sen_pdrv);
  311. MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
  312. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
  313. MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
  314. MODULE_LICENSE("GPL");