566de2155546196d39fb5f80812c13c64060177c
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / lis3dh.c
1 /* drivers/input/sensors/access/kxtik.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@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 <mach/gpio.h>
29 #include <mach/board.h> 
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34
35
36 #define LIS3DH_INT_COUNT                (0x0E)
37 #define LIS3DH_WHO_AM_I                 (0x0F)
38
39 /* full scale setting - register & mask */
40 #define LIS3DH_TEMP_CFG_REG             (0x1F)
41 #define LIS3DH_CTRL_REG1                (0x20)
42 #define LIS3DH_CTRL_REG2                (0x21)
43 #define LIS3DH_CTRL_REG3                (0x22)
44 #define LIS3DH_CTRL_REG4                (0x23)
45 #define LIS3DH_CTRL_REG5                (0x24)
46 #define LIS3DH_CTRL_REG6                (0x25)
47 #define LIS3DH_REFERENCE                (0x26)
48 #define LIS3DH_STATUS_REG               (0x27)
49 #define LIS3DH_OUT_X_L                  (0x28)
50 #define LIS3DH_OUT_X_H                  (0x29)
51 #define LIS3DH_OUT_Y_L                  (0x2a)
52 #define LIS3DH_OUT_Y_H                  (0x2b)
53 #define LIS3DH_OUT_Z_L                  (0x2c)
54 #define LIS3DH_OUT_Z_H                  (0x2d)
55 #define LIS3DH_FIFO_CTRL_REG            (0x2E)
56
57 #define LIS3DH_INT1_CFG                 (0x30)
58 #define LIS3DH_INT1_SRC                 (0x31)
59 #define LIS3DH_INT1_THS                 (0x32)
60 #define LIS3DH_INT1_DURATION            (0x33)
61
62 #define LIS3DH_DEVID                    (0x33)  //chip id
63 #define LIS3DH_ACC_DISABLE              (0x08)
64
65 #define LIS3DH_RANGE                    2000000
66
67 /* LIS3DH */
68 #define LIS3DH_PRECISION                16
69 #define LIS3DH_BOUNDARY                 (0x1 << (LIS3DH_PRECISION - 1))
70 #define LIS3DH_GRAVITY_STEP             (LIS3DH_RANGE / LIS3DH_BOUNDARY)
71
72 #define ODR1                            0x10  /* 1Hz output data rate */
73 #define ODR10                           0x20  /* 10Hz output data rate */
74 #define ODR25                           0x30  /* 25Hz output data rate */
75 #define ODR50                           0x40  /* 50Hz output data rate */
76 #define ODR100                          0x50  /* 100Hz output data rate */
77 #define ODR200                          0x60  /* 200Hz output data rate */
78 #define ODR400                          0x70  /* 400Hz output data rate */
79 #define ODR1250                         0x90  /* 1250Hz output data rate */
80
81
82
83 struct sensor_reg_data {
84         char reg;
85         char data;
86 };
87
88 /****************operate according to sensor chip:start************/
89
90 static int sensor_active(struct i2c_client *client, int enable, int rate)
91 {
92         struct sensor_private_data *sensor =
93             (struct sensor_private_data *) i2c_get_clientdata(client);  
94         int result = 0;
95         int status = 0;
96                 
97         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
98
99         sensor->ops->ctrl_data |= ODR100;       //100HZ,if 0 then power down
100         
101         //register setting according to chip datasheet          
102         if(!enable)
103         {       
104                 status = LIS3DH_ACC_DISABLE;    //lis3dh        
105                 sensor->ops->ctrl_data |= status;       
106         }
107         else
108         {
109                 status = ~LIS3DH_ACC_DISABLE;   //lis3dh
110                 sensor->ops->ctrl_data &= status;
111         }
112
113         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
114         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
115         if(result)
116                 printk("%s:fail to active sensor\n",__func__);
117         
118         return result;
119
120 }
121
122 static int sensor_init(struct i2c_client *client)
123 {       
124         struct sensor_private_data *sensor =
125             (struct sensor_private_data *) i2c_get_clientdata(client);  
126         int result = 0;
127         int i;
128         struct sensor_reg_data reg_data[] = 
129         {                       
130                 {LIS3DH_CTRL_REG2,0X00},                        
131                 {LIS3DH_CTRL_REG4,0x08},        //High resolution output mode: 1, Normal mode   
132                 {LIS3DH_CTRL_REG6,0x40},        
133                 {LIS3DH_TEMP_CFG_REG,0x00},     //
134                 {LIS3DH_FIFO_CTRL_REG,0x00},    //      
135                 {LIS3DH_INT1_CFG,0xFF},         //6 direction position recognition      
136                 {LIS3DH_INT1_THS,0x7F},         //Interrupt 1 threshold 
137                 {LIS3DH_INT1_DURATION,0x7F},    //Duration value 0x00->ox7f
138         };  
139         
140         result = sensor->ops->active(client,0,0);
141         if(result)
142         {
143                 printk("%s:line=%d,error\n",__func__,__LINE__);
144                 return result;
145         }
146
147         sensor->status_cur = SENSOR_OFF;
148         
149         for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
150         {
151                 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
152                 if(result)
153                 {
154                         printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
155                         return result;
156                 }
157         }
158
159         
160         if(sensor->pdata->irq_enable)
161         {
162
163                 result = sensor_write_reg(client, LIS3DH_CTRL_REG3, 0x40);//I1_AOI1 =1  if motion       
164                 if(result)
165                 {
166                         printk("%s:line=%d,error\n",__func__,__LINE__);
167                         return result;
168                 }
169
170                 result = sensor_write_reg(client, LIS3DH_CTRL_REG5, 0x08);
171                 if(result)
172                 {
173                         printk("%s:line=%d,error\n",__func__,__LINE__);
174                         return result;
175                 }
176
177         }
178         
179         return result;
180 }
181
182 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
183 {
184         s64 result;
185         struct sensor_private_data *sensor =
186             (struct sensor_private_data *) i2c_get_clientdata(client);  
187         //int precision = sensor->ops->precision;
188         switch (sensor->devid) {        
189                 case LIS3DH_DEVID:              
190                         result = ((int)high_byte << 8) | (int)low_byte;
191                         if (result < LIS3DH_BOUNDARY)
192                         result = result* LIS3DH_GRAVITY_STEP;
193                 else
194                         result = ~( ((~result & (0x7fff>>(16-LIS3DH_PRECISION)) ) + 1) 
195                                                 * LIS3DH_GRAVITY_STEP) + 1;
196                         break;
197
198                 default:
199                         printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
200                         return -EFAULT;
201     }
202
203     return (int)result;
204 }
205
206 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
207 {
208         struct sensor_private_data *sensor =
209                 (struct sensor_private_data *) i2c_get_clientdata(client);      
210
211         /* Report acceleration sensor information */
212         input_report_abs(sensor->input_dev, ABS_X, axis->x);
213         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
214         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
215         input_sync(sensor->input_dev);
216         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
217
218         return 0;
219 }
220
221 #define GSENSOR_MIN  10
222 static int sensor_report_value(struct i2c_client *client)
223 {
224         struct sensor_private_data *sensor =
225                         (struct sensor_private_data *) i2c_get_clientdata(client);      
226         struct sensor_platform_data *pdata = sensor->pdata;
227         int ret = 0;
228         int x,y,z;
229         struct sensor_axis axis;        
230         char buffer[6] = {0};   
231         char value = 0;
232         
233         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
234         {
235                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
236                 return -1;
237         }
238         
239         memset(buffer, 0, 6);
240
241         value = sensor_read_reg(client, LIS3DH_STATUS_REG);
242         if((value & 0x0f) == 0)
243         {
244                 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
245                 return -1;
246         }
247                 
248         
249         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
250         do {
251                 *buffer = sensor->ops->read_reg;
252                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
253                 if (ret < 0)
254                 return ret;
255         } while (0);
256
257         //this gsensor need 6 bytes buffer
258         x = sensor_convert_data(sensor->client, buffer[1], buffer[0]);  //buffer[1]:high bit 
259         y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
260         z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);          
261
262         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
263         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;     
264         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
265
266         DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
267
268         //Report event  only while value is changed to save some power
269         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))
270         {
271                 gsensor_report_value(client, &axis);
272
273                 /* »¥³âµØ»º´æÊý¾Ý. */
274                 mutex_lock(&(sensor->data_mutex) );
275                 sensor->axis = axis;
276                 mutex_unlock(&(sensor->data_mutex) );
277         }
278
279         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
280         {
281                 
282                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
283                 DBG("%s:sensor int status :0x%x\n",__func__,value);
284         }
285         
286         return ret;
287 }
288
289 struct sensor_operate gsensor_lis3dh_ops = {
290         .name                           = "lis3dh",
291         .type                           = SENSOR_TYPE_ACCEL,            //sensor type and it should be correct
292         .id_i2c                         = ACCEL_ID_LIS3DH,              //i2c id number
293         .read_reg                       = (LIS3DH_OUT_X_L | 0x80),      //read data
294         .read_len                       = 6,                            //data length
295         .id_reg                         = LIS3DH_WHO_AM_I,              //read device id from this register
296         .id_data                        = LIS3DH_DEVID,                 //device id
297         .precision                      = LIS3DH_PRECISION,             //12 bits
298         .ctrl_reg                       = LIS3DH_CTRL_REG1,             //enable or disable 
299         .int_status_reg                 = LIS3DH_INT1_SRC,              //intterupt status register
300         .range                          = {-LIS3DH_RANGE,LIS3DH_RANGE}, //range
301         .trig                           = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),              
302         .active                         = sensor_active,        
303         .init                           = sensor_init,
304         .report                         = sensor_report_value,
305 };
306
307 /****************operate according to sensor chip:end************/
308
309 //function name should not be changed
310 static struct sensor_operate *gsensor_get_ops(void)
311 {
312         return &gsensor_lis3dh_ops;
313 }
314
315
316 static int __init gsensor_lis3dh_init(void)
317 {
318         struct sensor_operate *ops = gsensor_get_ops();
319         int result = 0;
320         int type = ops->type;
321         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
322         return result;
323 }
324
325 static void __exit gsensor_lis3dh_exit(void)
326 {
327         struct sensor_operate *ops = gsensor_get_ops();
328         int type = ops->type;
329         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
330 }
331
332
333 module_init(gsensor_lis3dh_init);
334 module_exit(gsensor_lis3dh_exit);
335
336