ark_camera_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Arkmicro ISP Camera driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/version.h>
  8. #include <linux/kernel.h>
  9. #include <linux/kthread.h>
  10. #include <linux/cdev.h>
  11. #include <linux/device.h>
  12. #include <linux/of.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/fs.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/wait.h>
  18. #include <linux/fb.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/delay.h>
  21. #include <linux/completion.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/poll.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/clk.h>
  26. #include "ark_camera.h"
  27. #include "Gem_isp.h"
  28. #include "Gem_isp_sys.h"
  29. static struct ark_camera_device *ark_camera = NULL;
  30. unsigned long isp_get_clk(void)
  31. {
  32. if(ark_camera)
  33. return clk_get_rate(ark_camera->isp_clk);
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(isp_get_clk);
  37. unsigned long isp_get_sensor_mclk(void)
  38. {
  39. if(ark_camera)
  40. return clk_get_rate(ark_camera->sensor_mclk);
  41. return 0;
  42. }
  43. EXPORT_SYMBOL(isp_get_sensor_mclk);
  44. static int ark_camera_open(struct inode *inode, struct file *filp)
  45. {
  46. struct ark_camera_device *dev;
  47. struct ark_camera_context *context;
  48. dev = container_of(inode->i_cdev, struct ark_camera_device, cdev);
  49. context = &dev->context;
  50. filp->private_data = dev;
  51. return 0;
  52. }
  53. static int ark_camera_fasync(int fd, struct file *filp, int mode)
  54. {
  55. int ret;
  56. struct ark_camera_device *camera =
  57. (struct ark_camera_device *)filp->private_data;
  58. ret = fasync_helper(fd, filp, mode, &camera->async_queue_cam);
  59. return ret;
  60. }
  61. static int ark_camera_release(struct inode *inode, struct file *filp)
  62. {
  63. struct ark_camera_device *dev;
  64. dev = container_of(inode->i_cdev, struct ark_camera_device, cdev);
  65. if(filp->f_flags & FASYNC)
  66. {
  67. /* remove this filp from the asynchronusly notified filp's */
  68. ark_camera_fasync(-1, filp, 0);
  69. }
  70. return 0;
  71. }
  72. static int ark_camera_read(struct file *filp, char __user *user, size_t size,loff_t *ppos)
  73. {
  74. int count;
  75. struct ark_camera_device *camera = (struct ark_camera_device *)filp->private_data;
  76. if (size > ISP_FRAME_NUM)
  77. return -EINVAL;
  78. wait_event_interruptible(camera->context.frame_finish_waitq, camera->context.frame_finish_count > 0);
  79. spin_lock_irq(&camera->context.lock);
  80. count = min(size, (size_t)camera->context.frame_finish_count);
  81. if (copy_to_user(user, &camera->context.frame_finish, count)) {
  82. printk("%s %d: copy_to_user error\n",
  83. __FUNCTION__, __LINE__);
  84. return -EFAULT;
  85. }
  86. camera->context.frame_finish_count -= count;
  87. if (camera->context.frame_finish_count > 0) {
  88. int i;
  89. char tmp;
  90. for (i = 0; i < camera->context.frame_finish_count; i++) {
  91. tmp = camera->context.frame_finish[count + i];
  92. camera->context.frame_finish[i] = tmp;
  93. }
  94. }
  95. spin_unlock_irq(&camera->context.lock);
  96. return count;
  97. }
  98. static unsigned int ark_camera_poll(struct file *filp, poll_table *wait)
  99. {
  100. struct ark_camera_device *camera = (struct ark_camera_device *)filp->private_data;
  101. unsigned int mask = 0;
  102. poll_wait(filp, &camera->context.frame_finish_waitq, wait);
  103. spin_lock_irq(&camera->context.lock);
  104. if (camera->context.frame_finish_count > 0)
  105. mask |= POLLIN | POLLRDNORM;
  106. spin_unlock_irq(&camera->context.lock);
  107. return mask;
  108. }
  109. extern isp_param_t g_isp_param;
  110. static long ark_camera_ioctl(struct file *filp,
  111. unsigned int cmd, unsigned long arg)
  112. {
  113. /* struct ark_camera_device *camera =
  114. (struct ark_camera_device *)filp->private_data; */
  115. switch (cmd)
  116. {
  117. case ARK_CAMERA_IOCTL_START:
  118. isp_enable();
  119. break;
  120. case ARK_CAMERA_IOCTL_STOP:
  121. isp_disable();
  122. break;
  123. case ARK_CAMERA_IOCTL_GET_YUV_BUFFER:
  124. if(copy_to_user((void*)arg, g_isp_param.y_addr, sizeof(g_isp_param.y_addr)))
  125. return -EFAULT;
  126. break;
  127. case ARK_CAMERA_IOCTL_SET_FRAME_READY:
  128. {
  129. int frame_id;
  130. if(copy_from_user(&frame_id, (void*)arg, sizeof(frame_id)))
  131. return -EFAULT;
  132. isp_sys_set_frame_ready(frame_id);
  133. }
  134. break;
  135. case ARK_CAMERA_IOCTL_GET_FRAME_SIZE:
  136. {
  137. unsigned int frame_size = 0;
  138. frame_size = (IMAGE_V_SZ << 16) | IMAGE_H_SZ;
  139. if(copy_to_user((void*)arg, &frame_size, sizeof(frame_size)))
  140. return -EFAULT;
  141. }
  142. break;
  143. case ARK_CAMERA_IOCTL_GET_FRAME_FORMAT:
  144. {
  145. unsigned int format = isp_get_video_format();
  146. if(copy_to_user((void*)arg, &format, sizeof(format)))
  147. return -EFAULT;
  148. }
  149. break;
  150. case ARK_CAMERA_IOCTL_GET_FPS:
  151. {
  152. unsigned int fps = isp_get_sensor_fps();
  153. if(copy_to_user((void*)arg, &fps, sizeof(fps)))
  154. return -EFAULT;
  155. }
  156. break;
  157. default:
  158. printk("%s %d: undefined cmd (0x%2X)\n",
  159. __FUNCTION__, __LINE__, cmd);
  160. return -EINVAL;
  161. }
  162. return 0;
  163. }
  164. static struct file_operations ark_camera_fops = {
  165. .owner = THIS_MODULE,
  166. .open = ark_camera_open,
  167. .unlocked_ioctl = ark_camera_ioctl,
  168. .release = ark_camera_release,
  169. .fasync = ark_camera_fasync,
  170. .read = ark_camera_read,
  171. .poll = ark_camera_poll,
  172. };
  173. /* This function is invoked when the device module is loaded into the
  174. * kernel. It allocates system resources for constructing driver control
  175. * data structures and initializes them accordingly.
  176. */
  177. static int ark_camera_probe(struct platform_device *pdev)
  178. {
  179. struct ark_camera_device *camera;
  180. dev_t dev;
  181. struct resource *res;
  182. void __iomem *regs;
  183. int error = 0;
  184. camera = devm_kzalloc(&pdev->dev, sizeof(struct ark_camera_device), GFP_KERNEL);
  185. if (camera == NULL) {
  186. printk(KERN_ERR "%s %d: failed to allocate memory\n",
  187. __FUNCTION__, __LINE__);
  188. return -ENOMEM;
  189. }
  190. camera->context.dev = &pdev->dev;
  191. camera->context.sensor_reset = devm_gpiod_get(&pdev->dev, "sensor-reset", GPIOD_OUT_LOW);
  192. if (IS_ERR(camera->context.sensor_reset))
  193. return PTR_ERR(camera->context.sensor_reset);
  194. //camera->context.sensor_standby = devm_gpiod_get(&pdev->dev, "sensor-standby", GPIOD_OUT_HIGH);
  195. //if (IS_ERR(camera->context.sensor_standby))
  196. // return PTR_ERR(camera->context.sensor_standby);
  197. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  198. if (IS_ERR(res))
  199. return PTR_ERR(res);
  200. regs = devm_ioremap_resource(&pdev->dev, res);
  201. if (IS_ERR(regs))
  202. return PTR_ERR(regs);
  203. camera->context.base = regs;
  204. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  205. if (IS_ERR(res))
  206. return PTR_ERR(res);
  207. regs = ioremap(res->start, resource_size(res));
  208. if (IS_ERR(regs))
  209. return PTR_ERR(regs);
  210. camera->context.scalebase = regs;
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  212. if (IS_ERR(res)) {
  213. error = PTR_ERR(res);
  214. goto err_sysmem_res_req;
  215. }
  216. regs = ioremap(res->start, resource_size(res)); /* baseaddr conflict */
  217. if (IS_ERR(regs)) {
  218. error = PTR_ERR(regs);
  219. goto err_sysmem_res_req;
  220. }
  221. camera->isp_clk = devm_clk_get(&pdev->dev, "isp-clk");
  222. if(IS_ERR(camera->isp_clk)) {
  223. error = PTR_ERR(camera->isp_clk);
  224. camera->isp_clk = NULL;
  225. goto err_sysmem_res_req;
  226. }
  227. camera->sensor_mclk = devm_clk_get(&pdev->dev, "sensor-mclk");
  228. if(IS_ERR(camera->sensor_mclk)) {
  229. error = PTR_ERR(camera->sensor_mclk);
  230. camera->sensor_mclk = NULL;
  231. goto err_sysmem_res_req;
  232. }
  233. camera->context.sysbase = regs;
  234. camera->driver_name = "ark_camera_drv";
  235. camera->name = "ark_camera";
  236. camera->major = 0; /* if 0, let system choose */
  237. camera->minor_start = 0;
  238. camera->minor_num = 1; /* one dev only */
  239. camera->num = 1;
  240. ark_camera = camera;
  241. /* register char device */
  242. if (!camera->major) {
  243. error = alloc_chrdev_region(
  244. &dev,
  245. camera->minor_start,
  246. camera->num,
  247. camera->name
  248. );
  249. if (!error) {
  250. camera->major = MAJOR(dev);
  251. camera->minor_start = MINOR(dev);
  252. //printk(KERN_ERR "%s %d: allocate device major=%d minor=%d\n",
  253. // __FUNCTION__, __LINE__,
  254. // camera->major, camera->minor_start);
  255. }
  256. } else {
  257. dev = MKDEV(camera->major, camera->minor_start);
  258. // printk(KERN_ERR "%s %d: dev %d\n", __FUNCTION__, __LINE__, dev);
  259. error = register_chrdev_region(dev, camera->num,
  260. (char *)camera->name);
  261. }
  262. if (error < 0) {
  263. printk(KERN_ERR "%s %d: register driver error\n",
  264. __FUNCTION__, __LINE__);
  265. goto err_driver_register;
  266. }
  267. /* associate the file operations */
  268. cdev_init(&camera->cdev, &ark_camera_fops);
  269. camera->cdev.owner = THIS_MODULE; //driver->owner;
  270. camera->cdev.ops = &ark_camera_fops;
  271. error = cdev_add(&camera->cdev, dev, camera->num);
  272. if (error) {
  273. printk(KERN_ERR "%s %d: cdev add error\n", __FUNCTION__, __LINE__);
  274. goto err_cdev_add;
  275. }
  276. //printk(KERN_ERR "%s %d: cdev made, name: %s, major: %d, minor: %d \n",
  277. // __FUNCTION__, __LINE__, camera->name, camera->major,
  278. // camera->minor_start);
  279. camera->camera_class = class_create(THIS_MODULE, "camera_class");
  280. if(IS_ERR(camera->camera_class)) {
  281. printk(KERN_ERR "Err: failed in creating ark camera class.\n");
  282. camera->camera_class = NULL;
  283. goto err_cdev_add;
  284. }
  285. camera->camera_device = device_create(camera->camera_class, NULL, dev, NULL, "camera");
  286. if (IS_ERR(camera->camera_device)) {
  287. printk(KERN_ERR "Err: failed in creating ark camera device.\n");
  288. camera->camera_device = NULL;
  289. goto err_cdev_add;
  290. }
  291. /* initialize hardware and data structure */
  292. error = ark_camera_dev_init(camera);
  293. if (error != 0) {
  294. printk(KERN_ERR "%s %d: dev init err\n",
  295. __FUNCTION__, __LINE__);
  296. goto err_init;
  297. }
  298. //printk(KERN_ERR "%s %d: dev init done\n", __FUNCTION__, __LINE__);
  299. camera->context.irq = platform_get_irq(pdev, 0);
  300. if (camera->context.irq < 0) {
  301. printk(KERN_ERR "%s %d: can't get irq resource.\n", __FUNCTION__, __LINE__);
  302. goto err_irq;
  303. }
  304. error = devm_request_irq(
  305. &pdev->dev,
  306. camera->context.irq,
  307. ark_camera_intr_handler,
  308. IRQF_SHARED, //SA_SHIRQ,
  309. "camera",
  310. camera
  311. );
  312. if (!error) {
  313. //printk(KERN_ERR "%s %d: succeed to get assigned irq %d\n",
  314. // __FUNCTION__, __LINE__, camera->context.irq);
  315. } else {
  316. printk(KERN_ERR "%s %d: can't get assigned irq %d, error %d\n",
  317. __FUNCTION__, __LINE__, camera->context.irq, error);
  318. }
  319. return 0;
  320. err_irq:
  321. err_init:
  322. cdev_del(&camera->cdev);
  323. err_cdev_add:
  324. if (camera->camera_class) {
  325. if (camera->camera_device) {
  326. device_destroy(camera->camera_class, dev);
  327. }
  328. class_destroy(camera->camera_class);
  329. }
  330. unregister_chrdev_region(dev, camera->num);
  331. err_driver_register:
  332. iounmap(camera->context.sysbase);
  333. err_sysmem_res_req:
  334. iounmap(camera->context.scalebase);
  335. ark_camera = NULL;
  336. return error;
  337. }
  338. /* This function is invoked when the device module is removed from the
  339. * kernel. It releases all resources to the system.
  340. */
  341. static int ark_camera_remove(struct platform_device *pdev)
  342. {
  343. struct ark_camera_device *camera;
  344. dev_t dev;
  345. camera = platform_get_drvdata(pdev);
  346. if (camera == NULL)
  347. return -ENODEV;
  348. dev = MKDEV(camera->major, camera->minor_start);
  349. if(camera->context.camera_queue)
  350. destroy_workqueue(camera->context.camera_queue);
  351. cdev_del(&camera->cdev);
  352. unregister_chrdev_region(dev, camera->num);
  353. iounmap(camera->context.sysbase);
  354. iounmap(camera->context.scalebase);
  355. ark_camera = NULL;
  356. return 0;
  357. }
  358. static const struct of_device_id camera_of_match[] = {
  359. { .compatible = "arkmicro,isp-camera", },
  360. { }
  361. };
  362. MODULE_DEVICE_TABLE(of, camera_of_match);
  363. static struct platform_driver ark_camera_driver = {
  364. .driver = {
  365. .name = "ark_camera",
  366. .owner = THIS_MODULE,
  367. .of_match_table = of_match_ptr(camera_of_match),
  368. },
  369. .probe = ark_camera_probe,
  370. .remove = ark_camera_remove,
  371. };
  372. module_platform_driver(ark_camera_driver);
  373. MODULE_AUTHOR("Sim");
  374. MODULE_DESCRIPTION("ArkMicro Camera Driver");
  375. MODULE_LICENSE("GPL v2");