sensor:mask some debug information
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / mma8452.c
1 /* drivers/input/sensors/access/mma8452.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/mma8452.h>
34 #include <linux/sensor-dev.h>
35
36 #if 0
37 #define SENSOR_DEBUG_TYPE SENSOR_TYPE_ACCEL
38 #define DBG(x...) if(sensor->pdata->type == SENSOR_DEBUG_TYPE) printk(x)
39 #else
40 #define DBG(x...)
41 #endif
42
43
44 #define MMA8451_DEVID           0x1a
45 #define MMA8452_DEVID           0x2a
46 #define MMA8453_DEVID           0x3a
47
48 #define MMA8452_ENABLE          1
49
50
51 /****************operate according to sensor chip:start************/
52
53 static int sensor_active(struct i2c_client *client, int enable, int rate)
54 {
55         struct sensor_private_data *sensor =
56             (struct sensor_private_data *) i2c_get_clientdata(client);  
57         int result = 0;
58         int status = 0;
59                 
60         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
61         
62         //register setting according to chip datasheet          
63         if(enable)
64         {       
65                 status = MMA8452_ENABLE;        //mma8452
66                 sensor->ops->ctrl_data |= status;       
67         }
68         else
69         {
70                 status = ~MMA8452_ENABLE;       //mma8452
71                 sensor->ops->ctrl_data &= status;
72         }
73
74         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
75         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
76         if(result)
77                 printk("%s:fail to active sensor\n",__func__);
78         
79         return result;
80
81 }
82
83 static int sensor_init(struct i2c_client *client)
84 {
85         int tmp;
86         int ret = 0;
87         struct sensor_private_data *sensor =
88             (struct sensor_private_data *) i2c_get_clientdata(client);  
89         
90         ret = sensor->ops->active(client,0,0);
91         if(ret)
92         {
93                 printk("%s:line=%d,error\n",__func__,__LINE__);
94                 return ret;
95         }
96         
97         sensor->status_cur = SENSOR_OFF;
98
99         /* disable FIFO  FMODE = 0*/
100         ret = sensor_write_reg(client,MMA8452_REG_F_SETUP,0);
101         DBG("%s: MMA8452_REG_F_SETUP:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_F_SETUP));
102
103         /* set full scale range to 2g */
104         ret = sensor_write_reg(client,MMA8452_REG_XYZ_DATA_CFG,0);
105         DBG("%s: MMA8452_REG_XYZ_DATA_CFG:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_XYZ_DATA_CFG));
106
107         /* set bus 8bit/14bit(FREAD = 1,FMODE = 0) ,data rate*/
108         tmp = (MMA8452_RATE_12P5<< MMA8452_RATE_SHIFT) | FREAD_MASK;
109         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG1,tmp);
110
111         sensor->ops->ctrl_data = tmp;
112         
113         DBG("mma8452 MMA8452_REG_CTRL_REG1:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG1));
114         
115         DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
116
117         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG3,5);
118         DBG("mma8452 MMA8452_REG_CTRL_REG3:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG3));
119         
120         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG4,1);
121         DBG("mma8452 MMA8452_REG_CTRL_REG4:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG4));
122
123         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG5,1);
124         DBG("mma8452 MMA8452_REG_CTRL_REG5:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG5));        
125
126         DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
127
128         printk("------sensor_chip_init\n");
129         
130         return ret;
131 }
132
133 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
134 {
135     s64 result;
136         struct sensor_private_data *sensor =
137             (struct sensor_private_data *) i2c_get_clientdata(client);  
138         //int precision = sensor->ops->precision;
139         switch (sensor->devid) {
140                 case MMA8451_DEVID:     
141                         swap(high_byte,low_byte);
142                         result = ((int)high_byte << (MMA8451_PRECISION-8)) 
143                                         | ((int)low_byte >> (16-MMA8451_PRECISION));
144                         if (result < MMA8451_BOUNDARY)
145                                 result = result* MMA8451_GRAVITY_STEP;
146                         else
147                                 result = ~( ((~result & (0x7fff>>(16-MMA8451_PRECISION)) ) + 1) 
148                                                 * MMA8451_GRAVITY_STEP) + 1;
149                         break;
150
151                 case MMA8452_DEVID:                     
152                         swap(high_byte,low_byte);
153                         result = ((int)high_byte << (MMA8452_PRECISION-8)) 
154                                         | ((int)low_byte >> (16-MMA8452_PRECISION));
155                         if (result < MMA8452_BOUNDARY)
156                                 result = result* MMA8452_GRAVITY_STEP;
157                         else
158                                 result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1) 
159                                                 * MMA8452_GRAVITY_STEP) + 1;
160                         break;
161                         
162                 case MMA8453_DEVID:             
163                         swap(high_byte,low_byte);
164                         result = ((int)high_byte << (MMA8453_PRECISION-8)) 
165                                         | ((int)low_byte >> (16-MMA8453_PRECISION));
166                         if (result < MMA8453_BOUNDARY)
167                                 result = result* MMA8453_GRAVITY_STEP;
168                         else
169                                 result = ~( ((~result & (0x7fff>>(16-MMA8453_PRECISION)) ) + 1) 
170                                                 * MMA8453_GRAVITY_STEP) + 1;
171                         break;
172
173                 default:
174                         printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
175                         return -EFAULT;
176     }
177
178     return (int)result;
179 }
180
181 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
182 {
183         struct sensor_private_data *sensor =
184             (struct sensor_private_data *) i2c_get_clientdata(client);  
185
186         /* Report acceleration sensor information */
187         input_report_abs(sensor->input_dev, ABS_X, axis->x);
188         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
189         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
190         input_sync(sensor->input_dev);
191         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
192
193         return 0;
194 }
195
196 #define GSENSOR_MIN             10
197 static int sensor_report_value(struct i2c_client *client)
198 {
199         struct sensor_private_data *sensor =
200                 (struct sensor_private_data *) i2c_get_clientdata(client);      
201         struct sensor_platform_data *pdata = sensor->pdata;
202         int ret = 0;
203         int x,y,z;
204         struct sensor_axis axis;
205         char buffer[6] = {0};   
206         char value = 0;
207         
208         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
209         {
210                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
211                 return -1;
212         }
213         
214         memset(buffer, 0, 6);
215         
216         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
217         do {
218                 *buffer = sensor->ops->read_reg;
219                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
220                 if (ret < 0)
221                 return ret;
222         } while (0);
223
224
225         //this gsensor need 6 bytes buffer
226         x = sensor_convert_data(sensor->client, buffer[1], buffer[0]);  //buffer[1]:high bit 
227         y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
228         z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);          
229
230         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
231         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; 
232         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
233
234         DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
235
236         //Report event only while value is changed to save some power
237         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))
238         {
239                 gsensor_report_value(client, &axis);
240
241                 /* »¥³âµØ»º´æÊý¾Ý. */
242                 mutex_lock(&(sensor->data_mutex) );
243                 sensor->axis = axis;
244                 mutex_unlock(&(sensor->data_mutex) );
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_mma8452_ops = {
259         .name                           = "mma8452",
260         .type                           = SENSOR_TYPE_ACCEL,                    //sensor type and it should be correct
261         .id_i2c                         = ACCEL_ID_MMA845X,                     //i2c id number
262         .read_reg                       = MMA8452_REG_X_OUT_MSB,                //read data
263         .read_len                       = 6,                                    //data length
264         .id_reg                         = MMA8452_REG_WHO_AM_I,                 //read device id from this register
265         .id_data                        = MMA8452_DEVID,                        //device id
266         .precision                      = MMA8452_PRECISION,                    //12 bit
267         .ctrl_reg                       = MMA8452_REG_CTRL_REG1,                //enable or disable     
268         .int_status_reg                 = MMA8452_REG_INTSRC,                   //intterupt status register
269         .range                          = {-MMA845X_RANGE,MMA845X_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_mma8452_ops;
282 }
283
284
285 static int __init gsensor_mma8452_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         DBG("%s\n",__func__);
292         return result;
293 }
294
295 static void __exit gsensor_mma8452_exit(void)
296 {
297         struct sensor_operate *ops = gsensor_get_ops();
298         int type = ops->type;
299         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
300 }
301
302
303 module_init(gsensor_mma8452_init);
304 module_exit(gsensor_mma8452_exit);
305
306
307