vimc-sensor.c 12 KB

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