vimc-sensor.c 11 KB

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