-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_mpu6050_drv.c
361 lines (297 loc) · 9.67 KB
/
i2c_mpu6050_drv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/irqreturn.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/ktime.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/sensor/gsnsr_reg.h>
#define CONFIG_MPU605X_POLL (1)
struct gsnsr_data_t{
struct input_dev *input_dev;
int irq;
int ax;
int ay;
int az;
bool data_flag;
atomic_t enabled;
unsigned int tim_val;
int64_t timestamp;
ktime_t ktime;
struct hrtimer hr_timer;
struct work_struct work;
struct i2c_client *client;
};
static inline int64_t mpu605x_acc_get_time_ns(void)
{
struct timespec ts;
get_monotonic_boottime(&ts);
return timespec_to_ns(&ts);
}
/*
** 、注册sensor的输入子系统
*/
static struct gsnsr_data_t g_gsnsr_data;
static ssize_t show_enable_sensor(struct device *device, struct device_attribute *attr,
const char *buf, size_t count){
char tbuf[12] = {0};
strcpy(tbuf,buf);
//sscanf(tbuf,"%d",&g_gsnsr_data.switch_flag);
printk("enable sensor sta = %s",tbuf);
return 0;
}
static ssize_t show_set_sensordelay(struct device *device, struct device_attribute *attr,
const char *buf, size_t count){
char tbuf[12] = {0};
strcpy(tbuf,buf);
sscanf(tbuf,"%d",&g_gsnsr_data.tim_val);
printk("show_set_sensortime tim_val = %s",tbuf);
return 0;
}
static ssize_t show_get_mpu_data(struct device *device, struct device_attribute *attr,
const char *buf, size_t count){
if(true == g_gsnsr_data.data_flag){
return sprintf(buf,"%3d,%3d,%3d",g_gsnsr_data.ax,g_gsnsr_data.ay,g_gsnsr_data.az);
}
return 0;
}
static DEVICE_ATTR(enable_sensor, 0644, NULL, show_enable_sensor);
static DEVICE_ATTR(set_sensordelay, 0644, NULL, show_set_sensordelay);
static DEVICE_ATTR(get_mpu_data, 0644, show_get_mpu_data,NULL);
static struct attribute *mpu605x_input_sysfs_entries[] = {
&dev_attr_enable_sensor.attr,
&dev_attr_set_sensordelay.attr,
&dev_attr_get_mpu_data.attr,
NULL
};
static struct attribute_group mpu605x_input_attr_group = {
.attrs = mpu605x_input_sysfs_entries,
};
static int mpu6050_writereg(const struct i2c_client *cli,u8 reg_add,u8 reg_dat)
{
return i2c_smbus_write_byte_data(cli, reg_dat, reg_add);
}
static unsigned int mpu605x_read_bytes(struct i2c_client *client,u8 cmd,u8 *data,int len){
struct i2c_msg msg[2];
msg[0].addr = 0x68;
msg[0].buf = &cmd;
msg[0].len = 1;
msg[0].flags = 0;
msg[1].addr = 0x68;
msg[1].buf = data;
msg[1].len = len;
msg[1].flags = I2C_M_RD;
return i2c_transfer(client->adapter,msg,2);
}
static void reset_device(struct i2c_client *cli){
mpu6050_writereg(cli,MPU_PWR_MGMT1_REG,0X80); //复位MPU6050
msleep(100);
mpu6050_writereg(cli,MPU_PWR_MGMT1_REG,0X00); //唤醒MPU6050
}
static void mpu6050_set_lpf(struct i2c_client *cli,u16 lpf)
{
u8 data=0;
if(lpf>=188)data=1;
else if(lpf>=98)data=2;
else if(lpf>=42)data=3;
else if(lpf>=20)data=4;
else if(lpf>=10)data=5;
else
data=6;
mpu6050_writereg(cli,MPU_CFG_REG,data);//设置数字低通滤波器
}
static void mpu6050_set_rate(struct i2c_client *cli,u16 rate)
{
u8 data;
if(rate>1000)rate=1000;
if(rate<4)rate=4;
data=1000/rate-1;
data=mpu6050_writereg(cli,MPU_SAMPLE_RATE_REG,data); //设置数字低通滤波器
mpu6050_set_lpf(cli,rate/2); //自动设置LPF为采样率的一半
}
/*
** 、初始化硬件设备
*/
static int gsnsr_bsp_Initlation(struct i2c_client *cli,struct gsnsr_platform_data *info){
int chipid = -1;
reset_device(cli);
chipid = i2c_smbus_read_byte_data(cli, MPU6050_WHO_AM_I);
if(chipid != MPU6050_ADDRESS){
printk("is not support this devices and id = %d\n",chipid);
return -ENODEV;
}
#if (1 == CONFIG_MPU605X_POLL)
mpu6050_writereg(cli,MPU_PWR_MGMT1_REG, 0x00); //解除休眠状态
mpu6050_writereg(cli,MPU_SAMPLE_RATE_REG , 0x07); //陀螺仪采样率
mpu6050_writereg(cli,MPU_CFG_REG , 0x06);
mpu6050_writereg(cli,MPU_ACCEL_CFG_REG , 0x01); //配置加速度传感器工作在16G模式
mpu6050_writereg(cli,MPU_GYRO_CFG_REG, 0x18); //陀螺仪自检及测量范围,典型值:0x18
#else
mpu6050_writereg(cli,MPU_SAMPLE_RATE_REG,20); //陀螺仪采样率 50HZ
mpu6050_writereg(cli,MPU_CFG_REG,0x06);
mpu6050_writereg(cli,MPU_GYRO_CFG_REG,(3<<3));
mpu6050_writereg(cli,MPU_ACCEL_CFG_REG,0 ); //配置加速度传感器工作在16G模式
mpu6050_writereg(cli,MPU_FIFO_EN_REG,0X00); //关闭FIFO
mpu6050_writereg(cli,MPU_USER_CTRL_REG,0X00); //I2C主模式关闭
mpu6050_writereg(cli,MPU_GYRO_CFG_REG,0x18); //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
mpu6050_writereg(cli,MPU_INTBP_CFG_REG,0x9c); //设置为低电平触发
mpu6050_writereg(cli,MPU_INT_EN_REG,0x01); //选择为数据中断
#endif
return 0;
}
static u8 mpu_get_accelerometer(struct i2c_client *cli,short *ax,short *ay,short *az)
{
u8 buf[6];
int res = -1;
//res = i2c_smbus_read_i2c_block_data(cli, MPU_ACCEL_XOUTH_REG, 6,buf);
res = mpu605x_read_bytes(cli,MPU_ACCEL_XOUTH_REG,buf,6);
if(res > 0)
{
*ax=((u16)buf[0]<<8)|buf[1];
*ay=((u16)buf[2]<<8)|buf[3];
*az=((u16)buf[4]<<8)|buf[5];
}
return res;;
}
static void gsensor_data_irq_work(struct work_struct *work) {
u8 sta = 0;
u16 ax = 0,ay = 0,az = 0;
ktime_t tmpkt;
sta = i2c_smbus_read_byte_data(g_gsnsr_data.client,MPU_INT_STA_REG);
if(sta >= 0){
mpu_get_accelerometer(g_gsnsr_data.client,&ax,&ay,&az);
printk("get mpu6050 data x = %d y = %d z = %d\n",ax,ay,az);
input_report_abs(g_gsnsr_data.input_dev, ABS_X, ax);
input_report_abs(g_gsnsr_data.input_dev, ABS_Y, ay);
input_report_abs(g_gsnsr_data.input_dev, ABS_Z, az);
input_sync(g_gsnsr_data.input_dev);
g_gsnsr_data.data_flag = true;
}
#if (1 == CONFIG_MPU605X_POLL)
tmpkt = ktime_sub(g_gsnsr_data.ktime,
ktime_set(0,
(mpu605x_acc_get_time_ns() - g_gsnsr_data.timestamp)));
hrtimer_start(&g_gsnsr_data.hr_timer, g_gsnsr_data.ktime, HRTIMER_MODE_REL);
#else
enable_irq(g_gsnsr_data.irq);
#endif
}
#if (1 == CONFIG_MPU605X_POLL)
static enum hrtimer_restart mpu605x_acc_poll_function_read(struct hrtimer *timer){
g_gsnsr_data.timestamp = mpu605x_acc_get_time_ns();
schedule_work(&g_gsnsr_data.work);
return HRTIMER_NORESTART;
}
#else
static irqreturn_t mpu6050_handler_isr(int irq, void *regs){
disable_irq_nosync(g_gsnsr_data.irq);
schedule_work(&g_gsnsr_data.work);
return IRQ_HANDLED ;
}
#endif
static int gsensor_i2c_probe(struct i2c_client *cli, const struct i2c_device_id *id){
int ret = -EINVAL;
struct gsnsr_platform_data *info;
printk("hello i found mpu6050 devices\n");
/*首先检查I2C是否使能*/
if(!i2c_check_functionality(cli->adapter, I2C_FUNC_I2C)){
printk("i2c0 is not enable\n");
return -ENODEV;
}
g_gsnsr_data.client = cli;
g_gsnsr_data.input_dev = input_allocate_device();
g_gsnsr_data.input_dev->name = "mpu605x";
if(NULL == g_gsnsr_data.input_dev){
printk("input_allocate_device fail\n");
return -ENODEV;
}
info = (struct gsnsr_platform_data *)(cli->dev.platform_data);
if(NULL == info){
printk("info is NULL\n");
goto skip_alloc;
}
g_gsnsr_data.irq = info->irq;
if(0 != gsnsr_bsp_Initlation(cli,info)){
goto skip_alloc;
}
atomic_set(&g_gsnsr_data.enabled, 0);
ret = sysfs_create_group(&cli->dev.kobj, &mpu605x_input_attr_group);
if (ret) {
dev_err(&cli->dev, "create sysfs group failed!\n");
goto skip_alloc;
}
INIT_WORK(&g_gsnsr_data.work, gsensor_data_irq_work);
g_gsnsr_data.input_dev->evbit[0] = BIT_MASK(EV_ABS);//标示input支持的类型
g_gsnsr_data.input_dev->keybit[BIT_WORD(BTN_X)] = BIT_MASK(BTN_X);
g_gsnsr_data.input_dev->keybit[BIT_WORD(BTN_Y)] = BIT_MASK(BTN_Y);
g_gsnsr_data.input_dev->keybit[BIT_WORD(BTN_Z)] = BIT_MASK(BTN_Z);
input_set_abs_params(g_gsnsr_data.input_dev, ABS_X, 0, 0x3FF, 32, 0);
input_set_abs_params(g_gsnsr_data.input_dev, ABS_Y, 0, 0x3FF, 32, 0);
input_set_abs_params(g_gsnsr_data.input_dev, ABS_Z, 0, 0x3FF, 32, 0);
g_gsnsr_data.input_dev->name = "MPU605x";
g_gsnsr_data.input_dev->phys = "input/mpu605x";
g_gsnsr_data.input_dev->id.bustype = BUS_I2C;
g_gsnsr_data.input_dev->id.vendor = 0xDEAD;
g_gsnsr_data.input_dev->id.product = 0xBEEF;
g_gsnsr_data.input_dev->id.version = 0x0102;
printk("irq = %d\n",info->irq);
input_set_drvdata(g_gsnsr_data.input_dev, (void *)&g_gsnsr_data);
#if (1 == CONFIG_MPU605X_POLL)
hrtimer_init(&g_gsnsr_data.hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
g_gsnsr_data.hr_timer.function = &mpu605x_acc_poll_function_read;
hrtimer_start(&(g_gsnsr_data.hr_timer), g_gsnsr_data.ktime, HRTIMER_MODE_REL);
ret = input_register_device(g_gsnsr_data.input_dev);
#else
ret = request_irq(info->irq,mpu6050_handler_isr,
IRQF_TRIGGER_LOW,
"mpu6050", cli);
if(ret){
printk("request_irq fail %d\n",ret);
}
disable_irq(info->irq);
ret = input_register_device(g_gsnsr_data.input_dev);
enable_irq(info->irq);
#endif
return 0;
skip_alloc:
input_free_device(g_gsnsr_data.input_dev);
return 0;
}
static int gsensor_i2c_remove(struct i2c_client *cli){
struct gsnsr_platform_data *info = (struct gsnsr_platform_data *)cli->dev.platform_data;
input_free_device(g_gsnsr_data.input_dev);
input_unregister_device(g_gsnsr_data.input_dev);
free_irq(info->irq, NULL);
return 0;
}
static const struct i2c_device_id mpu6050_i2c_id[] = {
{ "bsp_mpu605x", 0 },
{ }
};
static struct i2c_driver mpu6050_i2c_driver = {
.driver = {
.name = "bsp_mpu605x",
.owner = THIS_MODULE,
},
.probe = gsensor_i2c_probe,
.remove = __devexit_p(gsensor_i2c_remove),
.id_table = mpu6050_i2c_id,
};
static __init int gsensor_Init(void){
return i2c_add_driver(&mpu6050_i2c_driver);
}
static __exit void gsensor_Exit(void){
i2c_del_driver(&mpu6050_i2c_driver);
}
module_init(gsensor_Init);
module_exit(gsensor_Exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zyw");
MODULE_DESCRIPTION("ALL Belong to yuanyang eclec");