driver/input/sensors/accel/目录下增加相应驱动
实现 gsensor_mpu6500_ops 实例:
struct sensor_operate gsensor_da215s_ops= {
.name = "gs_da215s", //与 gsensor_da215s_id 中定义的名称一致
.type = SENSOR_TYPE_ACCEL, //sensor 类型是 gsensor
.id_i2c = ACCEL_ID_DA215S, // 定 义 在 include/linux/sensor-dev.h 中
.read_reg = ACC_X_LSB, //读取 gsensor 数据的起始 寄存器地址
.read_len = 6, //需要读取的 gsensor 数据的字节数
.id_reg = DA215S_CHIP_ID, //芯片唯一 ID 寄存器
.id_data = DA215S_CHIPID_DATA, //芯片 ID 值
.precision = DA215S_PRECISION, //采样 gsensor 数据的adc 位数
.ctrl_reg = DA215S_MODE_BW, //用于使能 gsensor 的寄 存器地址
.int_status_reg = INT_MAP1, //中断状态寄存器地址
.range = {-DA215S_DATA_RANGE, DA215S_DATA_RANGE}, //量程,这里表示上报的量 程为+-1G
.trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT, // 中断类型
.active = sensor_active, //用于开关 gsensor
.init = sensor_init, //用于初始化 gsensor
.report = sensor_report_value, //用于上报gsensor数据
};
static const struct i2c_device_id gsensor_da215s_id[] = {
{"gs_da215s", ACCEL_ID_DA215S},
{}
};
static const struct i2c_device_id gsensor_da215s_id[] = {
{"gs_da215s", ACCEL_ID_DA215S},
{}
};
static int gsensor_da215s_probe(struct i2c_client *client, const struct i2c_device_id *devid)
{
//注册 sensor 驱动到 sensor-dev.c devid 为gsensor_da215s_id地址
return sensor_register_device(client, NULL, devid, &gsensor_da215s_ops);
}
static int gsensor_da215s_remove(struct i2c_client *client)
{
return sensor_unregister_device(client, NULL, &gsensor_da215s_ops);
}
//i2c_driver
static struct i2c_driver gsensor_da215s_driver = {
.probe = gsensor_da215s_probe,
.remove = gsensor_da215s_remove,
.shutdown = sensor_shutdown,
.id_table = gsensor_da215s_id,
.driver = {
.name = "gsensor_da215s",
#ifdef CONFIG_PM
.pm = &sensor_pm_ops,
#endif
},
};
#define DA215S_PRECISION 14
#define DA215S_DATA_RANGE (16384*4)
static struct sensor_operate gsensor_da215s_ops = {
.name = "gs_da215s",
.type = SENSOR_TYPE_ACCEL,
.id_i2c = ACCEL_ID_DA215S,
.read_reg = ACC_X_LSB,
.read_len = 6,
.id_reg = DA215S_CHIP_ID,
.id_data = DA215S_CHIPID_DATA,
.precision = DA215S_PRECISION,
.ctrl_reg = DA215S_MODE_BW,
.int_status_reg = INT_MAP1,
.range = {-DA215S_DATA_RANGE, DA215S_DATA_RANGE},
.trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
.active = sensor_active,
.init = sensor_init,
.report = sensor_report_value,
.suspend = sensor_suspend,
.resume = sensor_resume,
};
gs_da215s: gs_da215s@26 {
status = "okay";
compatible = "gs_da215s";// 与 sensor-dev.c 中的sensor_id 定义匹配
reg = <0x26>;//i2c 地址
irq-gpio = <&gpio1 RK_PA4 IRQ_TYPE_EDGE_RISING>;//中断脚,中断类型
irq_enable = <0>;//是否使用中断模式,如果要过cts
// 或者 vts 建议使用轮询,sensor-dev.c
//的轮询模式已经可以满足 cts 测试要求,如果
//使用中断模式,需要 gsensor chip driver 做好采样率的配置;
poll_delay_ms = <30>;//轮询间隔,最新代码这个值没有意义
type = <SENSOR_TYPE_ACCEL>;//传感器类型,不能搞错
layout = <4>;
};
# Sensors
BOARD_SENSOR_ST := true //采用 RK 的 sensors Hal,也就是本文介绍的
BOARD_SENSOR_MPU_PAD := false //仅适用 MPU6500、mpu6050 等芯片
支持哪些类型的 sensor,如果没有,要配置成 false,否则 vts 和 cts 测试会失败:
BOARD_GRAVITY_SENSOR_SUPPORT := true
BOARD_COMPASS_SENSOR_SUPPORT := false
BOARD_GYROSCOPE_SENSOR_SUPPORT := false
BOARD_PROXIMITY_SENSOR_SUPPORT := false
BOARD_LIGHT_SENSOR_SUPPORT := true
BOARD_PRESSURE_SENSOR_SUPPORT := false
BOARD_TEMPERATURE_SENSOR_SUPPORT := false
/*
*input dev will ignore report data if data value is the same with
last_value,
*sample rate will not enough by this way, so just avoid this case
*/
if ((sensor->axis.x == axis.x) && (sensor->axis.y == axis.y) &&
(sensor->axis.z == axis.z)) {
if (flag) {
flag = 0;
axis.x += 1;
axis.y += 1;
axis.z += 1;
} else {
flag = 1;
axis.x -= 1;
axis.y -= 1;
axis.z -= 1;
}
}