5a828e723bccaead6943613106ba9e51d93fd370
[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 <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33
34 /* Default register settings */
35 #define RBUFF_SIZE              12      /* Rx buffer size */
36
37 #define MMA8452_REG_STATUS              0x0 //RO
38 #define MMA8452_REG_X_OUT_MSB           0x1 //RO
39 #define MMA8452_REG_X_OUT_LSB           0x2 //RO
40 #define MMA8452_REG_Y_OUT_MSB           0x3 //RO
41 #define MMA8452_REG_Y_OUT_LSB           0x4 //RO
42 #define MMA8452_REG_Z_OUT_MSB           0x5 //RO
43 #define MMA8452_REG_Z_OUT_LSB           0x6 //RO
44 #define MMA8452_REG_F_SETUP                     0x9 //RW
45
46 #define MMA8452_REG_SYSMOD                              0xB //RO
47 #define MMA8452_REG_INTSRC                      0xC //RO
48 #define MMA8452_REG_WHO_AM_I                    0xD //RO
49 #define MMA8452_REG_XYZ_DATA_CFG                0xE //RW
50 #define MMA8452_REG_HP_FILTER_CUTOFF    0xF //RW
51 #define MMA8452_REG_PL_STATUS                   0x10 //RO
52 #define MMA8452_REG_PL_CFG                              0x11 //RW
53 #define MMA8452_REG_PL_COUNT                    0x12 //RW
54 #define MMA8452_REG_PL_BF_ZCOMP                 0x13 //RW
55 #define MMA8452_REG_P_L_THS_REG                 0x14 //RW
56 #define MMA8452_REG_FF_MT_CFG                   0x15 //RW
57 #define MMA8452_REG_FF_MT_SRC                   0x16 //RO
58 #define MMA8452_REG_FF_MT_THS                   0x17 //RW
59 #define MMA8452_REG_FF_MT_COUNT                 0x18 //RW
60 #define MMA8452_REG_TRANSIENT_CFG               0x1D //RW
61 #define MMA8452_REG_TRANSIENT_SRC               0x1E //RO
62 #define MMA8452_REG_TRANSIENT_THS               0x1F //RW
63 #define MMA8452_REG_TRANSIENT_COUNT             0x20 //RW
64 #define MMA8452_REG_PULSE_CFG                   0x21 //RW
65 #define MMA8452_REG_PULSE_SRC                   0x22 //RO
66 #define MMA8452_REG_PULSE_THSX                  0x23 //RW
67 #define MMA8452_REG_PULSE_THSY                  0x24 //RW
68 #define MMA8452_REG_PULSE_THSZ                  0x25 //RW
69 #define MMA8452_REG_PULSE_TMLT                  0x26 //RW
70 #define MMA8452_REG_PULSE_LTCY                  0x27 //RW
71 #define MMA8452_REG_PULSE_WIND                  0x28 //RW
72 #define MMA8452_REG_ASLP_COUNT                  0x29 //RW
73 #define MMA8452_REG_CTRL_REG1                   0x2A //RW
74 #define MMA8452_REG_CTRL_REG2                   0x2B //RW
75 #define MMA8452_REG_CTRL_REG3                   0x2C //RW
76 #define MMA8452_REG_CTRL_REG4                   0x2D //RW
77 #define MMA8452_REG_CTRL_REG5                   0x2E //RW
78 #define MMA8452_REG_OFF_X                               0x2F //RW
79 #define MMA8452_REG_OFF_Y                               0x30 //RW
80 #define MMA8452_REG_OFF_Z                               0x31 //RW
81
82 /*rate*/
83 #define MMA8452_RATE_800          0
84 #define MMA8452_RATE_400          1
85 #define MMA8452_RATE_200          2
86 #define MMA8452_RATE_100          3
87 #define MMA8452_RATE_50           4
88 #define MMA8452_RATE_12P5         5
89 #define MMA8452_RATE_6P25         6
90 #define MMA8452_RATE_1P56         7
91 #define MMA8452_RATE_SHIFT                3
92
93
94 #define MMA8452_ASLP_RATE_50          0
95 #define MMA8452_ASLP_RATE_12P5        1
96 #define MMA8452_ASLP_RATE_6P25        2
97 #define MMA8452_ASLP_RATE_1P56        3
98 #define MMA8452_ASLP_RATE_SHIFT           6
99
100 /*Auto-adapt mma845x series*/
101 /*Modified by Yick @ROCKCHIP 
102   xieyi@rockchips.com*/
103 /*
104   Range: unit(ug 1g=1 000 000 ug)
105                  option(2g,4g,8g)
106                  G would be defined on android HAL
107   Precision: bit wide of valid data
108   Boundary: Max positive count
109   Gravity_step: gravity value indicated by per count
110  */
111 #define FREAD_MASK                              0 /* enabled(1<<1) only if reading MSB 8bits*/
112 #define MMA845X_RANGE                   2000000
113 /* mma8451 */
114 #define MMA8451_PRECISION       14
115 #define MMA8451_BOUNDARY        (0x1 << (MMA8451_PRECISION - 1))
116 #define MMA8451_GRAVITY_STEP    MMA845X_RANGE / MMA8451_BOUNDARY
117
118 /* mma8452 */
119 #define MMA8452_PRECISION       12
120 #define MMA8452_BOUNDARY        (0x1 << (MMA8452_PRECISION - 1))
121 #define MMA8452_GRAVITY_STEP    MMA845X_RANGE / MMA8452_BOUNDARY
122
123 /* mma8453 */
124 #define MMA8453_PRECISION       10
125 #define MMA8453_BOUNDARY        (0x1 << (MMA8453_PRECISION - 1))
126 #define MMA8453_GRAVITY_STEP    MMA845X_RANGE / MMA8453_BOUNDARY
127
128 /* mma8653 */
129 #define MMA8653_PRECISION       10
130 #define MMA8653_BOUNDARY        (0x1 << (MMA8653_PRECISION - 1))
131 #define MMA8653_GRAVITY_STEP    MMA845X_RANGE / MMA8653_BOUNDARY
132
133
134 #define MMA8451_DEVID           0x1a
135 #define MMA8452_DEVID           0x2a
136 #define MMA8453_DEVID           0x3a
137 #define MMA8653_DEVID           0x5a
138
139
140 #define MMA8452_ENABLE          1
141
142
143 /****************operate according to sensor chip:start************/
144
145 static int sensor_active(struct i2c_client *client, int enable, int rate)
146 {
147         struct sensor_private_data *sensor =
148             (struct sensor_private_data *) i2c_get_clientdata(client);  
149         int result = 0;
150         int status = 0;
151                 
152         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
153         
154         //register setting according to chip datasheet          
155         if(enable)
156         {       
157                 status = MMA8452_ENABLE;        //mma8452
158                 sensor->ops->ctrl_data |= status;       
159         }
160         else
161         {
162                 status = ~MMA8452_ENABLE;       //mma8452
163                 sensor->ops->ctrl_data &= status;
164         }
165
166         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
167         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
168         if(result)
169                 printk("%s:fail to active sensor\n",__func__);
170         
171         return result;
172
173 }
174
175 static int sensor_init(struct i2c_client *client)
176 {
177         int tmp;
178         int ret = 0; 
179         int i = 0;      
180         unsigned char id_reg = MMA8452_REG_WHO_AM_I;
181         unsigned char id_data = 0;
182         struct sensor_private_data *sensor =
183             (struct sensor_private_data *) i2c_get_clientdata(client);  
184         
185         ret = sensor->ops->active(client,0,0);
186         if(ret)
187         {
188                 printk("%s:line=%d,error\n",__func__,__LINE__);
189                 return ret;
190         }
191         
192         sensor->status_cur = SENSOR_OFF;
193
194         for(i=0; i<3; i++)
195         {
196                 ret = sensor_rx_data(client, &id_reg, 1);
197                 id_data = id_reg;
198                 if(!ret)
199                 break;
200         }
201
202         if(ret)
203         {
204                 printk("%s:fail to read id,ret=%d\n",__func__, ret);
205                 return ret;
206         }
207
208         sensor->devid = id_data;
209
210         /* disable FIFO  FMODE = 0*/
211         ret = sensor_write_reg(client,MMA8452_REG_F_SETUP,0);
212         DBG("%s: MMA8452_REG_F_SETUP:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_F_SETUP));
213
214         /* set full scale range to 2g */
215         ret = sensor_write_reg(client,MMA8452_REG_XYZ_DATA_CFG,0);
216         DBG("%s: MMA8452_REG_XYZ_DATA_CFG:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_XYZ_DATA_CFG));
217
218         /* set bus 8bit/14bit(FREAD = 1,FMODE = 0) ,data rate*/
219         tmp = (MMA8452_RATE_12P5<< MMA8452_RATE_SHIFT) | FREAD_MASK;
220         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG1,tmp);
221
222         sensor->ops->ctrl_data = tmp;
223         
224         DBG("mma8452 MMA8452_REG_CTRL_REG1:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG1));
225         
226         DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
227
228         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG3,5);
229         DBG("mma8452 MMA8452_REG_CTRL_REG3:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG3));
230         
231         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG4,1);
232         DBG("mma8452 MMA8452_REG_CTRL_REG4:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG4));
233
234         ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG5,1);
235         DBG("mma8452 MMA8452_REG_CTRL_REG5:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG5));        
236
237         DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
238
239         return ret;
240 }
241
242 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
243 {
244     s64 result;
245         struct sensor_private_data *sensor =
246             (struct sensor_private_data *) i2c_get_clientdata(client);  
247         //int precision = sensor->ops->precision;
248         switch (sensor->devid) {
249                 case MMA8451_DEVID:     
250                         swap(high_byte,low_byte);
251                         result = ((int)high_byte << (MMA8451_PRECISION-8)) 
252                                         | ((int)low_byte >> (16-MMA8451_PRECISION));
253                         if (result < MMA8451_BOUNDARY)
254                                 result = result* MMA8451_GRAVITY_STEP;
255                         else
256                                 result = ~( ((~result & (0x7fff>>(16-MMA8451_PRECISION)) ) + 1) 
257                                                 * MMA8451_GRAVITY_STEP) + 1;
258                         break;
259
260                 case MMA8452_DEVID:                     
261                         swap(high_byte,low_byte);
262                         result = ((int)high_byte << (MMA8452_PRECISION-8)) 
263                                         | ((int)low_byte >> (16-MMA8452_PRECISION));
264                         if (result < MMA8452_BOUNDARY)
265                                 result = result* MMA8452_GRAVITY_STEP;
266                         else
267                                 result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1) 
268                                                 * MMA8452_GRAVITY_STEP) + 1;
269                         break;
270                         
271                 case MMA8453_DEVID:
272                         swap(high_byte,low_byte);
273                         result = ((int)high_byte << (MMA8453_PRECISION-8)) 
274                                         | ((int)low_byte >> (16-MMA8453_PRECISION));
275                         if (result < MMA8453_BOUNDARY)
276                                 result = result* MMA8453_GRAVITY_STEP;
277                         else
278                                 result = ~( ((~result & (0x7fff>>(16-MMA8453_PRECISION)) ) + 1) 
279                                                 * MMA8453_GRAVITY_STEP) + 1;
280                         break;
281
282                 case MMA8653_DEVID:
283                         swap(high_byte,low_byte);
284                         result = ((int)high_byte << (MMA8653_PRECISION-8)) 
285                                         | ((int)low_byte >> (16-MMA8653_PRECISION));
286                         if (result < MMA8653_BOUNDARY)
287                                 result = result* MMA8653_GRAVITY_STEP;
288                         else
289                                 result = ~( ((~result & (0x7fff>>(16-MMA8653_PRECISION)) ) + 1) 
290                                                 * MMA8653_GRAVITY_STEP) + 1;
291                         break;
292
293                 default:
294                         printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
295                         return -EFAULT;
296     }
297
298     return (int)result;
299 }
300
301 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
302 {
303         struct sensor_private_data *sensor =
304             (struct sensor_private_data *) i2c_get_clientdata(client);  
305
306         /* Report acceleration sensor information */
307         input_report_abs(sensor->input_dev, ABS_X, axis->x);
308         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
309         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
310         input_sync(sensor->input_dev);
311         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
312
313         return 0;
314 }
315
316 #define GSENSOR_MIN             10
317 static int sensor_report_value(struct i2c_client *client)
318 {
319         struct sensor_private_data *sensor =
320                 (struct sensor_private_data *) i2c_get_clientdata(client);      
321         struct sensor_platform_data *pdata = sensor->pdata;
322         int ret = 0;
323         int x,y,z;
324         struct sensor_axis axis;
325         char buffer[6] = {0};   
326         char value = 0;
327         
328         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
329         {
330                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
331                 return -1;
332         }
333         
334         memset(buffer, 0, 6);
335         
336         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
337         do {
338                 *buffer = sensor->ops->read_reg;
339                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
340                 if (ret < 0)
341                 return ret;
342         } while (0);
343
344
345         //this gsensor need 6 bytes buffer
346         x = sensor_convert_data(sensor->client, buffer[1], buffer[0]);  //buffer[1]:high bit 
347         y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
348         z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);          
349
350         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
351         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; 
352         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
353
354         DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
355
356         //Report event only while value is changed to save some power
357         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))
358         {
359                 gsensor_report_value(client, &axis);
360
361                 /* »¥³âµØ»º´æÊý¾Ý. */
362                 mutex_lock(&(sensor->data_mutex) );
363                 sensor->axis = axis;
364                 mutex_unlock(&(sensor->data_mutex) );
365         }
366
367         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
368         {
369                 
370                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
371                 DBG("%s:sensor int status :0x%x\n",__func__,value);
372         }
373         
374         return ret;
375 }
376
377
378 struct sensor_operate gsensor_mma8452_ops = {
379         .name                           = "mma8452",
380         .type                           = SENSOR_TYPE_ACCEL,                    //sensor type and it should be correct
381         .id_i2c                         = ACCEL_ID_MMA845X,                             //i2c id number
382         .read_reg                       = MMA8452_REG_X_OUT_MSB,                //read data
383         .read_len                       = 6,                                                    //data length
384         .id_reg                         = SENSOR_UNKNOW_DATA,                   //read device id from this register
385         .id_data                        = SENSOR_UNKNOW_DATA,                   //device id
386         .precision                      = MMA8452_PRECISION,                    //12 bit
387         .ctrl_reg                       = MMA8452_REG_CTRL_REG1,                //enable or disable     
388         .int_status_reg                 = MMA8452_REG_INTSRC,                   //intterupt status register
389         .range                          = {-MMA845X_RANGE,MMA845X_RANGE},       //range
390         .trig                           = IRQF_TRIGGER_LOW|IRQF_ONESHOT,                
391         .active                         = sensor_active,        
392         .init                           = sensor_init,
393         .report                         = sensor_report_value,
394 };
395
396 /****************operate according to sensor chip:end************/
397
398 //function name should not be changed
399 static struct sensor_operate *gsensor_get_ops(void)
400 {
401         return &gsensor_mma8452_ops;
402 }
403
404
405 static int __init gsensor_mma8452_init(void)
406 {
407         struct sensor_operate *ops = gsensor_get_ops();
408         int result = 0;
409         int type = ops->type;
410         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);      
411         return result;
412 }
413
414 static void __exit gsensor_mma8452_exit(void)
415 {
416         struct sensor_operate *ops = gsensor_get_ops();
417         int type = ops->type;
418         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
419 }
420
421
422 module_init(gsensor_mma8452_init);
423 module_exit(gsensor_mma8452_exit);
424
425
426