驱动文件位于:linux-3.4\drivers\hwmon\da380
该目录的tree 如下:
├── built-in.o
├── da380.o
├── Kconfig
├── Makefile
├── mir3da_core.c
├── mir3da_core.h
├── mir3da_core.o
├── mir3da_cust.c
├── mir3da_cust.h
├── mir3da_cust.o
├── modules.builtin
└── modules.order
mir3da_core.c提供了一些需要调用到的算法
mir3da_cust.c提供模块的init 和 exit
我们先来看看mir3da_cust.c:
module_init函数:
-
static int __init mir3da_init(void)
-
{
-
int res;
-
-
MI_FUN;
-
-
if(gsensor_fetch_sysconfig_para()){
-
MI_ERR("fetch_sysconfig_para failed!\n");
-
return -1;
-
}
-
printk("step4 : mir3da_init\n");
-
res = i2c_add_driver(&mir3da_driver);
-
if (res < 0){
-
MI_ERR("add mir3da i2c driver failed\n");
-
return -ENODEV;
-
}
-
-
return (res);
-
}
有2个关键作用
1.
取得sysconfig中的Gsensor配置参数:
gsensor_fetch_sysconfig_para()
2. 添加I2C驱动:
i2c_add_driver(&mir3da_driver)
接下来,我们就看看这个添加的i2c驱动:
mir3da_driver
-
static struct i2c_driver mir3da_driver = {
-
.class = I2C_CLASS_HWMON,
-
.driver = {
-
.name = MIR3DA_DRV_NAME,
-
.owner = THIS_MODULE,
-
},
-
-
.probe = mir3da_probe,
-
.suspend = mir3da_suspend,
-
.resume = mir3da_resume,
-
.detect = mir3da_detect,
-
.remove = __devexit_p(mir3da_remove),
-
.id_table = mir3da_id,
-
#if (PLATFORM == ALLWINNER_A13)
-
.address_list = u_i2c_addr.normal_i2c,
-
#else
-
.address_list = normal_i2c,
-
#endif
-
};
其中, .probe .suspend 等成员函数均在同一个驱动文件中(mir3da_cust.c)实现
除了add_i2c_driver,还需要添加i2c类型的设备表:
-
MODULE_DEVICE_TABLE(i2c, mir3da_id);
这个
mir3da_id就是本设备的i2c设备ID表:(最后一个元素为空,用于标示结束)
-
#define MIR3DA_DRV_NAME "da380"
-
-
static const struct i2c_device_id mir3da_id[] = {
-
{ MIR3DA_DRV_NAME, 0 },
-
{ }
-
};
此外:
-
MIR_GENERAL_OPS_DECLARE(ops_handle, i2c_smbus_read, i2c_smbus_read_block, i2c_smbus_write, sensor_sync_write, sensor_sync_read, msdelay, printk, sprintf);
这个宏声明了:i2c_smbus_read, i2c_smbus_read_block, i2c_smbus_write等函数,只有在哪里调用的,还没有找到 囧囧
此外,还又一大串的:
-
static DEVICE_ATTR(enable, S_IRUGO | S_IWUGO, mir3da_enable_show, mir3da_enable_store);
-
static DEVICE_ATTR(delay, S_IRUGO | S_IWUGO, mir3da_delay_show, mir3da_delay_store);
-
static DEVICE_ATTR(axis_data, S_IRUGO |S_IWUGO, mir3da_axis_data_show, NULL);
-
static DEVICE_ATTR(reg_data, S_IWUGO | S_IRUGO, mir3da_reg_data_show, mir3da_reg_data_store);
-
static DEVICE_ATTR(log_level, S_IWUGO | S_IRUGO, mir3da_log_level_show, mir3da_log_level_store);
-
#ifdef MIR3DA_OFFSET_TEMP_SOLUTION
-
static DEVICE_ATTR(offset, S_IWUGO | S_IRUGO, mir3da_offset_show, mir3da_offset_store);
-
static DEVICE_ATTR(calibrate_miraGSensor, S_IWUGO | S_IRUGO, mir3da_calibrate_show, mir3da_calibrate_store);
-
#endif
-
#ifdef FILTER_AVERAGE_ENHANCE
-
static DEVICE_ATTR(average_enhance, S_IWUGO | S_IRUGO, mir3da_average_enhance_show, mir3da_average_enhance_store);
-
#endif
-
// aad cz
-
static DEVICE_ATTR(int2_enable, S_IRUGO | S_IWUGO, mir3da_int2_enable_show, mir3da_int2_enable_store);
-
static DEVICE_ATTR(int2_clear, S_IRUGO | S_IWUGO, mir3da_int2_clear_enable_show , mir3da_int2_clear_enable_store);
-
static DEVICE_ATTR(int2_start_status, S_IRUGO | S_IWUGO, mir3da_int2_start_statu_show , mir3da_int2_start_statu_store);
-
-
static DEVICE_ATTR(primary_offset, S_IWUGO, mir3da_primary_offset_show, NULL);
-
static DEVICE_ATTR(version, S_IRUGO, mir3da_version_show, NULL);
-
static DEVICE_ATTR(vendor, S_IRUGO, mir3da_vendor_show, NULL);
-
static DEVICE_ATTR(slope_th, S_IRUGO|S_IWUGO,mir3da_slope_th_show, mir3da_slope_th_store);
函数宏DEVICE_ATTR内封装的是__ATTR(_name,_mode,_show,_store)方法,_show表示的是读方法,_stroe表示的是写方法。
当我们cat 一个 接口时则会调用 “_show”,当我们 echo时候回调用“_store”。在函数
_show和_store中真正实现了对相关硬件的操作。相关的_show和_store也都在该文件中定义。
最后,还有一些workqueue(在sensor_write_work()以及sensor_write_work()等函数中,这些函数的调用也没有找到 囧囧)等。
到此,BA380的Gsensor驱动文件简单分析完毕了。
阅读(2539) | 评论(0) | 转发(0) |