vvcam_csi_driver.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "csi_ioctl.h"
  67. #include "csi_common.h"
  68. #define VIVCAM_CSI_NAME "vivcsi"
  69. #define VIVCAM_CSI_MAXCNT 2
  70. struct vvcam_csi_driver_dev
  71. {
  72. struct cdev cdev;
  73. dev_t devt;
  74. struct class *class;
  75. struct mutex vvmutex;
  76. void *private;
  77. };
  78. static unsigned int vvcam_csi_major = 0;
  79. static unsigned int vvcam_csi_minor = 0;
  80. static struct class *vvcam_csi_class;
  81. static unsigned int devise_register_index = 0;
  82. static int vvcam_csi_open(struct inode * inode, struct file * file)
  83. {
  84. struct vvcam_csi_driver_dev *pdriver_dev;
  85. pdriver_dev = container_of(inode->i_cdev, struct vvcam_csi_driver_dev, cdev);
  86. file->private_data = pdriver_dev;
  87. return 0;
  88. };
  89. static long vvcam_csi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  90. {
  91. struct vvcam_csi_driver_dev *pdriver_dev;
  92. struct vvcam_csi_dev * pcsi_dev;
  93. long ret;
  94. pdriver_dev = file->private_data;
  95. if (pdriver_dev == NULL)
  96. {
  97. pr_err("%s:file private is null point error\n", __func__);
  98. return -ENOMEM;
  99. }
  100. pcsi_dev = pdriver_dev->private;
  101. pr_info("%s:pdriver_dev =0x%px\n", __func__,pdriver_dev);
  102. pr_info("%s:csi[%d] pcsi_dev =0x%px\n", __func__,pcsi_dev->device_idx,pcsi_dev);
  103. mutex_lock(&pdriver_dev->vvmutex);
  104. ret = csi_priv_ioctl(pcsi_dev, cmd, (void __user *)arg);
  105. mutex_unlock(&pdriver_dev->vvmutex);
  106. return ret;
  107. };
  108. static int vvcam_csi_release(struct inode * inode, struct file * file)
  109. {
  110. return 0;
  111. };
  112. static struct file_operations vvcam_csi_fops = {
  113. .owner = THIS_MODULE,
  114. .open = vvcam_csi_open,
  115. .release = vvcam_csi_release,
  116. .unlocked_ioctl = vvcam_csi_ioctl,
  117. };
  118. static int vvcam_csi_probe(struct platform_device *pdev)
  119. {
  120. int ret = 0;
  121. struct vvcam_csi_driver_dev *pdriver_dev;
  122. struct vvcam_csi_dev * pcsi_dev;
  123. pr_info("enter %s\n", __func__);
  124. if (pdev->id >= VIVCAM_CSI_MAXCNT)
  125. {
  126. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  127. return -EINVAL;
  128. }
  129. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_csi_driver_dev), GFP_KERNEL);
  130. if (pdriver_dev == NULL)
  131. {
  132. pr_err("%s:alloc struct vvcam_csi_driver_dev error\n", __func__);
  133. return -ENOMEM;
  134. }
  135. memset(pdriver_dev,0,sizeof(struct vvcam_csi_driver_dev ));
  136. pr_info("%s:csi[%d]: pdriver_dev =0x%px\n", __func__,pdev->id,pdriver_dev);
  137. pcsi_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_csi_dev), GFP_KERNEL);
  138. if (pcsi_dev == NULL)
  139. {
  140. pr_err("%s:alloc struct vvcam_csi_dev error\n", __func__);
  141. return -ENOMEM;
  142. }
  143. memset(pcsi_dev,0,sizeof(struct vvcam_csi_dev ));
  144. pr_info("%s:csi[%d]: pcsi_dev =0x%px\n", __func__,pdev->id,pcsi_dev);
  145. pcsi_dev->device_idx = pdev->id;
  146. //mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. //pcsi_dev->base = devm_ioremap_resource(&pdev->dev, mem);
  148. if (pcsi_dev->device_idx == 0)
  149. {
  150. pcsi_dev->base = ioremap(VVCSI0_BASE, VVCSI_SIZE);
  151. }else
  152. {
  153. pcsi_dev->base = ioremap(VVCSI1_BASE, VVCSI_SIZE);
  154. }
  155. if (IS_ERR(pcsi_dev->base))
  156. return PTR_ERR(pcsi_dev->base);
  157. pdriver_dev->private = pcsi_dev;
  158. mutex_init(&pdriver_dev->vvmutex);
  159. platform_set_drvdata(pdev, pdriver_dev);
  160. ret = vvnative_csi_module_init(pcsi_dev);
  161. if (ret != 0)
  162. {
  163. pr_err("%s:vvnative_csi_module_init error\n", __func__);
  164. return -ENOMEM;
  165. }
  166. if (devise_register_index == 0)
  167. {
  168. if (vvcam_csi_major == 0)
  169. {
  170. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, VIVCAM_CSI_MAXCNT, VIVCAM_CSI_NAME);
  171. if (ret != 0)
  172. {
  173. pr_err("%s:alloc_chrdev_region error\n", __func__);
  174. return ret;
  175. }
  176. vvcam_csi_major = MAJOR(pdriver_dev->devt);
  177. vvcam_csi_minor = MINOR(pdriver_dev->devt);
  178. }
  179. else
  180. {
  181. pdriver_dev->devt = MKDEV(vvcam_csi_major, vvcam_csi_minor);
  182. ret = register_chrdev_region(pdriver_dev->devt, VIVCAM_CSI_MAXCNT, VIVCAM_CSI_NAME);
  183. if (ret)
  184. {
  185. pr_err("%s:register_chrdev_region error\n", __func__);
  186. return ret;
  187. }
  188. }
  189. vvcam_csi_class = class_create(THIS_MODULE, VIVCAM_CSI_NAME);
  190. if (IS_ERR(vvcam_csi_class))
  191. {
  192. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  193. return -EINVAL;
  194. }
  195. }
  196. pdriver_dev->devt = MKDEV(vvcam_csi_major, vvcam_csi_minor + pdev->id);
  197. cdev_init(&pdriver_dev->cdev, &vvcam_csi_fops);
  198. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  199. if ( ret )
  200. {
  201. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  202. return ret;
  203. }
  204. pdriver_dev->class = vvcam_csi_class;
  205. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  206. pdriver_dev, "%s%d", VIVCAM_CSI_NAME, pdev->id);
  207. devise_register_index++;
  208. pr_info("exit %s\n", __func__);
  209. return ret;
  210. }
  211. static int vvcam_csi_remove(struct platform_device *pdev)
  212. {
  213. struct vvcam_csi_driver_dev *pdriver_dev;
  214. struct vvcam_csi_dev * pcsi_dev;
  215. pr_info("enter %s\n", __func__);
  216. devise_register_index--;
  217. pdriver_dev = platform_get_drvdata(pdev);
  218. pcsi_dev = pdriver_dev->private;
  219. iounmap(pcsi_dev->base);
  220. cdev_del(&pdriver_dev->cdev);
  221. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  222. unregister_chrdev_region(pdriver_dev->devt, VIVCAM_CSI_MAXCNT);
  223. if (devise_register_index == 0)
  224. {
  225. class_destroy(pdriver_dev->class);
  226. }
  227. return 0;
  228. }
  229. static struct platform_driver vvcam_csi_driver = {
  230. .probe = vvcam_csi_probe,
  231. .remove = vvcam_csi_remove,
  232. .driver = {
  233. .name = VIVCAM_CSI_NAME,
  234. .owner = THIS_MODULE,
  235. }
  236. };
  237. static void vvcam_csi_pdev_release(struct device *dev)
  238. {
  239. pr_info("enter %s\n", __func__);
  240. }
  241. #ifdef WITH_VVCAM
  242. static struct resource vvcam_csi0_resource[] = {
  243. [0] = {
  244. .start = VVCSI0_BASE,
  245. .end = VVCSI0_BASE + VVCSI_SIZE - 1,
  246. .flags = IORESOURCE_MEM,
  247. },
  248. };
  249. static struct platform_device vvcam_csi_pdev = {
  250. .name = VIVCAM_CSI_NAME,
  251. .id = 0,
  252. .resource = vvcam_csi0_resource,
  253. .num_resources = 0,
  254. .dev.release = vvcam_csi_pdev_release,
  255. };
  256. #endif
  257. #ifdef WITH_VVCAM_DUAL
  258. static struct resource vvcam_csi1_resource[] = {
  259. [0] = {
  260. .start = VVCSI1_BASE,
  261. .end = VVCSI1_BASE + VVCSI_SIZE - 1,
  262. .flags = IORESOURCE_MEM,
  263. },
  264. };
  265. static struct platform_device vvcam_csi_dual_pdev = {
  266. .name = VIVCAM_CSI_NAME,
  267. .id = 1,
  268. .resource = vvcam_csi1_resource,
  269. .num_resources = 0,
  270. .dev.release = vvcam_csi_pdev_release,
  271. };
  272. #endif
  273. static int __init vvcam_csi_init_module(void)
  274. {
  275. int ret = 0;
  276. pr_info("enter %s\n", __func__);
  277. #ifdef WITH_VVCAM
  278. ret = platform_device_register(&vvcam_csi_pdev);
  279. if (ret)
  280. {
  281. pr_err("register platform device failed.\n");
  282. return ret;
  283. }
  284. #endif
  285. #ifdef WITH_VVCAM_DUAL
  286. ret = platform_device_register(&vvcam_csi_dual_pdev);
  287. if (ret)
  288. {
  289. pr_err("register platform device failed.\n");
  290. return ret;
  291. }
  292. #endif
  293. ret = platform_driver_register(&vvcam_csi_driver);
  294. if (ret)
  295. {
  296. pr_err("register platform driver failed.\n");
  297. return ret;
  298. }
  299. return ret;
  300. }
  301. static void __exit vvcam_csi_exit_module(void)
  302. {
  303. pr_info("enter %s\n", __func__);
  304. platform_driver_unregister(&vvcam_csi_driver);
  305. #ifdef WITH_VVCAM
  306. platform_device_unregister(&vvcam_csi_pdev);
  307. #endif
  308. #ifdef WITH_VVCAM_DUAL
  309. platform_device_unregister(&vvcam_csi_dual_pdev);
  310. #endif
  311. }
  312. module_init(vvcam_csi_init_module);
  313. module_exit(vvcam_csi_exit_module);
  314. MODULE_DESCRIPTION("CSI");
  315. MODULE_LICENSE("GPL");