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