1 /* drivers/input/sensors/access/mpu6500_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/mpu6500.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, MPU6500_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 |= MPU6500_PWRM1_SLEEP;
60 status = ~BIT_ACCEL_STBY;
61 sensor->ops->ctrl_data &= status;
62 pwrm1 &=~MPU6500_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, MPU6500_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);
94 if(read_data != sensor->ops->id_data)
96 printk("%s:check id err,read_data:%d,ops->id_data:%d\n",__func__,read_data,sensor->ops->id_data);
101 res = sensor_write_reg(client, MPU6500_PWR_MGMT_1,0x80);
104 printk("set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
109 res = sensor_write_reg(client, MPU6500_GYRO_CONFIG,0x18); //config gyro for 2000dps
112 printk("set MPU6500_GYRO_CONFIG error,res: %d!\n", res);
117 res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG,0x00); //config Accel for +_2G
120 printk("set MPU6500_ACCEL_CONFIG error,res: %d!\n", res);
125 res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG2,0x00);
128 printk("set MPU6500_ACCEL_CONFIG2 error,res: %d!\n", res);
131 res = sensor_write_reg(client, MPU6500_PWR_MGMT_2,0x3F); //set accl and gyro all axis into standby mode
134 printk("set MPU6500_PWR_MGMT_2 error,res: %d!\n", res);
138 res = sensor_write_reg(client, MPU6500_PWR_MGMT_1,0x41);
141 printk("set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
147 res = sensor->ops->active(client,0,0);
150 printk("%s:line=%d,error\n",__func__,__LINE__);
156 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
158 struct sensor_private_data *sensor =
159 (struct sensor_private_data *) i2c_get_clientdata(client);
161 /* Report acceleration sensor information */
162 input_report_abs(sensor->input_dev, ABS_X, axis->x);
163 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
164 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
165 input_sync(sensor->input_dev);
166 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
171 #define GSENSOR_MIN 10
172 static int sensor_report_value(struct i2c_client *client)
174 struct sensor_private_data *sensor =
175 (struct sensor_private_data *) i2c_get_clientdata(client);
176 struct sensor_platform_data *pdata = sensor->pdata;
179 struct sensor_axis axis;
183 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
185 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
189 memset(buffer, 0, 6);
191 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
193 *buffer = sensor->ops->read_reg;
194 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
199 x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
200 y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
201 z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
203 printk("mpu6500_acc: x:%d,y:%d,z:%d,-4:%d\n",x,y,z,-4);
204 printk("mpu6500_acc:orientation:\n%d %d %d\n%d %d %d\n%d %d %d\n",pdata->orientation[0],
205 pdata->orientation[1],pdata->orientation[2],pdata->orientation[3],pdata->orientation[4],
206 pdata->orientation[5],pdata->orientation[6],pdata->orientation[7],pdata->orientation[8]);
208 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
209 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
210 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
212 //ΪÁ˲»ÐÞ¸Ähal²ã´úÂ룬Êý¾Ýת»»ºóÉϱ¨
217 //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))
219 gsensor_report_value(client, &axis);
221 /* »¥³âµØ»º´æÊý¾Ý. */
222 mutex_lock(&(sensor->data_mutex) );
224 mutex_unlock(&(sensor->data_mutex) );
227 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
230 value = sensor_read_reg(client, sensor->ops->int_status_reg);
231 DBG("%s:sensor int status :0x%x\n",__func__,value);
238 struct sensor_operate gsensor_mpu6500_ops = {
239 .name = "mpu6500_acc",
240 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
241 .id_i2c = ACCEL_ID_MPU6500, //i2c id number
242 .read_reg = MPU6500_ACCEL_XOUT_H, //read data
243 .read_len = 6, //data length
244 .id_reg = MPU6500_WHOAMI, //read device id from this register
245 .id_data = MPU6500_DEVICE_ID, //device id
246 .precision = MPU6500_PRECISION, //16 bit
247 .ctrl_reg = MPU6500_PWR_MGMT_2, //enable or disable
248 .int_status_reg = MPU6500_INT_STATUS, //intterupt status register
249 .range = {-32768*61,32768*61}, //range
250 .trig = IRQF_TRIGGER_HIGH |IRQF_ONESHOT,
251 .active = sensor_active,
253 .report = sensor_report_value,
256 /****************operate according to sensor chip:end************/
258 //function name should not be changed
259 static struct sensor_operate *gsensor_get_ops(void)
261 return &gsensor_mpu6500_ops;
265 static int __init gsensor_mpu6500_init(void)
267 struct sensor_operate *ops = gsensor_get_ops();
269 int type = ops->type;
270 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
274 static void __exit gsensor_mpu6500_exit(void)
276 struct sensor_operate *ops = gsensor_get_ops();
277 int type = ops->type;
278 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
282 module_init(gsensor_mpu6500_init);
283 module_exit(gsensor_mpu6500_exit);