接续上一节,本文主要介绍驱动部分的客制化
1, hardware层
通过系统调用open,read,write对sys/class/sensor/m_acc_misc读写操作
路径:vendor/mediatek/proprietary/hardware/sensor/sensors-1.0/Acceleration.cpp
直接操作/sys/class/sensor/m_acc_misc节点,发命令。
2,kernel 层Sensor Driver 的客制化
2.1 驱动参数配置
Accelerometer 参数配置:
注意传感器的方向
2.2 创建传感器节点属性(所以传感器都需要通过次方法创建sys节点)
路径:drivers/misc/mediatek/sensors-1.0/hwmon/sensor_attributes/sensor_attr.c
static int __init sensor_attr_init(void)
sensor_attr_class = class_create(THIS_MODULE, “sensor”); //sys/class/sensor
sensor_attr_major = register_chrdev(0, “sensor”, &sensor_attr_fops);//注册sensor主设备号
int sensor_attr_register(struct sensor_attr_t *misc)
dev = MKDEV(sensor_attr_major, misc->minor);//对于每个sensor生成主次设备号
misc->this_device = device_create(sensor_attr_class, misc->parent, dev,misc, “%s”, misc->name); //创建节点/dev/m_acc_misc
err = sensor_event_register(misc->minor);//向hwmon注册设备
obj->client[handle].head = 0;
obj->client[handle].tail = 0;
obj->client[handle].bufsize = CONTINUE_SENSOR_BUF_SIZE;
缓存各种传感器上报的数据存入buffer(所有传感器的数据都会通过一个buffer数组存储起来)
路径:drivers/misc/mediatek/sensors-1.0/hwmon/sensor_event/sensor_event.c
int sensor_input_event(unsigned char handle, const struct sensor_event *event)
client->buffer[client->head++] = *event; //将event存储用数组实现的队列中
client->head &= client->bufsize - 1;//0 1 2 3 … 2047 0 1 2 3… 2047 0 1 2 3…
unsigned int sensor_event_register(unsigned char handle)
obj->client[handle].head = 0;
obj->client[handle].tail = 0;
obj->client[handle].bufsize = BIO_SENSOR_BUF_SIZE;
obj->client[handle].buffull = false;
管理所有加速度传感器,并且注册的大量sys节点属性提供给上层访问,将所有加速度传感器接口通过节点属性给上层调用
路径:kernel-4.14/drivers/misc/mediatek/sensors-1.0/accelerometer/accel.c
static int acc_probe(void)
acc_context_obj = acc_context_alloc_object();
struct acc_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
INIT_WORK(&obj->report, acc_work_func);//初始化report工作队列绑定acc_work_func函数
cxt = acc_context_obj;
cxt->acc_data.get_data(&x, &y, &z, &status);//从mc3433驱动中通过get_data获取xyz数据
acc_data_report(&cxt->drv_data);//数据上报
event.word[0] = data->x;
event.word[1] = data->y;
event.word[2] = data->z;
sensor_input_event() //hwmon/sensor_event/sensor_event.c
initTimer(&obj->hrTimer, acc_poll);//初始化定时器,绑定acc_poll函数
hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
timer->function = callback;
acc_real_driver_init();//调用所有加速度传感器驱动的init函数
gsensor_init_list[i]->init(); //调用具体驱动的init函数,比如mc3433
DEVICE_ATTR(accenablenodata, 0644, acc_show_enable_nodata,acc_store_enable_nodata); //上电
DEVICE_ATTR(accactive, 0644, acc_show_active, acc_store_active); //使能
DEVICE_ATTR(accbatch, 0644, acc_show_batch, acc_store_batch);
DEVICE_ATTR(accflush, 0644, acc_show_flush, acc_store_flush); //FLUSH_ACTION
DEVICE_ATTR(acccali, 0644, acc_show_cali, acc_store_cali);
DEVICE_ATTR(accdevnum, 0644, acc_show_sensordevnum, NULL);
提供给具体加速度传感器使用的通用接口
int acc_register_control_path(struct acc_control_path *ctl) //用于注册控制相关操作函数
acc_misc_init(struct acc_context *cxt)
cxt->mdev.fops = &accel_fops;//open,read(sensor_event_read),poll
err = sensor_attr_register(&cxt->mdev);//生成m_acc_misc节点
sysfs_create_group(&acc_context_obj->mdev.this_device->kobj,&acc_attribute_group);
kobject_uevent(&acc_context_obj->mdev.this_device->kobj, KOBJ_ADD);
int acc_register_data_path(struct acc_data_path *data) //用于注册获取数据相关操作函数
cxt = acc_context_obj;
cxt->acc_data.get_data = data->get_data;
cxt->acc_data.get_raw_data = data->get_raw_data;
int acc_driver_add(struct acc_init_info *obj) //将驱动注册到gsensor_init_list[i]中
obj->platform_diver_addr = &gsensor_driver;
gsensor_init_list[i] = obj;
加速度传感器芯片mc3433驱动,i2c驱动
路径:drivers/misc/mediatek/sensors-1.0/accelerometer/mc3433/mc3433.c
static int __init mc3xxx_init(void)
acc_driver_add(&mc3xxx_init_info);
static struct acc_init_info mc3xxx_init_info = {
.name = MC3XXX_DEV_NAME,
.init = mc3xxx_local_init,
.uninit = mc3xxx_remove,
};
static int mc3xxx_local_init(void)
i2c_add_driver(&mc3xxx_i2c_driver)
static struct i2c_driver mc3xxx_i2c_driver = {
.driver = {
.name = MC3XXX_DEV_NAME,
.pm = &mc3xxx_pm_ops,
.of_match_table = accel_of_match,
},
.probe = mc3xxx_i2c_probe,
.remove = mc3xxx_i2c_remove,
.id_table = mc3xxx_i2c_id,
};
static int mc3xxx_i2c_probe
get_accel_dts_func(client->dev.of_node, hw); //从设备树解析i2c_num,i2c_addr,direction,power_vol等信息初始化hw
hwmsen_get_convert(obj->hw->direction, &obj->cvt);
MC3XXX_Init(new_client, 1);//i2c 硬件初始化
accel_factory_device_register(&mc3xxx_factory_device); //工厂模式
mc3xxx_create_attr(&(mc3xxx_init_info.platform_diver_addr->driver)); //sys创建mc3xxx节点属性
for (idx = 0; idx < num; idx++)
err = driver_create_file(driver, mc3xxx_attr_list[idx]);
ctl.open_report_data = mc3xxx_open_report_data; //一些函数赋值
ctl.enable_nodata = mc3xxx_enable_nodata;
ctl.batch = mc3xxx_batch;
ctl.flush = mc3xxx_flush;
ctl.set_delay = mc3xxx_set_delay;
acc_register_control_path(&ctl);
acc_register_data_path(&data);
3,驱动移植实例
3.1.添加kernel-3.18\drivers\misc\mediatek\magnetometer\st480文件夹(包含st480.c、st480.h、Kconfig、Makefile四个
st480.c
st480.h
Kconfig 建宏--MTK_ST480
Makefile
3.2.kernel-3.18\drivers\misc\mediatek\magnetometer\Kconfig 添加:
source "drivers/misc/mediatek/magnetometer/st480/Kconfig
3.3.kernel-3.18\drivers\misc\mediatek\magnetometer\Makefile 添加:
obj-$(CONFIG_MTK_ST480) += st480/
- 4.kernel-3.18\arch\arm\boot\dts\len6797_6m_n.dts 添加:
注:这一段没有的话(或名字不对应),会导致开机重启
cust_mag@0 {
compatible = "mediatek,st480";
i2c_num = <2>; // i2c控制器2(即i2c总线2),sensor都接在控制器2上--即i2c总线2
i2c_addr = <0x0C 0 0 0>; // dws也写了一个i2c地址,到底用哪个看驱动,通常写在dws
direction = <1>; // 方向 –> 唯一用到的
power_id = <0xffff>;
power_vol = <0>;
is_batch_supported = <0>;
};
3.5.kernel-3.18\arch\arm\configs\len6797_6m_n_debug_defconfig、len6797_6m_n_defconfig 修改
CONFIG_CUSTOM_KERNEL_MAGNETOMETER=y
CONFIG_MTK_ST480=y
4,调试方法
getevent -i 查看所有的input设备
getevent -t /dev/input/event4 获取gsensor往上层上报的包(封装过,加入time等信息,以16进制方式打印)
1. ps
靠近:
[ 2208.288534] 0002 0002 00000001
[ 2208.288534] 0002 0001 00000003
[ 2208.288534] 0000 0000 00000000
远离:
[ 2212.838044] 0002 0002 00000002
[ 2212.838044] 0002 0001 00000003
[ 2212.838044] 0000 0000 00000000
2. cat /proc/kmsg | grep "dgsensor"
<4>[ 564.973691]<0> (0)[2935:kworker/0:3]dgsensor epl_sensor_read_ps_status gRawData.raw_bytes[0] = 0xa
<4>[ 564.973725]<0> (0)[2935:kworker/0:3]dgsensor epl_sensor_read_ps_status epl_sensor.ps.compare_low = 0x8
<4>[ 565.168562]<0> (0)[2935:kworker/0:3]dgsensor epl_sensor_read_ps_status gRawData.raw_bytes[0] = 0xa
<4>[ 565.168588]<0> (0)[2935:kworker/0:3]dgsensor epl_sensor_read_ps_status epl_sensor.ps.compare_low = 0x8
<4>[ 565.368485]<0> (0)[2935:kworker/0:3]dgsensor epl_sensor_read_ps_status gRawData.raw_bytes[0] = 0xa