vse_driver.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program;
  43. *
  44. *****************************************************************************
  45. *
  46. * Note: This software is released under dual MIT and GPL licenses. A
  47. * recipient may use this file under the terms of either the MIT license or
  48. * GPL License. If you wish to use only one license not the other, you can
  49. * indicate your decision by deleting one of the above license notices in your
  50. * version of this file.
  51. *
  52. *****************************************************************************/
  53. #include <linux/delay.h>
  54. #include <linux/clk.h>
  55. #include <linux/io.h>
  56. #include <linux/module.h>
  57. #include <linux/proc_fs.h>
  58. #include <linux/debugfs.h>
  59. #include <linux/of_device.h>
  60. #include <linux/sched_clock.h>
  61. #include <linux/videodev2.h>
  62. #include <media/v4l2-ctrls.h>
  63. #include <media/v4l2-subdev.h>
  64. #include <media/v4l2-device.h>
  65. #include <media/v4l2-ioctl.h>
  66. #include <media/v4l2-event.h>
  67. #include <media/media-entity.h>
  68. #include <uapi/linux/media-bus-format.h>
  69. #include "vse_driver.h"
  70. #include "vse_ioctl.h"
  71. #define DEVICE_NAME "vvcam-vse"
  72. int vse_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  73. struct v4l2_event_subscription *sub)
  74. {
  75. return v4l2_event_subscribe(fh, sub, 2, NULL);
  76. }
  77. int vse_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  78. struct v4l2_event_subscription *sub)
  79. {
  80. return v4l2_event_unsubscribe(fh, sub);
  81. }
  82. #ifdef CONFIG_COMPAT
  83. static long vse_ioctl_compat(struct v4l2_subdev *sd,
  84. unsigned int cmd, void *arg)
  85. {
  86. struct vse_device *vse_dev = v4l2_get_subdevdata(sd);
  87. return vse_priv_ioctl(&vse_dev->ic_dev, cmd, arg);
  88. }
  89. long vse_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  90. {
  91. return vse_ioctl_compat(sd, cmd, arg);
  92. }
  93. #else /* CONFIG_COMPAT */
  94. long vse_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  95. {
  96. struct vse_device *vse_dev = v4l2_get_subdevdata(sd);
  97. return vse_priv_ioctl(&^vse_dev->ic_dev, cmd, arg);
  98. }
  99. #endif /* CONFIG_COMPAT */
  100. int vse_set_stream(struct v4l2_subdev *sd, int enable)
  101. {
  102. return 0;
  103. }
  104. static struct v4l2_subdev_core_ops vse_v4l2_subdev_core_ops = {
  105. .ioctl = vse_ioctl,
  106. .subscribe_event = vse_subscribe_event,
  107. .unsubscribe_event = vse_unsubscribe_event,
  108. };
  109. static struct v4l2_subdev_video_ops vse_v4l2_subdev_video_ops = {
  110. .s_stream = vse_set_stream,
  111. };
  112. static struct v4l2_subdev_ops vse_v4l2_subdev_ops = {
  113. .core = &vse_v4l2_subdev_core_ops,
  114. .video = &vse_v4l2_subdev_video_ops,
  115. };
  116. int vse_hw_probe(struct platform_device *pdev)
  117. {
  118. struct vse_device *vse_dev;
  119. int rc = 0;
  120. pr_info("enter %s\n", __func__);
  121. vse_dev = kzalloc(sizeof(struct vse_device), GFP_KERNEL);
  122. if (!vse_dev) {
  123. rc = -ENOMEM;
  124. goto end;
  125. }
  126. v4l2_subdev_init(&vse_dev->sd, &vse_v4l2_subdev_ops);
  127. snprintf(vse_dev->sd.name, sizeof(vse_dev->sd.name), DEVICE_NAME);
  128. vse_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  129. vse_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
  130. vse_dev->sd.owner = THIS_MODULE;
  131. v4l2_set_subdevdata(&vse_dev->sd, vse_dev);
  132. vse_dev->vd = kzalloc(sizeof(*vse_dev->vd), GFP_KERNEL);
  133. if (WARN_ON(!vse_dev->vd)) {
  134. rc = -ENOMEM;
  135. goto end;
  136. }
  137. rc = v4l2_device_register(&(pdev->dev), vse_dev->vd);
  138. if (WARN_ON(rc < 0))
  139. goto end;
  140. rc = v4l2_device_register_subdev(vse_dev->vd, &vse_dev->sd);
  141. if (rc) {
  142. pr_err("failed to register subdev %d\n", rc);
  143. goto end;
  144. }
  145. vse_dev->ic_dev.base = ioremap(VSE_REG_BASE, VSE_REG_SIZE);
  146. #ifdef VSE_REG_RESET
  147. vse_dev->ic_dev.reset = ioremap(VSE_REG_RESET, 4);
  148. #endif
  149. pr_info("vse ioremap addr: 0x%08x 0x%08x %px", VSE_REG_BASE,
  150. VSE_REG_SIZE, vse_dev->ic_dev.base);
  151. platform_set_drvdata(pdev, vse_dev);
  152. rc = v4l2_device_register_subdev_nodes(vse_dev->vd);
  153. return rc;
  154. end:
  155. return rc;
  156. }
  157. int vse_hw_remove(struct platform_device *pdev)
  158. {
  159. struct vse_device *vse = platform_get_drvdata(pdev);
  160. pr_info("enter %s\n", __func__);
  161. if (!vse)
  162. return -1;
  163. v4l2_device_unregister_subdev(&vse->sd);
  164. v4l2_device_disconnect(vse->vd);
  165. v4l2_device_put(vse->vd);
  166. iounmap(vse->ic_dev.base);
  167. kzfree(vse);
  168. return 0;
  169. }
  170. static struct platform_driver viv_vse_driver = {
  171. .probe = vse_hw_probe,
  172. .remove = vse_hw_remove,
  173. .driver = {
  174. .name = DEVICE_NAME,
  175. .owner = THIS_MODULE,
  176. }
  177. };
  178. static void vse_pdev_release(struct device *dev)
  179. {
  180. pr_info("enter %s\n", __func__);
  181. }
  182. static struct platform_device viv_vse_pdev = {
  183. .name = DEVICE_NAME,
  184. .dev.release = vse_pdev_release,
  185. };
  186. static int __init viv_vse_init_module(void)
  187. {
  188. int ret = 0;
  189. pr_info("enter %s\n", __func__);
  190. ret = platform_device_register(&viv_vse_pdev);
  191. if (ret) {
  192. pr_err("register platform device failed.\n");
  193. return ret;
  194. }
  195. ret = platform_driver_register(&viv_vse_driver);
  196. if (ret) {
  197. pr_err("register platform driver failed.\n");
  198. platform_device_unregister(&viv_vse_pdev);
  199. return ret;
  200. }
  201. return ret;
  202. }
  203. static void __exit viv_vse_exit_module(void)
  204. {
  205. pr_info("enter %s\n", __func__);
  206. platform_driver_unregister(&viv_vse_driver);
  207. platform_device_unregister(&viv_vse_pdev);
  208. }
  209. module_init(viv_vse_init_module);
  210. module_exit(viv_vse_exit_module);
  211. MODULE_AUTHOR("zhiye.yin@verisilicon.com");
  212. MODULE_LICENSE("GPL");
  213. MODULE_ALIAS("VSE");
  214. MODULE_VERSION("1.0");