ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / mpu6500_acc.c
1 /* drivers/input/sensors/access/mpu6500_acc.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: oeh<oeh@rock-chips.com>
5  *
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.
9  *
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.
14  *
15  */
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>
31 #endif
32 #include <linux/sensor-dev.h>
33 #include <linux/mpu6500.h>
34
35
36
37 static int sensor_active(struct i2c_client *client, int enable, int rate)
38 {
39         struct sensor_private_data *sensor =
40             (struct sensor_private_data *) i2c_get_clientdata(client);  
41         int result = 0;
42         int status = 0;
43         u8 pwrm1 = 0;   
44         
45         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
46         pwrm1 = sensor_read_reg(client, MPU6500_PWR_MGMT_1);
47         //¹Ø±Õ
48         if(!enable)
49         {       
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)
54                 {
55                         pwrm1 |= MPU6500_PWRM1_SLEEP;
56                 }
57         }
58         else//´ò¿ª
59         {
60                 status = ~BIT_ACCEL_STBY;       
61                 sensor->ops->ctrl_data &= status;
62                 pwrm1 &=~MPU6500_PWRM1_SLEEP;
63         }
64         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
65         if(result)
66         {
67                 printk("%s:fail to set pwrm2\n",__func__);
68                 return -1;
69         }
70         msleep(20);
71
72         result = sensor_write_reg(client, MPU6500_PWR_MGMT_1,pwrm1);
73         if(result)
74         {
75                 printk("%s:fail to set pwrm1\n",__func__);
76                 return -1;
77         }
78         msleep(20);
79         
80         return result;
81
82 }
83
84 static int sensor_init(struct i2c_client *client)
85 {
86         int res=0; 
87         u8 read_data = 0;
88         struct sensor_private_data *sensor =
89             (struct sensor_private_data *) i2c_get_clientdata(client);  
90
91         //¼ì²âICÊÇ·ñΪMPU6500
92         read_data = sensor_read_reg(client,sensor->ops->id_reg);
93         
94         if(read_data != sensor->ops->id_data)
95         {
96                 printk("%s:check id err,read_data:%d,ops->id_data:%d\n",__func__,read_data,sensor->ops->id_data);
97                 return -1;
98         }
99         
100         //¼Ä´æÆ÷³õʼ»¯
101         res = sensor_write_reg(client, MPU6500_PWR_MGMT_1,0x80);
102         if (res) 
103         {
104                 printk("set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
105                 return res;
106         }
107         msleep(40);
108
109         res = sensor_write_reg(client, MPU6500_GYRO_CONFIG,0x18);  //config gyro for 2000dps
110         if (res) 
111         {
112                 printk("set MPU6500_GYRO_CONFIG error,res: %d!\n", res);
113                 return res;
114         }
115         msleep(10);
116
117         res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG,0x00);  //config Accel for +_2G
118         if (res) 
119         {
120                 printk("set MPU6500_ACCEL_CONFIG error,res: %d!\n", res);
121                 return res;
122         }
123         msleep(10);
124
125         res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG2,0x00);
126         if (res) 
127         {
128                 printk("set MPU6500_ACCEL_CONFIG2 error,res: %d!\n", res);
129                 return res;
130         }
131         res = sensor_write_reg(client, MPU6500_PWR_MGMT_2,0x3F); //set accl and gyro all axis into standby mode
132         if (res) 
133         {
134                 printk("set MPU6500_PWR_MGMT_2 error,res: %d!\n", res);
135                 return res;
136         }       
137         msleep(10);     
138         res = sensor_write_reg(client, MPU6500_PWR_MGMT_1,0x41);
139         if (res) 
140         {
141                 printk("set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
142                 return res;
143         }       
144         msleep(10); 
145
146         //ĬÈϹرÕ
147         res = sensor->ops->active(client,0,0);
148         if(res)
149         {
150                 printk("%s:line=%d,error\n",__func__,__LINE__);
151                 return res;
152         }
153         return res;
154 }
155
156 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
157 {
158         struct sensor_private_data *sensor =
159             (struct sensor_private_data *) i2c_get_clientdata(client);  
160
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);
167
168         return 0;
169 }
170
171 #define GSENSOR_MIN             10
172 static int sensor_report_value(struct i2c_client *client)
173 {
174         struct sensor_private_data *sensor =
175                 (struct sensor_private_data *) i2c_get_clientdata(client);      
176         struct sensor_platform_data *pdata = sensor->pdata;
177         int ret = 0;
178         short x,y,z;
179         struct sensor_axis axis;
180         u8 buffer[6] = {0};     
181         char value = 0;
182         
183         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
184         {
185                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
186                 return -1;
187         }
188         
189         memset(buffer, 0, 6);
190         
191         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
192         do {
193                 *buffer = sensor->ops->read_reg;
194                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
195                 if (ret < 0)
196                 return ret;
197         } while (0);
198
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);
202         /*
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]);
207         */
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;
211
212         //ΪÁ˲»ÐÞ¸Ähal²ã´úÂ룬Êý¾Ýת»»ºóÉϱ¨
213         axis.x = 61*axis.x;
214         axis.y = 61*axis.y;
215         axis.z = 61*axis.z;
216         
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))
218         {
219                 gsensor_report_value(client, &axis);
220
221                 /* »¥³âµØ»º´æÊý¾Ý. */
222                 mutex_lock(&(sensor->data_mutex) );
223                 sensor->axis = axis;
224                 mutex_unlock(&(sensor->data_mutex) );
225         }
226
227         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
228         {
229                 
230                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
231                 DBG("%s:sensor int status :0x%x\n",__func__,value);
232         }
233         
234         return ret;
235 }
236
237
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,        
252         .init                           = sensor_init,
253         .report                         = sensor_report_value,
254 };
255
256 /****************operate according to sensor chip:end************/
257
258 //function name should not be changed
259 static struct sensor_operate *gsensor_get_ops(void)
260 {
261         return &gsensor_mpu6500_ops;
262 }
263
264
265 static int __init gsensor_mpu6500_init(void)
266 {
267         struct sensor_operate *ops = gsensor_get_ops();
268         int result = 0;
269         int type = ops->type;
270         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);      
271         return result;
272 }
273
274 static void __exit gsensor_mpu6500_exit(void)
275 {
276         struct sensor_operate *ops = gsensor_get_ops();
277         int type = ops->type;
278         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
279 }
280
281
282 module_init(gsensor_mpu6500_init);
283 module_exit(gsensor_mpu6500_exit);
284
285
286