ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / mpu6880_acc.c
1 /* drivers/input/sensors/access/mpu6880_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/mpu6880.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, MPU6880_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 |= MPU6880_PWRM1_SLEEP;
56                 }
57         }
58         else//´ò¿ª
59         {
60                 status = ~BIT_ACCEL_STBY;       
61                 sensor->ops->ctrl_data &= status;
62                 pwrm1 &=~MPU6880_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, MPU6880_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ÊÇ·ñΪMPU6880
92         read_data = sensor_read_reg(client,sensor->ops->id_reg);
93         if(read_data != sensor->ops->id_data)
94         {
95                 printk("%s:check id err,read_data:%d,ops->id_data:%d\n",__func__,read_data,sensor->ops->id_data);
96                 return -1;
97         }
98         
99         //¼Ä´æÆ÷³õʼ»¯
100         res = sensor_write_reg(client, MPU6880_PWR_MGMT_1,0x80);
101         if (res) 
102         {
103                 printk("set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
104                 return res;
105         }
106         msleep(40);
107
108         res = sensor_write_reg(client, MPU6880_GYRO_CONFIG,0x18);  //config gyro for 2000dps
109         if (res) 
110         {
111                 printk("set MPU6880_GYRO_CONFIG error,res: %d!\n", res);
112                 return res;
113         }
114         msleep(10);
115
116         res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG,0x00);  //config Accel for +_2G
117         if (res) 
118         {
119                 printk("set MPU6880_ACCEL_CONFIG error,res: %d!\n", res);
120                 return res;
121         }
122         msleep(10);
123
124         res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG2,0x00);
125         if (res) 
126         {
127                 printk("set MPU6880_ACCEL_CONFIG2 error,res: %d!\n", res);
128                 return res;
129         }
130         res = sensor_write_reg(client, MPU6880_PWR_MGMT_2,0x3F); //set accl and gyro all axis into standby mode
131         if (res) 
132         {
133                 printk("set MPU6880_PWR_MGMT_2 error,res: %d!\n", res);
134                 return res;
135         }       
136         msleep(10);     
137         res = sensor_write_reg(client, MPU6880_PWR_MGMT_1,0x41);
138         if (res) 
139         {
140                 printk("set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
141                 return res;
142         }       
143         msleep(10); 
144
145         //ĬÈϹرÕ
146         res = sensor->ops->active(client,0,0);
147         if(res)
148         {
149                 printk("%s:line=%d,error\n",__func__,__LINE__);
150                 return res;
151         }
152         return res;
153 }
154
155 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
156 {
157         struct sensor_private_data *sensor =
158             (struct sensor_private_data *) i2c_get_clientdata(client);  
159
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);
166
167         return 0;
168 }
169
170 #define GSENSOR_MIN             10
171 static int sensor_report_value(struct i2c_client *client)
172 {
173         struct sensor_private_data *sensor =
174                 (struct sensor_private_data *) i2c_get_clientdata(client);      
175         struct sensor_platform_data *pdata = sensor->pdata;
176         int ret = 0;
177         short x,y,z;
178         struct sensor_axis axis;
179         u8 buffer[6] = {0};     
180         char value = 0;
181         
182         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
183         {
184                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
185                 return -1;
186         }
187         
188         memset(buffer, 0, 6);
189         
190         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
191         do {
192                 *buffer = sensor->ops->read_reg;
193                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
194                 if (ret < 0)
195                 return ret;
196         } while (0);
197
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);
201         
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;
209
210         //ΪÁ˲»ÐÞ¸Ähal²ã´úÂ룬Êý¾Ýת»»ºóÉϱ¨
211         axis.x = 61*axis.x;
212         axis.y = 61*axis.y;
213         axis.z = 61*axis.z;
214         
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))
216         {
217                 gsensor_report_value(client, &axis);
218
219                 /* »¥³âµØ»º´æÊý¾Ý. */
220                 mutex_lock(&(sensor->data_mutex) );
221                 sensor->axis = axis;
222                 mutex_unlock(&(sensor->data_mutex) );
223         }
224
225         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
226         {
227                 
228                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
229                 DBG("%s:sensor int status :0x%x\n",__func__,value);
230         }
231         
232         return ret;
233 }
234
235
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,        
250         .init                           = sensor_init,
251         .report                         = sensor_report_value,
252 };
253
254 /****************operate according to sensor chip:end************/
255
256 //function name should not be changed
257 static struct sensor_operate *gsensor_get_ops(void)
258 {
259         return &gsensor_mpu6880_ops;
260 }
261
262
263 static int __init gsensor_mpu6880_init(void)
264 {
265         struct sensor_operate *ops = gsensor_get_ops();
266         int result = 0;
267         int type = ops->type;
268         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);      
269         return result;
270 }
271
272 static void __exit gsensor_mpu6880_exit(void)
273 {
274         struct sensor_operate *ops = gsensor_get_ops();
275         int type = ops->type;
276         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
277 }
278
279
280 module_init(gsensor_mpu6880_init);
281 module_exit(gsensor_mpu6880_exit);
282
283
284