bm_isp_driver.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <asm/io.h>
  10. #include <linux/cdev.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/mm.h>
  17. #include <linux/timer.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/poll.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/of.h>
  30. #include "bm_printk.h"
  31. #include "bm_isp_ioctl.h"
  32. #define BM_DRIVER_NAME "bm_isp"
  33. #define BM_DRIVER_MAXCNT 3
  34. struct bm_isp_drvdata {
  35. struct cdev cdev;
  36. dev_t devt;
  37. struct class *class;
  38. struct mutex mutex;
  39. unsigned int device_idx;
  40. void __iomem *base;
  41. void __iomem *reset;
  42. //int irq_num;
  43. void *private; // can be bm_isp_drvdata_private, but not use now
  44. };
  45. struct bm_isp_drvdata_private {
  46. int private_tmp;
  47. };
  48. static struct class *bm_driver_class;
  49. static unsigned int bm_driver_major = 0;
  50. static unsigned int bm_driver_minor = 0;
  51. static unsigned int device_register_index = 0;
  52. #define check_retval(x)\
  53. do {\
  54. if ((x))\
  55. return -EIO;\
  56. } while (0)
  57. static unsigned int bm_isp_poll(struct file * filp, poll_table *wait)
  58. {
  59. return 0;
  60. }
  61. void bm_isp_work(struct work_struct *work)
  62. {
  63. }
  64. irqreturn_t bm_isp_irq(int irq, void *dev_id)
  65. {
  66. bm_info("enter %s\n", __func__);
  67. return IRQ_HANDLED;
  68. }
  69. static int bm_isp_open(struct inode * inode, struct file * file)
  70. {
  71. struct bm_isp_drvdata *drvdata;
  72. bm_info("enter %s\n", __func__);
  73. drvdata = container_of(inode->i_cdev, struct bm_isp_drvdata, cdev);
  74. file->private_data = drvdata;
  75. return 0;
  76. };
  77. static int bm_isp_write_reg(struct bm_isp_drvdata *drvdata, void *__user args)
  78. {
  79. struct bm_isp_reg_t reg;
  80. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  81. writel(reg.value, drvdata->base + reg.offset);
  82. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, reg.offset, reg.value);
  83. return 0;
  84. }
  85. static int bm_isp_read_reg(struct bm_isp_drvdata *drvdata, void *__user args)
  86. {
  87. struct bm_isp_reg_t reg;
  88. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  89. reg.value = readl(drvdata->base + reg.offset);
  90. check_retval(copy_to_user(args, &reg, sizeof(reg)));
  91. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, reg.offset, reg.value);
  92. return 0;
  93. }
  94. static long bm_isp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  95. {
  96. long ret = 0;
  97. struct bm_isp_drvdata *drvdata;
  98. bm_info("enter %s\n", __func__);
  99. drvdata = file->private_data;
  100. if (drvdata == NULL) {
  101. bm_err("%s:file private is null point error\n", __func__);
  102. return -ENOMEM;
  103. }
  104. mutex_lock(&drvdata->mutex);
  105. switch (cmd) {
  106. case BMISPIOC_WRITE_REG:
  107. ret = bm_isp_write_reg(drvdata, (void *)arg);
  108. break;
  109. case BMISPIOC_READ_REG:
  110. ret = bm_isp_read_reg(drvdata, (void *)arg);
  111. break;
  112. default:
  113. ret = -EPERM;
  114. bm_err("%s: unsupported command %d", __func__, cmd);
  115. break;
  116. }
  117. mutex_unlock(&drvdata->mutex);
  118. return ret;
  119. };
  120. static int bm_isp_release(struct inode * inode, struct file * file)
  121. {
  122. bm_info("enter %s\n", __func__);
  123. return 0;
  124. };
  125. static int bm_isp_mmap(struct file *pFile, struct vm_area_struct *vma)
  126. {
  127. bm_info("enter %s\n", __func__);
  128. return 0;
  129. };
  130. struct file_operations bm_isp_fops = {
  131. .owner = THIS_MODULE,
  132. .open = bm_isp_open,
  133. .release = bm_isp_release,
  134. .unlocked_ioctl = bm_isp_ioctl,
  135. .mmap = bm_isp_mmap,
  136. .poll = bm_isp_poll,
  137. };
  138. static int bm_isp_probe(struct platform_device *pdev)
  139. {
  140. int ret = 0;
  141. struct bm_isp_drvdata *drvdata;
  142. struct resource *iores_mem;
  143. u32 value;
  144. bm_info("enter %s\n", __func__);
  145. pdev->id = device_register_index;
  146. if (pdev->id >= BM_DRIVER_MAXCNT) {
  147. bm_err("%s:pdev id is %d error\n", __func__, pdev->id);
  148. return -EINVAL;
  149. }
  150. drvdata = devm_kzalloc(&pdev->dev,sizeof(struct bm_isp_drvdata), GFP_KERNEL);
  151. if (drvdata == NULL) {
  152. bm_err("%s:alloc struct drvdata error\n", __func__);
  153. return -ENOMEM;
  154. }
  155. iores_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. drvdata->base = devm_ioremap_resource(&pdev->dev, iores_mem);
  157. bm_info("%s: [%s%d]: drvdata->base=0x%px, phy_addr base=0x%llx\n", __func__,
  158. BM_DRIVER_NAME, pdev->id, drvdata->base, iores_mem->start);
  159. drvdata->reset = NULL;
  160. drvdata->device_idx = pdev->id;
  161. mutex_init(&drvdata->mutex);
  162. //drvdata->irq_num = platform_get_irq(pdev, 0);
  163. //bm_info("%s:[%s%d]: pdriver_dev->irq_num=%d\n", __func__,
  164. // BM_ISP_NAME, pdev->id, drvdata->irq_num);
  165. platform_set_drvdata(pdev, drvdata);
  166. if (pdev->id == 0) {
  167. if (bm_driver_major == 0) {
  168. ret = alloc_chrdev_region(&drvdata->devt, 0, BM_DRIVER_MAXCNT, BM_DRIVER_NAME);
  169. if (ret != 0) {
  170. bm_err("%s:alloc_chrdev_region error\n", __func__);
  171. return ret;
  172. }
  173. bm_driver_major = MAJOR(drvdata->devt);
  174. bm_driver_minor = MINOR(drvdata->devt);
  175. } else {
  176. drvdata->devt = MKDEV(bm_driver_major, bm_driver_minor);
  177. ret = register_chrdev_region(drvdata->devt, BM_DRIVER_MAXCNT, BM_DRIVER_NAME);
  178. if (ret) {
  179. bm_err("%s:register_chrdev_region error\n", __func__);
  180. return ret;
  181. }
  182. }
  183. bm_driver_class = class_create(THIS_MODULE, BM_DRIVER_NAME);
  184. if (IS_ERR(bm_driver_class)) {
  185. bm_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  186. return -EINVAL;
  187. }
  188. }
  189. drvdata->devt = MKDEV(bm_driver_major, bm_driver_minor + pdev->id);
  190. cdev_init(&drvdata->cdev, &bm_isp_fops);
  191. ret = cdev_add(&drvdata->cdev, drvdata->devt, 1);
  192. if ( ret ) {
  193. bm_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  194. return ret;
  195. } else {
  196. bm_info("%s[%d]:cdev_add OK, major=%d, minor=%d\n", __func__, __LINE__,
  197. bm_driver_major, bm_driver_minor + pdev->id);
  198. }
  199. drvdata->class = bm_driver_class;
  200. device_create(drvdata->class, NULL, drvdata->devt,
  201. drvdata, "%s%d", BM_DRIVER_NAME, pdev->id);
  202. value = readl(drvdata->base+0x4);
  203. bm_info("offset=04, value is:0x%08x\n", value);
  204. device_register_index++;
  205. bm_info("exit %s:[%s%d]\n", __func__, BM_DRIVER_NAME, pdev->id);
  206. return 0;
  207. //err_register:
  208. // When error occore, should free memory, destory device, unregister ...
  209. // but now, ignore it
  210. // return ret;
  211. }
  212. static int bm_isp_remove(struct platform_device *pdev)
  213. {
  214. struct bm_isp_drvdata *drvdata;
  215. bm_info("enter %s\n", __func__);
  216. device_register_index--;
  217. drvdata = platform_get_drvdata(pdev);
  218. //free_irq(drvdata->irq_num, drvdata);
  219. cdev_del(&drvdata->cdev);
  220. device_destroy(drvdata->class, drvdata->devt);
  221. unregister_chrdev_region(drvdata->devt, BM_DRIVER_MAXCNT);
  222. mutex_destroy(&drvdata->mutex);
  223. if (device_register_index == 0) {
  224. class_destroy(drvdata->class);
  225. }
  226. devm_kfree(&pdev->dev, drvdata);
  227. bm_info("exit %s\n", __func__);
  228. return 0;
  229. }
  230. static const struct of_device_id bm_isp_of_match[] = {
  231. { .compatible = "thead,light-bm-isp", },
  232. { /* sentinel */ },
  233. };
  234. MODULE_DEVICE_TABLE(of, bm_isp_of_match);
  235. static struct platform_driver bm_isp_driver = {
  236. .probe = bm_isp_probe,
  237. .remove = bm_isp_remove,
  238. .driver = {
  239. .name = BM_DRIVER_NAME,
  240. .owner = THIS_MODULE,
  241. .of_match_table = of_match_ptr(bm_isp_of_match),
  242. }
  243. };
  244. static int __init bm_isp_init_module(void)
  245. {
  246. int ret = 0;
  247. bm_info("enter %s\n", __func__);
  248. ret = platform_driver_register(&bm_isp_driver);
  249. if (ret) {
  250. bm_err("register platform driver failed.\n");
  251. return ret;
  252. }
  253. return ret;
  254. }
  255. static void __exit bm_isp_exit_module(void)
  256. {
  257. bm_info("enter %s\n", __func__);
  258. platform_driver_unregister(&bm_isp_driver);
  259. }
  260. module_init(bm_isp_init_module);
  261. module_exit(bm_isp_exit_module);
  262. MODULE_AUTHOR("Lu Chongzhi");
  263. MODULE_DESCRIPTION("BAREMETAL-ISP");
  264. MODULE_LICENSE("GPL");