scale_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Arkmicro Scale 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/fs.h>
  13. #include <linux/io.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/wait.h>
  16. #include <linux/fb.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/delay.h>
  19. #include <linux/completion.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/poll.h>
  22. #include <linux/of_device.h>
  23. #include <linux/irq.h>
  24. #include "scale.h"
  25. struct ark_scale_context *scale_context = NULL;
  26. static int ark_scale_open(struct inode *inode, struct file *filp)
  27. {
  28. struct ark_scale_device *dev;
  29. struct ark_scale_context *context;
  30. dev = container_of(inode->i_cdev, struct ark_scale_device, cdev);
  31. context = &dev->context;
  32. filp->private_data = dev;
  33. return 0;
  34. }
  35. static int ark_scale_fasync(int fd, struct file *filp, int mode)
  36. {
  37. int ret;
  38. struct ark_scale_device *scale =
  39. (struct ark_scale_device *)filp->private_data;
  40. ret = fasync_helper(fd, filp, mode, &scale->async_queue_wb);
  41. return ret;
  42. }
  43. static int ark_scale_release(struct inode *inode, struct file *filp)
  44. {
  45. struct ark_scale_device *dev;
  46. dev = container_of(inode->i_cdev, struct ark_scale_device, cdev);
  47. if(filp->f_flags & FASYNC)
  48. {
  49. /* remove this filp from the asynchronusly notified filp's */
  50. ark_scale_fasync(-1, filp, 0);
  51. }
  52. return 0;
  53. }
  54. static long ark_scale_ioctl(struct file *filp,
  55. unsigned int cmd, unsigned long arg)
  56. {
  57. int err = 0;
  58. struct ark_scale_device *scale = (struct ark_scale_device *)filp->private_data;
  59. struct ark_scale_context *context = &scale->context;
  60. switch (cmd)
  61. {
  62. case SCALE_IOCTL_START:
  63. case SCALE_IOCTL_START_NO_WAIT:
  64. {
  65. struct ark_scale_param input_arg;
  66. if(copy_from_user(&input_arg, (void*)arg, sizeof(input_arg))) {
  67. printk(KERN_ERR "%s, SCALE_IOCTL_START copy_from_user failed\n", __FUNCTION__);
  68. return -EFAULT;
  69. }
  70. if(cmd == SCALE_IOCTL_START) {
  71. err = ark_scale_start(context, &input_arg);
  72. } else {
  73. err = ark_scale_start_nowait(context, &input_arg);
  74. }
  75. break;
  76. }
  77. //Old interface only for arkn141,you'd better not use it, please use SCALE_IOCTL_START above.
  78. case ARKN141_SCALE_IOCTL_START_OLD:
  79. {
  80. struct ark_scale_param input_arg;
  81. memset(&input_arg, 0, sizeof(input_arg));
  82. if(copy_from_user(&input_arg, (void*)arg, sizeof(struct ark_scale_param_old))) {
  83. printk("%s, copy_from_user failed\n", __FUNCTION__);
  84. return -EFAULT;
  85. }
  86. err = ark_scale_start(context, &input_arg);
  87. break;
  88. }
  89. case SCALE_IOCTL_WAIT_IDLE:
  90. {
  91. err = ark_scale_wait_idle(context);
  92. break;
  93. }
  94. case SCALE_IOCTL_GET_BUSY_SYATUS:
  95. {
  96. int busy = ark_scale_get_busy_status(context);
  97. if(copy_to_user((void*)arg, &busy, sizeof(int))) {
  98. printk(KERN_ERR "%s, SCALE_IOCTL_GET_BUSY_SYATUS copy_to_user failed\n", __FUNCTION__);
  99. return -EFAULT;
  100. }
  101. break;
  102. }
  103. default:
  104. printk("%s %d: undefined cmd (0x%2X)\n", __FUNCTION__, __LINE__, cmd);
  105. err = -EINVAL;
  106. break;
  107. }
  108. return err;
  109. }
  110. static struct file_operations ark_scale_fops = {
  111. .owner = THIS_MODULE,
  112. .open = ark_scale_open,
  113. .unlocked_ioctl = ark_scale_ioctl,
  114. .release = ark_scale_release,
  115. .fasync = ark_scale_fasync,
  116. };
  117. static const struct of_device_id axi_scale_of_match[] = {
  118. { .compatible = "arkmicro,arkn141-axi-scale", },
  119. { .compatible = "arkmicro,ark1668-axi-scale", },
  120. { .compatible = "arkmicro,ark1668e-axi-scale", },
  121. { }
  122. };
  123. MODULE_DEVICE_TABLE(of, axi_scale_of_match);
  124. /* This function is invoked when the device module is loaded into the
  125. * kernel. It allocates system resources for constructing driver control
  126. * data structures and initializes them accordingly.
  127. */
  128. static int ark_axi_scale_probe(struct platform_device *pdev)
  129. {
  130. struct ark_scale_device *scale;
  131. struct resource *res;
  132. const struct of_device_id *match = NULL;
  133. int int_trigger_type = 0;
  134. dev_t dev;
  135. void __iomem *regs;
  136. int error = 0;
  137. scale = devm_kzalloc(&pdev->dev, sizeof(struct ark_scale_device), GFP_KERNEL);
  138. if (scale == NULL) {
  139. dev_err(&pdev->dev, "%s %d: failed to allocate memory\n",
  140. __FUNCTION__, __LINE__);
  141. return -ENOMEM;
  142. }
  143. scale_context = &scale->context;
  144. sema_init(&scale_context->scale_sem, 1);
  145. match = of_match_device(axi_scale_of_match, &pdev->dev);
  146. if(!strcmp(match->compatible,axi_scale_of_match[SOC_TYPE_ARKN141].compatible)){
  147. //match arkn141
  148. scale->context.soc_type = SOC_TYPE_ARKN141;
  149. int_trigger_type = IRQF_SHARED;
  150. } else if(!strcmp(match->compatible,axi_scale_of_match[SOC_TYPE_ARK1668].compatible)) {
  151. //match ark1668
  152. scale->context.soc_type = SOC_TYPE_ARK1668;
  153. int_trigger_type = IRQF_TRIGGER_RISING;
  154. } else if(!strcmp(match->compatible,axi_scale_of_match[SOC_TYPE_ARK1668E].compatible)) {
  155. //match ark1668e
  156. scale->context.soc_type = SOC_TYPE_ARK1668E;
  157. int_trigger_type = IRQF_TRIGGER_RISING;
  158. } else {
  159. printk("%s, Invalid soc_type:%d\n", __FUNCTION__, scale->context.soc_type);
  160. return -EINVAL;
  161. }
  162. scale->driver_name = "ark_axi_scale_drv";
  163. scale->name = "ark_axi_scale";
  164. scale->major = 0; /* if 0, let system choose */
  165. scale->minor_start = 0;
  166. scale->minor_num = 1; /* one dev only */
  167. scale->num = 1;
  168. /* register char device */
  169. if (!scale->major) {
  170. error = alloc_chrdev_region(
  171. &dev,
  172. scale->minor_start,
  173. scale->num,
  174. scale->name
  175. );
  176. if (!error) {
  177. scale->major = MAJOR(dev);
  178. scale->minor_start = MINOR(dev);
  179. }
  180. } else {
  181. dev = MKDEV(scale->major, scale->minor_start);
  182. error = register_chrdev_region(dev, scale->num,
  183. (char *)scale->name);
  184. }
  185. if (error < 0) {
  186. dev_err(&pdev->dev, "%s %d: register driver error\n",
  187. __FUNCTION__, __LINE__);
  188. goto err_driver_register;
  189. }
  190. /* associate the file operations */
  191. cdev_init(&scale->cdev, &ark_scale_fops);
  192. scale->cdev.owner = THIS_MODULE; //driver->owner;
  193. scale->cdev.ops = &ark_scale_fops;
  194. error = cdev_add(&scale->cdev, dev, scale->num);
  195. if (error) {
  196. dev_err(&pdev->dev, "%s %d: cdev add error\n", __FUNCTION__, __LINE__);
  197. goto err_cdev_add;
  198. }
  199. scale->scale_class = class_create(THIS_MODULE, "scale_class");
  200. if(IS_ERR(scale->scale_class)) {
  201. dev_err(&pdev->dev, "Err: failed in creating ark scale class.\n");
  202. scale->scale_class = NULL;
  203. goto err_cdev_add;
  204. }
  205. scale->scale_device = device_create(scale->scale_class, NULL, dev, NULL, "axi_scale");
  206. if (IS_ERR(scale->scale_device)) {
  207. dev_err(&pdev->dev, "Err: failed in creating ark scale device.\n");
  208. scale->scale_device = NULL;
  209. goto err_cdev_add;
  210. }
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (IS_ERR(res)) {
  213. error = PTR_ERR(res);
  214. goto err_mem_res_req;
  215. }
  216. regs = devm_ioremap_resource(&pdev->dev, res);
  217. if (IS_ERR(regs)) {
  218. error = PTR_ERR(regs);
  219. goto err_mem_res_req;
  220. }
  221. scale->context.mmio_base = regs;
  222. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  223. if (IS_ERR(res)) {
  224. error = PTR_ERR(res);
  225. goto err_mem_res_req;
  226. }
  227. regs = ioremap(res->start, resource_size(res)); /* baseaddr conflict */
  228. if (IS_ERR(regs)) {
  229. error = PTR_ERR(regs);
  230. goto err_mem_res_req;
  231. }
  232. scale->context.sys_base = regs;
  233. scale->context.clk = devm_clk_get(&pdev->dev, NULL);
  234. if (IS_ERR(scale->context.clk)) {
  235. dev_err(&pdev->dev, "Err: failed in getting scale clock.\n");
  236. error = PTR_ERR(scale->context.clk);
  237. goto err_init;
  238. }
  239. if(of_property_read_u32(pdev->dev.of_node, "softreset-reg", &scale->context.softreset_reg)) {
  240. scale->context.softreset_reg = -1;
  241. }
  242. if(of_property_read_u32(pdev->dev.of_node, "softreset-offset", &scale->context.softreset_offset)) {
  243. scale->context.softreset_offset = -1;
  244. }
  245. /* initialize hardware and data structure */
  246. error = ark_scale_dev_init(&scale->context);
  247. if (error != 0) {
  248. dev_err(&pdev->dev, "%s %d: dev init err\n",
  249. __FUNCTION__, __LINE__);
  250. goto err_init;
  251. }
  252. scale->context.irq = platform_get_irq(pdev, 0);
  253. if (scale->context.irq < 0) {
  254. dev_err(&pdev->dev, "%s %d: can't get irq resource.\n", __FUNCTION__, __LINE__);
  255. goto err_irq;
  256. }
  257. error = devm_request_irq(
  258. &pdev->dev,
  259. scale->context.irq,
  260. ark_scale_intr_handler,
  261. int_trigger_type,//IRQF_SHARED, //SA_SHIRQ,
  262. "axi_scale",
  263. scale
  264. );
  265. if (error) {
  266. dev_err(&pdev->dev, "%s %d: can't get assigned irq %d, error %d\n",
  267. __FUNCTION__, __LINE__, scale->context.irq, error);
  268. goto err_irq;
  269. }
  270. scale->context.dev = &pdev->dev;
  271. platform_set_drvdata(pdev, scale);
  272. return 0;
  273. err_irq:
  274. err_init:
  275. iounmap(scale->context.sys_base);
  276. err_mem_res_req:
  277. cdev_del(&scale->cdev);
  278. err_cdev_add:
  279. if (scale->scale_class) {
  280. if (scale->scale_device) {
  281. device_destroy(scale->scale_class, dev);
  282. }
  283. class_destroy(scale->scale_class);
  284. }
  285. unregister_chrdev_region(dev, scale->num);
  286. err_driver_register:
  287. return error;
  288. }
  289. /* This function is invoked when the device module is removed from the
  290. * kernel. It releases all resources to the system.
  291. */
  292. static int ark_axi_scale_remove(struct platform_device *pdev)
  293. {
  294. struct ark_scale_device *scale;
  295. dev_t dev;
  296. scale = platform_get_drvdata(pdev);
  297. if (scale == NULL)
  298. return -ENODEV;
  299. dev = MKDEV(scale->major, scale->minor_start);
  300. cdev_del(&scale->cdev);
  301. if (scale->scale_class) {
  302. if (scale->scale_device) {
  303. device_destroy(scale->scale_class, dev);
  304. }
  305. class_destroy(scale->scale_class);
  306. }
  307. unregister_chrdev_region(dev, scale->num);
  308. return 0;
  309. }
  310. static struct platform_driver ark_axi_scale_driver = {
  311. .driver = {
  312. .name = "axi_scale",
  313. .of_match_table = of_match_ptr(axi_scale_of_match),
  314. },
  315. .probe = ark_axi_scale_probe,
  316. .remove = ark_axi_scale_remove,
  317. };
  318. module_platform_driver(ark_axi_scale_driver);
  319. MODULE_AUTHOR("Sim");
  320. MODULE_DESCRIPTION("ArkMicro Scaler Driver");
  321. MODULE_LICENSE("GPL v2");