scale_drv.c 8.7 KB

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