[DemoCode下载] N32905 adc字符设备驱动(转)

[复制链接]
1561|8
 楼主| 598330983 发表于 2017-5-28 22:42 | 显示全部楼层 |阅读模式
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/input.h>
  6. #include <linux/init.h>
  7. #include <linux/serio.h>
  8. #include <linux/delay.h>
  9. #include <linux/clk.h>
  10. #include <linux/wait.h>
  11. #include <linux/sched.h>
  12. #include <asm/io.h>
  13. #include <asm/irq.h>
  14. #include <asm/uaccess.h>
  15. #include <linux/cdev.h>
  16. #include <linux/miscdevice.h>
  17. #include <mach/hardware.h>
  18. #include <mach/irqs.h>
  19. #include <mach/w55fa93_reg.h>
  20. #include <linux/input.h>
  21. #include <linux/clk.h>

  22. #define DEVICE_NAME "adc"
  23. #define outp32(addr, value)     writel(value, addr)
  24. #define inp32(addr)             readl(addr)


  25. static void __iomem *base_addr;

  26. #define ADC_INPUT(x)        ((x) << 9)


  27. #define SET_NORMAL_AIN2_WI  do{\
  28.                                 outl( (ADC_CON_ADC_EN | ADC_CONV | ADC_INT | (2<<9)), \
  29.                                 REG_ADC_CON);\
  30.                             }while(0)

  31. #define SET_NORMAL_AIN2_WI_WR \
  32.                             while(  (inl(REG_ADC_CON) & ADC_INT) != ADC_INT )


  33. #define SET_NORMAL_AIN0_WI  do{\
  34.                                 outl( (ADC_CON_ADC_EN | ADC_CONV | ADC_INT | (0<<9)), \
  35.                                 REG_ADC_CON);\
  36.                             }while(0)

  37. #define SET_NORMAL_AIN0_WI_WR \
  38.                             while(  (inl(REG_ADC_CON) & ADC_INT) != ADC_INT )


  39. static ssize_t w55fa93_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
  40. {
  41.     unsigned int adc_data = -1;

  42.     SET_NORMAL_AIN2_WI;
  43.     SET_NORMAL_AIN2_WI_WR;

  44.     adc_data = inl(REG_ADC_XDATA);

  45. //    printk("adc_data=%d\n", adc_data);
  46.     if (adc_data != -1)
  47.     {
  48.         copy_to_user(buffer, &adc_data, sizeof(adc_data));
  49.     }
  50.     return 0;
  51. }

  52. static int w55fa93_adc_open(struct inode *inode, struct file *filp)
  53. {
  54.     printk( "adc opened\n");
  55.     outp32(REG_APBIPRST, inp32(REG_APBIPRST) | ADCRST);
  56.     outp32(REG_APBIPRST,  inp32(REG_APBIPRST) & ~ADCRST);
  57.     return 0;
  58. }

  59. static int w55fa93_adc_release(struct inode *inode, struct file *filp)
  60. {
  61.     printk( "adc closed\n");
  62.     return 0;
  63. }
  64. static struct file_operations dev_fops = {
  65.     owner:  THIS_MODULE,
  66.     open:   w55fa93_adc_open,
  67.     read:   w55fa93_adc_read,
  68.     release:    w55fa93_adc_release,
  69. };

  70. static struct miscdevice misc = {
  71.     .minor = MISC_DYNAMIC_MINOR,
  72.     .name = DEVICE_NAME,
  73.     .fops = &dev_fops,
  74. };

  75. static int __init dev_init(void)
  76. {
  77.     int ret;
  78.     struct clk *clk;

  79.     base_addr=ioremap(W55FA93_PA_ADC, 0x20);
  80.     if (base_addr == NULL) {
  81.         printk(KERN_ERR "Failed to remap register block\n");
  82.         return -ENOMEM;
  83.     }
  84.     printk("base_addr:0x%lx\n", (unsigned long)base_addr);

  85.     clk = clk_get(NULL, "ADC");
  86.     clk_enable(clk);

  87.     ret = misc_register(&misc);

  88.     printk (DEVICE_NAME"\tinitialized\n");
  89.     return ret;
  90. }

  91. static void __exit dev_exit(void)
  92. {
  93.     struct clk *clk;
  94.     clk = clk_get(NULL, "ADC");
  95.     clk_disable(clk);

  96.     iounmap(base_addr);

  97.     printk (DEVICE_NAME"\texit!\n");
  98.     misc_deregister(&misc);
  99. }

  100. module_init(dev_init);
  101. module_exit(dev_exit);

  102. MODULE_AUTHOR("zpzyf");
  103. MODULE_DESCRIPTION("w55fa93 adc char driver");
  104. MODULE_LICENSE("GPL");


 楼主| 598330983 发表于 2017-5-28 22:45 | 显示全部楼层
应该有人需要这个。
huangcunxiake 发表于 2017-5-28 23:46 | 显示全部楼层
非常感谢,拿走学习学习。
gejigeji521 发表于 2017-6-5 20:18 | 显示全部楼层
能在系统里跑就爽了
dongnanxibei 发表于 2017-6-6 15:09 | 显示全部楼层
学习的好资料。
玛尼玛尼哄 发表于 2017-6-6 17:54 | 显示全部楼层
看的不是很懂。。
heisexingqisi 发表于 2017-6-7 17:37 | 显示全部楼层
这几个ARM9内核的,公开的资料不多。
yiy 发表于 2017-6-7 22:46 | 显示全部楼层
跑的Linux。
xixi2017 发表于 2017-6-8 07:17 | 显示全部楼层
这种很专业的写法,至今无法参透。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

5575

帖子

22

粉丝
快速回复 在线客服 返回列表 返回顶部