vvcam_soc_driver.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <asm/io.h>
  54. #include <linux/cdev.h>
  55. #include <linux/module.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/delay.h>
  58. #include <linux/clk.h>
  59. #include <linux/io.h>
  60. #include <linux/mm.h>
  61. #include <linux/slab.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/debugfs.h>
  64. #include <linux/miscdevice.h>
  65. #include <linux/uaccess.h>
  66. #include "soc_ioctl.h"
  67. #define VIVCAM_SOC_NAME "vivsoc"
  68. #define VIVCAM_SOC_MAXCNT 2
  69. struct vvcam_soc_driver_dev
  70. {
  71. struct cdev cdev;
  72. dev_t devt;
  73. struct class *class;
  74. struct mutex vvmutex;
  75. void *private;
  76. };
  77. static unsigned int vvcam_soc_major = 0;
  78. static unsigned int vvcam_soc_minor = 0;
  79. static struct class *vvcam_soc_class;
  80. static unsigned int devise_register_index = 0;
  81. static int vvcam_soc_open(struct inode * inode, struct file * file)
  82. {
  83. struct vvcam_soc_driver_dev *pdriver_dev;
  84. pdriver_dev = container_of(inode->i_cdev, struct vvcam_soc_driver_dev, cdev);
  85. file->private_data = pdriver_dev;
  86. return 0;
  87. };
  88. static long vvcam_soc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  89. {
  90. long ret = 0;
  91. struct vvcam_soc_driver_dev *pdriver_dev;
  92. struct vvcam_soc_dev* psoc_dev;
  93. pdriver_dev = file->private_data;
  94. if (pdriver_dev == NULL)
  95. {
  96. pr_err("%s:file private is null point error\n", __func__);
  97. return -ENOMEM;
  98. }
  99. psoc_dev = pdriver_dev->private;
  100. ret = soc_priv_ioctl(psoc_dev, cmd , (void __user *)arg);
  101. return ret;
  102. };
  103. static int vvcam_soc_release(struct inode * inode, struct file * file)
  104. {
  105. return 0;
  106. };
  107. static struct file_operations vvcam_soc_fops = {
  108. .owner = THIS_MODULE,
  109. .open = vvcam_soc_open,
  110. .release = vvcam_soc_release,
  111. .unlocked_ioctl = vvcam_soc_ioctl,
  112. };
  113. static int vvcam_soc_probe(struct platform_device *pdev)
  114. {
  115. int ret = 0;
  116. struct vvcam_soc_driver_dev *pdriver_dev;
  117. struct vvcam_soc_dev * psoc_dev;
  118. // struct resource *mem;
  119. pr_info("enter %s\n", __func__);
  120. if (pdev->id >= VIVCAM_SOC_MAXCNT)
  121. {
  122. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  123. return -EINVAL;
  124. }
  125. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_soc_driver_dev), GFP_KERNEL);
  126. if (pdriver_dev == NULL)
  127. {
  128. pr_err("%s:alloc struct vvcam_soc_driver_dev error\n", __func__);
  129. return -ENOMEM;
  130. }
  131. memset(pdriver_dev,0,sizeof(struct vvcam_soc_driver_dev ));
  132. psoc_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_soc_dev), GFP_KERNEL);
  133. if (psoc_dev == NULL)
  134. {
  135. pr_err("%s:alloc struct vvcam_soc_dev error\n", __func__);
  136. return -ENOMEM;
  137. }
  138. memset(psoc_dev,0,sizeof(struct vvcam_soc_dev ));
  139. //mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. //psoc_dev->base = devm_ioremap_resource(&pdev->dev, mem);
  141. psoc_dev->base = ioremap(VVCTRL_BASE, VVCTRL_SIZE);
  142. if (IS_ERR(psoc_dev->base ))
  143. return PTR_ERR(psoc_dev->base );
  144. pdriver_dev->private = psoc_dev;
  145. mutex_init(&pdriver_dev->vvmutex);
  146. platform_set_drvdata(pdev, pdriver_dev);
  147. vvnative_soc_init(psoc_dev);
  148. if (devise_register_index == 0)
  149. {
  150. if (vvcam_soc_major == 0)
  151. {
  152. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, VIVCAM_SOC_MAXCNT, VIVCAM_SOC_NAME);
  153. if (ret != 0)
  154. {
  155. pr_err("%s:alloc_chrdev_region error\n", __func__);
  156. return ret;
  157. }
  158. vvcam_soc_major = MAJOR(pdriver_dev->devt);
  159. vvcam_soc_minor = MINOR(pdriver_dev->devt);
  160. }
  161. else
  162. {
  163. pdriver_dev->devt = MKDEV(vvcam_soc_major, vvcam_soc_minor);
  164. ret = register_chrdev_region(pdriver_dev->devt, VIVCAM_SOC_MAXCNT, VIVCAM_SOC_NAME);
  165. if (ret)
  166. {
  167. pr_err("%s:register_chrdev_region error\n", __func__);
  168. return ret;
  169. }
  170. }
  171. vvcam_soc_class = class_create(THIS_MODULE, VIVCAM_SOC_NAME);
  172. if (IS_ERR(vvcam_soc_class))
  173. {
  174. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  175. return -EINVAL;
  176. }
  177. }
  178. pdriver_dev->devt = MKDEV(vvcam_soc_major, vvcam_soc_minor + pdev->id);
  179. cdev_init(&pdriver_dev->cdev, &vvcam_soc_fops);
  180. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  181. if ( ret )
  182. {
  183. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  184. return ret;
  185. }
  186. pdriver_dev->class = vvcam_soc_class;
  187. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  188. pdriver_dev, "%s%d", VIVCAM_SOC_NAME, pdev->id);
  189. devise_register_index++;
  190. pr_info("exit %s\n", __func__);
  191. return ret;
  192. }
  193. static int vvcam_soc_remove(struct platform_device *pdev)
  194. {
  195. struct vvcam_soc_driver_dev *pdriver_dev;
  196. struct vvcam_soc_dev * psoc_dev;
  197. pr_info("enter %s\n", __func__);
  198. devise_register_index--;
  199. pdriver_dev = platform_get_drvdata(pdev);
  200. psoc_dev = pdriver_dev->private;
  201. iounmap(psoc_dev->base);
  202. cdev_del(&pdriver_dev->cdev);
  203. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  204. unregister_chrdev_region(pdriver_dev->devt, VIVCAM_SOC_MAXCNT);
  205. if (devise_register_index == 0)
  206. {
  207. class_destroy(pdriver_dev->class);
  208. }
  209. return 0;
  210. }
  211. static struct platform_driver vvcam_soc_driver = {
  212. .probe = vvcam_soc_probe,
  213. .remove = vvcam_soc_remove,
  214. .driver = {
  215. .name = VIVCAM_SOC_NAME,
  216. .owner = THIS_MODULE,
  217. }
  218. };
  219. static void vvcam_soc_pdev_release(struct device *dev)
  220. {
  221. pr_info("enter %s\n", __func__);
  222. }
  223. static struct resource vvcam_soc_resource[] = {
  224. [0] = {
  225. .start = VVCTRL_BASE,
  226. .end = VVCTRL_BASE + VVCTRL_SIZE - 1,
  227. .flags = IORESOURCE_MEM,
  228. },
  229. };
  230. static struct platform_device vvcam_soc_pdev = {
  231. .name = VIVCAM_SOC_NAME,
  232. .id = 0,
  233. .resource = vvcam_soc_resource,
  234. .num_resources = 1,
  235. .dev.release = vvcam_soc_pdev_release,
  236. };
  237. static int __init vvcam_soc_init_module(void)
  238. {
  239. int ret = 0;
  240. pr_info("enter %s\n", __func__);
  241. ret = platform_device_register(&vvcam_soc_pdev);
  242. if (ret)
  243. {
  244. pr_err("register platform device failed.\n");
  245. return ret;
  246. }
  247. ret = platform_driver_register(&vvcam_soc_driver);
  248. if (ret)
  249. {
  250. pr_err("register platform driver failed.\n");
  251. platform_device_unregister(&vvcam_soc_pdev);
  252. return ret;
  253. }
  254. return ret;
  255. }
  256. static void __exit vvcam_soc_exit_module(void)
  257. {
  258. pr_info("enter %s\n", __func__);
  259. platform_driver_unregister(&vvcam_soc_driver);
  260. platform_device_unregister(&vvcam_soc_pdev);
  261. }
  262. module_init(vvcam_soc_init_module);
  263. module_exit(vvcam_soc_exit_module);
  264. MODULE_DESCRIPTION("ISP");
  265. MODULE_LICENSE("GPL");