linux下的I2C驱动记录(RK)

本文详细介绍了RK平台上的I2C驱动实现过程,包括设备文件的位置、I2C地址配置、驱动注册方法等内容,并通过实例展示了触摸屏驱动的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在做的是RK平台的I2C驱动,不是说linux每个外设对应一个设备文件吗?可是找了一下一直没找到,今天特别搞了一下。I2C设备是注册为platform平台驱动的。
下面是我用adb命令查看的
linux的设备文件不一定只在dev/这个目录下面,像sys/ 目录下面也会有一些设备文件的

MID下面的I2C设备的设备文件


[cpp]  view plain copy
  1. 1|root@android:/ # ls sys/bus/i2c/drivers/                                       
  2. RK610_CODEC/    gc0308/         ir-kbd-i2c/     sensors/        tps65910/      
  3. act8846/        gc0328/         nt99340/        sp0838/         wm831x/        
  4. dummy/          gc2035/         rk610_ctl/      sp2518/           
  5. ft5x0x_touch/   gslX680/        rk610_hdmi/     sp2541/  


MID下面的I2C设备的I2C地址

[cpp]  view plain copy
  1. root@android:/ # ls sys/bus/i2c/devices/                                         
  2. 0-004c/ 1-002d/ 1-005a/ 2-0040/ 3-003c/ 4-0042/ 4-0060/ i2c-1/  i2c-3/    
  3. 1-0014/ 1-0034/ 2-0038/ 3-0021/ 4-0040/ 4-0046/ i2c-0/  i2c-2/  i2c-4/  


在板级文件里面,我们可以看到我们的I2C设备地址是多少,代码如下,用i2c_register_board_info这个函数来注册i2c的device。每一个I2C的硬件接口对应一个i2cX_info
 
 
[cpp]  view plain copy
  1. static void __init rk30_i2c_register_board_info(void)  
  2. {  
  3. #ifdef CONFIG_I2C0_RK30  
  4.     i2c_register_board_info(0, i2c0_info, ARRAY_SIZE(i2c0_info));  
  5. #endif  
  6. #ifdef CONFIG_I2C1_RK30  
  7.     i2c_register_board_info(1, i2c1_info, ARRAY_SIZE(i2c1_info));  
  8. #endif  
  9. #ifdef CONFIG_I2C2_RK30  
  10.     i2c_register_board_info(2, i2c2_info, ARRAY_SIZE(i2c2_info));  
  11. #endif  
  12. #ifdef CONFIG_I2C3_RK30  
  13.     i2c_register_board_info(3, i2c3_info, ARRAY_SIZE(i2c3_info));  
  14. #endif  
  15. #ifdef CONFIG_I2C4_RK30  
  16.     i2c_register_board_info(4, i2c4_info, ARRAY_SIZE(i2c4_info));  
  17. #endif  
  18. #ifdef CONFIG_I2C_GPIO_RK30  
  19.     i2c_register_board_info(5, i2c_gpio_info, ARRAY_SIZE(i2c_gpio_info));  
  20. #endif  
  21. }  


[cpp]  view plain copy
  1. #if defined (CONFIG_TOUCHSCREEN_GSLX680)  
  2.     {  
  3.         .type           = "gslX680",  
  4.         .addr           = 0x40,  
  5.         .flags          = 0,  
  6.         .platform_data =&gslx680_info,  
  7.     },  
  8. #endif  


然后看I2C的对应的设备驱动,下面给出一个TP的驱动的注册的地方,用i2c_add_driver这个函数去注册i2c的驱动driver

[cpp]  view plain copy
  1. static const struct i2c_device_id gsl_ts_id[] = {  
  2.     {GSLX680_I2C_NAME, 0},  
  3.     {}  
  4. };  
  5. MODULE_DEVICE_TABLE(i2c, gsl_ts_id);  
  6.   
  7. static struct i2c_driver gsl_ts_driver = {  
  8.     .driver = {  
  9.         .name = GSLX680_I2C_NAME,  
  10.         .owner = THIS_MODULE,  
  11.     },  
  12. #ifndef CONFIG_HAS_EARLYSUSPEND  
  13.     .suspend    = gsl_ts_suspend,  
  14.     .resume = gsl_ts_resume,  
  15. #endif  
  16.     .probe      = gsl_ts_probe,  
  17.     .remove     = __devexit_p(gsl_ts_remove),  
  18.     .id_table   = gsl_ts_id,  
  19. };  
  20.   
  21. static int __init gsl_ts_init(void)  
  22. {  
  23.     int ret;  
  24.     printk("==gsl_ts_init==\n");  
  25.     ret = i2c_add_driver(&gsl_ts_driver);  
  26.     printk("ret=%d\n",ret);  
  27.     return ret;  
  28. }  
  29. static void __exit gsl_ts_exit(void)  
  30. {  
  31.     printk("==gsl_ts_exit==\n");  
  32.     i2c_del_driver(&gsl_ts_driver);  
  33.     return;  
  34. }  
  35.   
  36. module_init(gsl_ts_init);  
  37. module_exit(gsl_ts_exit);  
  38.   
  39. MODULE_LICENSE("GPL");  
  40. MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");  
  41. MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");  
  42. MODULE_ALIAS("platform:gsl_ts");  


好了。然后再看一下这里,可以看到 devices/和drivers/

[cpp]  view plain copy
  1. 1|root@android:/ # ls sys/bus/i2c/                                               
  2. devices/            drivers_autoprobe   uevent                
  3. drivers/            drivers_probe  


