vimc-sensor.c 12 KB

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