fix sensors include file err
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / mma7660.c
1 /* drivers/input/sensors/access/mma7660.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 MMA7660_ENABLE          1
36
37 #define MMA7660_REG_X_OUT       0x0
38 #define MMA7660_REG_Y_OUT       0x1
39 #define MMA7660_REG_Z_OUT       0x2
40 #define MMA7660_REG_TILT        0x3
41 #define MMA7660_REG_SRST        0x4
42 #define MMA7660_REG_SPCNT       0x5
43 #define MMA7660_REG_INTSU       0x6
44 #define MMA7660_REG_MODE        0x7
45 #define MMA7660_REG_SR          0x8
46 #define MMA7660_REG_PDET        0x9
47 #define MMA7660_REG_PD          0xa
48
49
50 #define MMA7660_RANGE                   1500000
51
52 /* LIS3DH */
53 #define MMA7660_PRECISION       6
54 #define MMA7660_BOUNDARY                (0x1 << (MMA7660_PRECISION - 1))
55 #define MMA7660_GRAVITY_STEP            (MMA7660_RANGE / MMA7660_BOUNDARY)
56
57 #define MMA7660_COUNT_AVERAGE   2
58
59 struct sensor_axis_average {
60                 int x_average;
61                 int y_average;
62                 int z_average;
63                 int count;
64 };
65
66 static struct sensor_axis_average axis_average;
67
68 /****************operate according to sensor chip:start************/
69
70 static int sensor_active(struct i2c_client *client, int enable, int rate)
71 {
72         struct sensor_private_data *sensor =
73             (struct sensor_private_data *) i2c_get_clientdata(client);  
74         int result = 0;
75         int status = 0;
76                 
77         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
78         
79         //register setting according to chip datasheet          
80         if(enable)
81         {       
82                 status = MMA7660_ENABLE;        //mma7660
83                 sensor->ops->ctrl_data |= status;       
84         }
85         else
86         {
87                 status = ~MMA7660_ENABLE;       //mma7660
88                 sensor->ops->ctrl_data &= status;
89         }
90
91         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
92         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
93         if(result)
94                 printk("%s:fail to active sensor\n",__func__);
95         
96         return result;
97
98 }
99
100 static int sensor_init(struct i2c_client *client)
101 {       
102         struct sensor_private_data *sensor =
103             (struct sensor_private_data *) i2c_get_clientdata(client);  
104         int result = 0;
105         
106         result = sensor->ops->active(client,0,0);
107         if(result)
108         {
109                 printk("%s:line=%d,error\n",__func__,__LINE__);
110                 return result;
111         }
112         
113         sensor->status_cur = SENSOR_OFF;
114
115         DBG("%s:MMA7660_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MMA7660_REG_TILT));
116
117         result = sensor_write_reg(client, MMA7660_REG_SR, (0x01<<5)| 0x02);     //32 Samples/Second Active and Auto-Sleep Mode
118         if(result)
119         {
120                 printk("%s:line=%d,error\n",__func__,__LINE__);
121                 return result;
122         }
123
124         if(sensor->pdata->irq_enable)   //open interrupt
125         {
126                 result = sensor_write_reg(client, MMA7660_REG_INTSU, 1<<4);//enable int,GINT=1
127                 if(result)
128                 {
129                         printk("%s:line=%d,error\n",__func__,__LINE__);
130                         return result;
131                 }
132         }
133         
134         sensor->ops->ctrl_data = 1<<6;  //Interrupt output INT is push-pull
135         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
136         if(result)
137         {
138                 printk("%s:line=%d,error\n",__func__,__LINE__);
139                 return result;
140         }
141
142         
143         memset(&axis_average, 0, sizeof(struct sensor_axis_average));
144
145         return result;
146 }
147
148
149 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
150 {
151     s64 result;
152         //struct sensor_private_data *sensor =
153         //    (struct sensor_private_data *) i2c_get_clientdata(client);        
154         //int precision = sensor->ops->precision;
155                 
156         result = (int)low_byte;
157         if (result < MMA7660_BOUNDARY)
158                 result = result* MMA7660_GRAVITY_STEP;
159         else
160                 result = ~( ((~result & (0x7fff>>(16-MMA7660_PRECISION)) ) + 1) 
161                                 * MMA7660_GRAVITY_STEP) + 1;    
162
163         return (int)result;
164 }
165
166 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
167 {
168         struct sensor_private_data *sensor =
169             (struct sensor_private_data *) i2c_get_clientdata(client);  
170
171         /* Report acceleration sensor information */
172         input_report_abs(sensor->input_dev, ABS_X, axis->x);
173         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
174         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
175         input_sync(sensor->input_dev);
176         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
177
178         return 0;
179 }
180
181 #define GSENSOR_MIN             2
182 static int sensor_report_value(struct i2c_client *client)
183 {
184         struct sensor_private_data *sensor =
185                 (struct sensor_private_data *) i2c_get_clientdata(client);      
186         struct sensor_platform_data *pdata = sensor->pdata;
187         int ret = 0;
188         int x,y,z;
189         struct sensor_axis axis;
190         char buffer[3] = {0};   
191         char value = 0;
192         
193         if(sensor->ops->read_len < 3)   //sensor->ops->read_len = 3
194         {
195                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
196                 return -1;
197         }
198         
199         memset(buffer, 0, 3);
200         
201         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
202         do {
203                 *buffer = sensor->ops->read_reg;
204                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
205                 if (ret < 0)
206                 return ret;
207         } while (0);
208
209
210         //this gsensor need 6 bytes buffer
211         x = sensor_convert_data(sensor->client, 0, buffer[0]);  //buffer[1]:high bit 
212         y = sensor_convert_data(sensor->client, 0, buffer[1]);
213         z = sensor_convert_data(sensor->client, 0, buffer[2]);          
214
215         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
216         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; 
217         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
218
219         
220         axis_average.x_average += axis.x;
221         axis_average.y_average += axis.y;
222         axis_average.z_average += axis.z;
223         axis_average.count++;
224         
225         if(axis_average.count >= MMA7660_COUNT_AVERAGE)
226         {
227                 axis.x = axis_average.x_average / axis_average.count;   
228                 axis.y = axis_average.y_average / axis_average.count;   
229                 axis.z = axis_average.z_average / axis_average.count;
230                 
231                 DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
232                 
233                 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
234                 
235                 //Report event only while value is changed to save some power
236                 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))
237                 {
238                         gsensor_report_value(client, &axis);
239
240                         /* »¥³âµØ»º´æÊý¾Ý. */
241                         mutex_lock(&(sensor->data_mutex) );
242                         sensor->axis = axis;
243                         mutex_unlock(&(sensor->data_mutex) );
244                 }
245         }
246         
247         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
248         {
249                 
250                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
251                 DBG("%s:sensor int status :0x%x\n",__func__,value);
252         }
253         
254         return ret;
255 }
256
257
258 struct sensor_operate gsensor_mma7660_ops = {
259         .name                           = "mma7660",
260         .type                           = SENSOR_TYPE_ACCEL,                    //sensor type and it should be correct
261         .id_i2c                         = ACCEL_ID_MMA7660,                     //i2c id number
262         .read_reg                       = MMA7660_REG_X_OUT,                    //read data
263         .read_len                       = 3,                                    //data length
264         .id_reg                         = SENSOR_UNKNOW_DATA,                   //read device id from this register
265         .id_data                        = SENSOR_UNKNOW_DATA,                   //device id
266         .precision                      = MMA7660_PRECISION,                    //12 bit
267         .ctrl_reg                       = MMA7660_REG_MODE,                     //enable or disable     
268         .int_status_reg                 = SENSOR_UNKNOW_DATA,                   //intterupt status register
269         .range                          = {-MMA7660_RANGE,MMA7660_RANGE},       //range
270         .trig                           = IRQF_TRIGGER_LOW|IRQF_ONESHOT,                
271         .active                         = sensor_active,        
272         .init                           = sensor_init,
273         .report                         = sensor_report_value,
274 };
275
276 /****************operate according to sensor chip:end************/
277
278 //function name should not be changed
279 static struct sensor_operate *gsensor_get_ops(void)
280 {
281         return &gsensor_mma7660_ops;
282 }
283
284
285 static int __init gsensor_mma7660_init(void)
286 {
287         struct sensor_operate *ops = gsensor_get_ops();
288         int result = 0;
289         int type = ops->type;
290         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);      
291         return result;
292 }
293
294 static void __exit gsensor_mma7660_exit(void)
295 {
296         struct sensor_operate *ops = gsensor_get_ops();
297         int type = ops->type;
298         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
299 }
300
301
302 module_init(gsensor_mma7660_init);
303 module_exit(gsensor_mma7660_exit);
304
305
306