dw200_driver.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "dw200_driver.h"
  70. #include "dw200_ioctl.h"
  71. #define DEVICE_NAME "vvcam-dw200"
  72. int dw200_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 dw200_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 dw200_ioctl_compat(struct v4l2_subdev *sd,
  84. unsigned int cmd, void *arg)
  85. {
  86. struct dw200_device *dw200_dev = v4l2_get_subdevdata(sd);
  87. return dw200_priv_ioctl(&dw200_dev->ic_dev, cmd, arg);
  88. }
  89. long dw200_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  90. {
  91. return dw200_ioctl_compat(sd, cmd, arg);
  92. }
  93. #else /* CONFIG_COMPAT */
  94. long dw200_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  95. {
  96. struct dw200_device *dw200_dev = v4l2_get_subdevdata(sd);
  97. return dw200_priv_ioctl(&^dw200_dev->ic_dev, cmd, arg);
  98. }
  99. #endif /* CONFIG_COMPAT */
  100. int dw200_set_stream(struct v4l2_subdev *sd, int enable)
  101. {
  102. return 0;
  103. }
  104. static struct v4l2_subdev_core_ops dw200_v4l2_subdev_core_ops = {
  105. .ioctl = dw200_ioctl,
  106. .subscribe_event = dw200_subscribe_event,
  107. .unsubscribe_event = dw200_unsubscribe_event,
  108. };
  109. static struct v4l2_subdev_video_ops dw200_v4l2_subdev_video_ops = {
  110. .s_stream = dw200_set_stream,
  111. };
  112. static struct v4l2_subdev_ops dw200_v4l2_subdev_ops = {
  113. .core = &dw200_v4l2_subdev_core_ops,
  114. .video = &dw200_v4l2_subdev_video_ops,
  115. };
  116. int dw200_hw_probe(struct platform_device *pdev)
  117. {
  118. struct dw200_device *dw200_dev;
  119. int rc = 0;
  120. pr_info("enter %s\n", __func__);
  121. dw200_dev = kzalloc(sizeof(struct dw200_device), GFP_KERNEL);
  122. if (!dw200_dev) {
  123. rc = -ENOMEM;
  124. goto end;
  125. }
  126. v4l2_subdev_init(&dw200_dev->sd, &dw200_v4l2_subdev_ops);
  127. snprintf(dw200_dev->sd.name, sizeof(dw200_dev->sd.name), DEVICE_NAME);
  128. dw200_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  129. dw200_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
  130. dw200_dev->sd.owner = THIS_MODULE;
  131. v4l2_set_subdevdata(&dw200_dev->sd, dw200_dev);
  132. dw200_dev->vd = kzalloc(sizeof(*dw200_dev->vd), GFP_KERNEL);
  133. if (WARN_ON(!dw200_dev->vd)) {
  134. rc = -ENOMEM;
  135. goto end;
  136. }
  137. rc = v4l2_device_register(&(pdev->dev), dw200_dev->vd);
  138. if (WARN_ON(rc < 0))
  139. goto end;
  140. rc = v4l2_device_register_subdev(dw200_dev->vd, &dw200_dev->sd);
  141. if (rc) {
  142. pr_err("failed to register subdev %d\n", rc);
  143. goto end;
  144. }
  145. dw200_dev->ic_dev.dwe_base = ioremap(DWE_REG_BASE, DWE_REG_SIZE);
  146. dw200_dev->ic_dev.vse_base = ioremap(VSE_REG_BASE, VSE_REG_SIZE);
  147. #ifdef DWE_REG_RESET
  148. dw200_dev->ic_dev.dwe_reset = ioremap(DWE_REG_RESET, 4);
  149. #endif
  150. #ifdef VSE_REG_RESET
  151. dw200_dev->ic_dev.vse_reset = ioremap(VSE_REG_RESET, 4);
  152. #endif
  153. pr_info("dw200 ioremap addr: 0x%08x 0x%08x %px", DWE_REG_BASE,
  154. DWE_REG_SIZE, dw200_dev->ic_dev.dwe_base);
  155. platform_set_drvdata(pdev, dw200_dev);
  156. rc = v4l2_device_register_subdev_nodes(dw200_dev->vd);
  157. return 0;
  158. end:
  159. return rc;
  160. }
  161. int dw200_hw_remove(struct platform_device *pdev)
  162. {
  163. struct dw200_device *dw200 = platform_get_drvdata(pdev);
  164. pr_info("enter %s\n", __func__);
  165. if (!dw200)
  166. return -EINVAL;
  167. v4l2_device_unregister_subdev(&dw200->sd);
  168. v4l2_device_disconnect(dw200->vd);
  169. v4l2_device_put(dw200->vd);
  170. iounmap(dw200->ic_dev.dwe_base);
  171. iounmap(dw200->ic_dev.vse_base);
  172. #ifdef DWE_REG_RESET
  173. iounmap(dw200->ic_dev.dwe_reset);
  174. #endif
  175. #ifdef VSE_REG_RESET
  176. iounmap(dw200->ic_dev.vse_reset);
  177. #endif
  178. kzfree(dw200);
  179. return 0;
  180. }
  181. static struct platform_driver viv_dw200_driver = {
  182. .probe = dw200_hw_probe,
  183. .remove = dw200_hw_remove,
  184. .driver = {
  185. .name = DEVICE_NAME,
  186. .owner = THIS_MODULE,
  187. }
  188. };
  189. static void dw200_pdev_release(struct device *dev)
  190. {
  191. pr_info("enter %s\n", __func__);
  192. }
  193. static struct platform_device viv_dw200_pdev = {
  194. .name = DEVICE_NAME,
  195. .dev.release = dw200_pdev_release,
  196. };
  197. static int __init viv_dw200_init_module(void)
  198. {
  199. int ret = 0;
  200. pr_info("enter %s\n", __func__);
  201. ret = platform_device_register(&viv_dw200_pdev);
  202. if (ret) {
  203. pr_err("register platform device failed.\n");
  204. return ret;
  205. }
  206. ret = platform_driver_register(&viv_dw200_driver);
  207. if (ret) {
  208. pr_err("register platform driver failed.\n");
  209. platform_device_unregister(&viv_dw200_pdev);
  210. return ret;
  211. }
  212. return ret;
  213. }
  214. static void __exit viv_dw200_exit_module(void)
  215. {
  216. pr_info("enter %s\n", __func__);
  217. platform_driver_unregister(&viv_dw200_driver);
  218. platform_device_unregister(&viv_dw200_pdev);
  219. }
  220. module_init(viv_dw200_init_module);
  221. module_exit(viv_dw200_exit_module);
  222. MODULE_AUTHOR("zhiye.yin@verisilicon.com");
  223. MODULE_LICENSE("GPL");
  224. MODULE_ALIAS("Dewarp200");
  225. MODULE_VERSION("1.0");