再看一下这个,就很清楚的知道这个设备挂载在哪个I2C接口下面了

[cpp]  view plain copy
  1. root@android:/ # ll sys/bus/i2c/drivers/gslX680/                                 
  2. lrwxrwxrwx root     root              2013-09-04 08:51 2-0040 -> ../../../../devices/platform/rk30_i2c.2/i2c-2/2-0040  
  3. --w------- root     root         4096 2013-09-04 08:51 bind  
  4. --w------- root     root         4096 2013-09-04 08:51 uevent  
  5. --w------- root     root         4096 2013-09-04 08:51 unbind  
  6. root@android:/ #  


[cpp]  view plain copy
  1. root@android:/ # ll sys/bus/i2c/devices/2-0040/                                  
  2. lrwxrwxrwx root     root              2013-09-04 08:55 driver -> ../../../../../bus/i2c/drivers/gslX680  
  3. drwxr-xr-x root     root              2013-09-04 08:55 input  
  4. -r--r--r-- root     root         4096 2013-09-04 08:55 modalias  
  5. -r--r--r-- root     root         4096 2013-09-04 08:55 name  
  6. drwxr-xr-x root     root              2013-09-04 08:55 power  
  7. lrwxrwxrwx root     root              2013-09-04 08:55 subsystem -> ../../../../../bus/i2c  
  8. -rw-r--r-- root     root         4096 2013-09-04 08:55 uevent  
  9. root@android:/ #  


到这里已经能够很直接得去理解这个i2c驱动了。其他对于一些代码的理解,网上很多~

自己写了一个I2C的驱动。


[cpp]  view plain copy
  1. /* 
  2.  * drivers/input/touchscreen/gslX680.c 
  3.  * 
  4.  * Copyright (c) 2012 Shanghai Basewin 
  5.  *  Guan Yuwei<guanyuwei@basewin.com> 
  6.  * 
  7.  *  This program is free software; you can redistribute it and/or modify 
  8.  *  it under the terms of the GNU General Public License version 2 as 
  9.  *  published by the Free Software Foundation. 
  10.  */  
  11.   
  12. #include <linux/module.h>  
  13. #include <linux/delay.h>  
  14. #include <linux/earlysuspend.h>  
  15. #include <linux/hrtimer.h>  
  16. #include <linux/i2c.h>  
  17. #include <linux/input.h>  
  18. #include <linux/interrupt.h>  
  19. #include <linux/io.h>  
  20. #include <linux/platform_device.h>  
  21. #include <linux/async.h>  
  22. #include <mach/gpio.h>  
  23. #include <linux/irq.h>  
  24. #include <mach/board.h>  
  25. #include <linux/workqueue.h>  
  26. #include <linux/proc_fs.h>  
  27. #include <linux/input/mt.h>  
  28.   
  29. #define WQF_I2C_NAME "wfi2ctest"  
  30.   
  31. static const struct i2c_device_id gsl_ts_id[] = {  
  32.     {WQF_I2C_NAME, 0},  
  33.     {}  
  34. };  
  35. MODULE_DEVICE_TABLE(i2c, gsl_ts_id);  
  36.   
  37. static struct i2c_driver wftest_driver = {  
  38.     .driver = {  
  39.         .name = WQF_I2C_NAME,  
  40.         .owner = THIS_MODULE,  
  41.     },  
  42. };  
  43.   
  44. static int __init wf_init(void)  
  45. {  
  46.     int ret;  
  47.     printk("==wftest_driver==\n");  
  48.     ret = i2c_add_driver(&wftest_driver);  
  49.     printk("ret=%d\n",ret);  
  50.     return ret;  
  51. }  
  52. static void __exit wf_exit(void)  
  53. {  
  54.     printk("==wftest_driver==\n");  
  55.     i2c_del_driver(&wftest_driver);  
  56.     return;  
  57. }  
  58.   
  59. module_init(wf_init);  
  60. module_exit(wf_exit);  
  61.   
  62. MODULE_LICENSE("GPL");  
  63. MODULE_DESCRIPTION("wqf test i2c driver");  
  64. MODULE_AUTHOR("wf , 329410527@qq.com");  
  65. MODULE_ALIAS("platform:test");  


[cpp]  view plain copy
  1. root@android:/ # ls /sys/bus/i2c/devices/                                        
  2. 0-004c/ 1-002d/ 1-005a/ 2-0040/ 3-0021/ 4-0040/ 4-0046/ i2c-0/  i2c-2/  i2c-4/   
  3. 1-0014/ 1-0034/ 2-0038/ 2-0041/ 3-003c/ 4-0042/ 4-0060/ i2c-1/  i2c-3/    
  4. root@android:/ # ls /sys/bus/i2c/drivers                                         
  5. drivers/            drivers_autoprobe   drivers_probe         
  6. root@android:/ # ls /sys/bus/i2c/drivers/                                        
  7. RK610_CODEC/    gc0308/         ir-kbd-i2c/     sensors/        tps65910/      
  8. act8846/        gc0328/         nt99340/        sp0838/         wfi2ctest/     
  9. dummy/          gc2035/         rk610_ctl/      sp2518/         wm831x/        
  10. ft5x0x_touch/   gslX680/        rk610_hdmi/     sp2541/           
  11. root@android:/ # ls /sys/bus/i2c/drivers/  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值