Firefly开源社区

之前实现的一个adc驱动,贴一下

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-6-16 14:06:43     
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/delay.h>
  5. #include <linux/gpio.h>
  6. #include <linux/fs.h>
  7. #include <linux/cdev.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/interrupt.h>
  11. #include <asm/irq.h>
  12. #include <linux/iio/consumer.h>

  13. #ifdef CONFIG_OF
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/of_platform.h>
  17. #endif


  18. #define DESC(x) 1

  19. #if DESC("global area")

  20. static int global_major;
  21. static struct class *adc0_class;
  22. static struct device *adc0_device;
  23. static struct device *adc0_dev = NULL;

  24. #endif
  25. static int adc0_probe(struct platform_device *pdev)
  26. {
  27.         /*Get io channel*/
  28.         adc0_dev = &pdev->dev;
  29.         printk("%s \n", __FUNCTION__);
  30.        
  31.         return 0;
  32.        
  33. }

  34. static int adc0_remove(struct platform_device *pdev)
  35. {
  36.         return 0;
  37. }

  38. static const struct of_device_id of_adc0_match[] = {
  39.         { .compatible = "sunrex,adc_test" },
  40.         { /* Sentinel */ }
  41. };

  42. static struct platform_driver adc0_driver = {
  43.         .probe = adc0_probe,
  44.         .remove = adc0_remove,
  45.         .driver = {
  46.                 .owner = THIS_MODULE,
  47.                 .name  = "adc0",
  48. #ifdef CONFIG_OF
  49.                 .of_match_table  = of_adc0_match,
  50. #endif
  51.         },
  52. };

  53. static ssize_t adc0_read(struct file *filep, char __user *usrbuf, size_t size, loff_t *offset)
  54. {
  55.        
  56.         int val,ret;
  57.         struct iio_channel *chan; //定义 IIO 通道结构体

  58.         printk("%s \n", __FUNCTION__);
  59.         chan = iio_channel_get(adc0_dev, NULL); //获取 IIO 通道结构体
  60.         ret = iio_read_channel_raw(chan, &val);

  61.         printk("adc0 value = %u \n", val);
  62.         if (ret < 0)
  63.         {
  64.                 printk("Read channel failed. \n");
  65.                 return EIO;
  66.         }
  67.         copy_to_user(usrbuf, (void *)&val, size);

  68.          return 0;
  69. }
  70. static ssize_t adc0_write(struct file *filep, const char __user *usrbuf, size_t size, loff_t *offset)
  71. {
  72.         return 0;
  73. }

  74. static int adc0_open(struct inode *inode, struct file *filep)
  75. {
  76.         return 0;
  77. }
  78. static int adc0_release(struct inode *inode, struct file *filep)
  79. {

  80.         return 0;
  81. }


  82. static struct file_operations motor_fops = {
  83.         .owner =   THIS_MODULE,
  84.         .read  =   adc0_read,
  85.         .write =   adc0_write,
  86.         .open  =   adc0_open,
  87.         .release = adc0_release,
  88. };

  89. static int __init adc0_init(void)
  90. {
  91.         /*system will auto allocate major*/
  92.         global_major = register_chrdev(0, "adc0", &motor_fops);

  93.         /*create class*/
  94.         adc0_class = class_create(THIS_MODULE, "adc0_drv");

  95.         /*create device for app*/
  96.         adc0_device = device_create(adc0_class, NULL, MKDEV(global_major, 0), NULL, "adc0");
  97.        

  98.         //if (ret < 0)
  99.         //        return ret;
  100.         platform_driver_register(&adc0_driver);
  101.         return 0;
  102. }

  103. static void __exit adc0_exit(void)
  104. {
  105.         platform_driver_unregister(&adc0_driver);
  106.         device_destroy(adc0_class,MKDEV(global_major, 0));
  107.         class_destroy(adc0_class);
  108.         //unregister_chrdev_region(MKDEV(global_major, 0), 1);
  109.         unregister_chrdev(global_major, "adc0");
  110. }

  111. module_init(adc0_init);
  112. module_exit(adc0_exit);
  113. MODULE_LICENSE("GPL");
  114. MODULE_AUTHOR("zhang jiawei");
复制代码
回复

使用道具 举报

116

积分

0

威望

0

贡献

技术小白

积分
116
发表于 2017-6-23 11:13:29     
正在看这方面的内容,先收藏!谢谢!
回复

使用道具 举报

57

积分

0

威望

0

贡献

技术小白

积分
57
发表于 2017-7-14 10:44:30     
ADC   MARK
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表