1 /* drivers/input/sensors/access/mpu6880_acc.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: oeh<oeh@rock-chips.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
32 #include <linux/sensor-dev.h>
33 #include <linux/mpu6880.h>
37 static int sensor_active(struct i2c_client *client, int enable, int rate)
39 struct sensor_private_data *sensor =
40 (struct sensor_private_data *) i2c_get_clientdata(client);
45 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
46 pwrm1 = sensor_read_reg(client, MPU6880_PWR_MGMT_1);
50 status = BIT_ACCEL_STBY;
51 sensor->ops->ctrl_data |= status;
52 //gyroºÍacc¶¼²»¹¤×÷ʱ£¬Ä£¿é½øÈëÐÝÃß
53 if(sensor->ops->ctrl_data && (BIT_ACCEL_STBY | BIT_GYRO_STBY) != 0)
55 pwrm1 |= MPU6880_PWRM1_SLEEP;
60 status = ~BIT_ACCEL_STBY;
61 sensor->ops->ctrl_data &= status;
62 pwrm1 &=~MPU6880_PWRM1_SLEEP;
64 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
67 printk("%s:fail to set pwrm2\n",__func__);
72 result = sensor_write_reg(client, MPU6880_PWR_MGMT_1,pwrm1);
75 printk("%s:fail to set pwrm1\n",__func__);
84 static int sensor_init(struct i2c_client *client)
88 struct sensor_private_data *sensor =
89 (struct sensor_private_data *) i2c_get_clientdata(client);
92 read_data = sensor_read_reg(client,sensor->ops->id_reg);
93 if(read_data != sensor->ops->id_data)
95 printk("%s:check id err,read_data:%d,ops->id_data:%d\n",__func__,read_data,sensor->ops->id_data);
100 res = sensor_write_reg(client, MPU6880_PWR_MGMT_1,0x80);
103 printk("set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
108 res = sensor_write_reg(client, MPU6880_GYRO_CONFIG,0x18); //config gyro for 2000dps
111 printk("set MPU6880_GYRO_CONFIG error,res: %d!\n", res);
116 res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG,0x00); //config Accel for +_2G
119 printk("set MPU6880_ACCEL_CONFIG error,res: %d!\n", res);
124 res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG2,0x00);
127 printk("set MPU6880_ACCEL_CONFIG2 error,res: %d!\n", res);
130 res = sensor_write_reg(client, MPU6880_PWR_MGMT_2,0x3F); //set accl and gyro all axis into standby mode
133 printk("set MPU6880_PWR_MGMT_2 error,res: %d!\n", res);
137 res = sensor_write_reg(client, MPU6880_PWR_MGMT_1,0x41);
140 printk("set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
146 res = sensor->ops->active(client,0,0);
149 printk("%s:line=%d,error\n",__func__,__LINE__);
155 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
157 struct sensor_private_data *sensor =
158 (struct sensor_private_data *) i2c_get_clientdata(client);
160 /* Report acceleration sensor information */
161 input_report_abs(sensor->input_dev, ABS_X, axis->x);
162 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
163 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
164 input_sync(sensor->input_dev);
165 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
170 #define GSENSOR_MIN 10
171 static int sensor_report_value(struct i2c_client *client)
173 struct sensor_private_data *sensor =
174 (struct sensor_private_data *) i2c_get_clientdata(client);
175 struct sensor_platform_data *pdata = sensor->pdata;
178 struct sensor_axis axis;
182 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
184 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
188 memset(buffer, 0, 6);
190 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
192 *buffer = sensor->ops->read_reg;
193 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
198 x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
199 y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
200 z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
202 printk("mpu6880_acc: x:%d,y:%d,z:%d,-4:%d\n",x,y,z,-4);
203 printk("mpu6880_acc:orientation:\n%d %d %d\n%d %d %d\n%d %d %d\n",pdata->orientation[0],
204 pdata->orientation[1],pdata->orientation[2],pdata->orientation[3],pdata->orientation[4],
205 pdata->orientation[5],pdata->orientation[6],pdata->orientation[7],pdata->orientation[8]);
206 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
207 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
208 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
210 //ΪÁ˲»ÐÞ¸Ähal²ã´úÂ룬Êý¾Ýת»»ºóÉϱ¨
215 //if((abs(sensor->axis.x - axis.x) > GSENSOR_MIN) || (abs(sensor->axis.y - axis.y) > GSENSOR_MIN) || (abs(sensor->axis.z - axis.z) > GSENSOR_MIN))
217 gsensor_report_value(client, &axis);
219 /* »¥³âµØ»º´æÊý¾Ý. */
220 mutex_lock(&(sensor->data_mutex) );
222 mutex_unlock(&(sensor->data_mutex) );
225 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
228 value = sensor_read_reg(client, sensor->ops->int_status_reg);
229 DBG("%s:sensor int status :0x%x\n",__func__,value);
236 struct sensor_operate gsensor_mpu6880_ops = {
237 .name = "mpu6880_acc",
238 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
239 .id_i2c = ACCEL_ID_MPU6880, //i2c id number
240 .read_reg = MPU6880_ACCEL_XOUT_H, //read data
241 .read_len = 6, //data length
242 .id_reg = MPU6880_WHOAMI, //read device id from this register
243 .id_data = MPU6880_DEVICE_ID, //device id
244 .precision = MPU6880_PRECISION, //16 bit
245 .ctrl_reg = MPU6880_PWR_MGMT_2, //enable or disable
246 .int_status_reg = MPU6880_INT_STATUS, //intterupt status register
247 .range = {-32768*61,32768*61}, //range
248 .trig = IRQF_TRIGGER_HIGH |IRQF_ONESHOT,
249 .active = sensor_active,
251 .report = sensor_report_value,
254 /****************operate according to sensor chip:end************/
256 //function name should not be changed
257 static struct sensor_operate *gsensor_get_ops(void)
259 return &gsensor_mpu6880_ops;
263 static int __init gsensor_mpu6880_init(void)
265 struct sensor_operate *ops = gsensor_get_ops();
267 int type = ops->type;
268 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
272 static void __exit gsensor_mpu6880_exit(void)
274 struct sensor_operate *ops = gsensor_get_ops();
275 int type = ops->type;
276 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
280 module_init(gsensor_mpu6880_init);
281 module_exit(gsensor_mpu6880_exit);