input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / lsm303d.c
1 /* drivers/input/sensors/access/kxtik.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: Bruins <xwj@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
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/irq.h>
21 #include <linux/miscdevice.h>
22 #include <linux/gpio.h>
23 #include <asm/uaccess.h>
24 #include <asm/atomic.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/workqueue.h>
28 #include <linux/freezer.h>
29 #include <linux/of_gpio.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34
35
36 #define LSM303D_WHO_AM_I                (0x0F)
37
38 /* full scale setting - register & mask */
39 #define LSM303D_CTRL_REG0               (0x1F)
40 #define LSM303D_CTRL_REG1               (0x20)
41 #define LSM303D_CTRL_REG2               (0x21)
42 #define LSM303D_CTRL_REG3               (0x22)
43 #define LSM303D_CTRL_REG4               (0x23)
44 #define LSM303D_CTRL_REG5               (0x24)
45 #define LSM303D_CTRL_REG6               (0x25)
46 #define LSM303D_CTRL_REG7               (0x26)
47 #define LSM303D_STATUS_REG              (0x27)
48 #define LSM303D_OUT_X_L                 (0x28)
49 #define LSM303D_OUT_X_H                 (0x29)
50 #define LSM303D_OUT_Y_L                 (0x2a)
51 #define LSM303D_OUT_Y_H                 (0x2b)
52 #define LSM303D_OUT_Z_L                 (0x2c)
53 #define LSM303D_OUT_Z_H                 (0x2d)
54 #define LSM303D_FIFO_CTRL_REG           (0x2E)
55 #define LSM303D_FIFO_SRC_REG            (0X2F)
56
57 #define LSM303D_IG_CFG1                 (0x30)
58 #define LSM303D_IG_SRC1                 (0x31)
59 #define LSM303D_IG_THS1                 (0x32)
60 #define LSM303D_IG_DURATION1            (0x33)
61
62 #define LSM303D_IG_CFG2                 (0x34)
63 #define LSM303D_IG_SRC2                 (0x35)
64 #define LSM303D_IG_THS2                 (0x36)
65 #define LSM303D_IG_DURATION2            (0x37)
66
67
68 #define LSM303D_DEVID                   (0x49)  //chip id
69 #define LSM303D_ACC_DISABLE             (0x08)
70
71 #define LSM303D_RANGE                   2000000
72
73 /* LSM303D */
74 #define LSM303D_PRECISION               16
75 #define LSM303D_BOUNDARY                        (0x1 << (LSM303D_PRECISION - 1))
76 #define LSM303D_GRAVITY_STEP            (LSM303D_RANGE / LSM303D_BOUNDARY)
77
78 #define ODR3P25                         0x10  /* 3.25Hz output data rate */
79 #define ODR6P25                         0x20  /* 6.25Hz output data rate */
80 #define ODR12P5                         0x30  /* 12.5Hz output data rate */
81 #define ODR25                           0x40  /* 25Hz output data rate */
82 #define ODR50                           0x50  /* 50Hz output data rate */
83 #define ODR100                          0x60  /* 100Hz output data rate */
84 #define ODR200                          0x70  /* 200Hz output data rate */
85 #define ODR400                          0x80  /* 400Hz output data rate */
86 #define ODR800                          0x90  /* 800Hz output data rate */
87 #define ODR1600                         0xA0  /* 1600Hz output data rate */
88
89
90 struct sensor_reg_data {
91         char reg;
92         char data;
93 };
94
95 /****************operate according to sensor chip:start************/
96 static int sensor_active(struct i2c_client *client, int enable, int rate)
97 {
98         struct sensor_private_data *sensor =
99             (struct sensor_private_data *) i2c_get_clientdata(client);
100         int result = 0;
101         int status = 0;
102
103         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
104
105         sensor->ops->ctrl_data |= ODR100;       //100HZ,if 0 then power down
106
107         //register setting according to chip datasheet
108         if(!enable)
109         {
110                 status = LSM303D_ACC_DISABLE;   //lis3dh
111                 sensor->ops->ctrl_data |= status;
112         }
113         else
114         {
115                 status = ~LSM303D_ACC_DISABLE;  //lis3dh
116                 sensor->ops->ctrl_data &= status;
117         }
118
119         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
120         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
121         if(result)
122                 printk("%s:fail to active sensor\n",__func__);
123
124         return result;
125
126 }
127
128
129 static int sensor_init(struct i2c_client *client)
130 {
131         struct sensor_private_data *sensor =
132             (struct sensor_private_data *) i2c_get_clientdata(client);
133         int result = 0;
134         int i;
135
136         struct sensor_reg_data reg_data[] =
137         {
138                 {LSM303D_CTRL_REG0,0x00},
139                 {LSM303D_CTRL_REG1,0x07},
140                 {LSM303D_CTRL_REG2,0x00},
141                 {LSM303D_CTRL_REG3,0x00},
142                 {LSM303D_CTRL_REG4,0x00},
143                 {LSM303D_CTRL_REG5,0x78},               //High resolution output mode:11,
144                 {LSM303D_CTRL_REG6,0x20},
145                 {LSM303D_CTRL_REG7,0x00},
146                 {LSM303D_FIFO_CTRL_REG,0x00},
147                 {LSM303D_IG_CFG1,0xFF},         //6 direction position recognition
148                 {LSM303D_IG_THS1,0x7F},         //Interrupt 1 threshold
149                 {LSM303D_IG_DURATION1,0x7F},    //Duration value 0x00->ox7f
150
151         /*
152                 {LSM303D_CTRL_REG7,0x00},
153                 {LSM303D_CTRL_REG4,0x08},               //High resolution output mode: 1, Normal mode
154                 {LSM303D_CTRL_REG6,0x40},
155
156                 {LSM303D_FIFO_CTRL_REG,0x00},   //
157                 {LSM303D_IG_CFG1,0xFF},                 //6 direction position recognition
158                 {LSM303D_IG_THS1,0x7F},                 //Interrupt 1 threshold
159                 {LSM303D_IG_DURATION1,0x7F},    //Duration value 0x00->ox7f
160                 */
161         };
162
163         result = sensor->ops->active(client,0,0);
164         if(result)
165         {
166                 printk("%s:line=%d,error\n",__func__,__LINE__);
167                 return result;
168         }
169
170         sensor->status_cur = SENSOR_OFF;
171
172         for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
173         {
174                 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
175                 if(result)
176                 {
177                         printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
178                         return result;
179                 }
180         }
181
182
183         if(sensor->pdata->irq_enable)
184         {
185
186                 result = sensor_write_reg(client, LSM303D_CTRL_REG3, 0x20);
187                 if(result)
188                 {
189                         printk("%s:line=%d,error\n",__func__,__LINE__);
190                         return result;
191                 }
192
193                 i = sensor_read_reg(client,LSM303D_CTRL_REG5);
194
195                 result = sensor_write_reg(client, LSM303D_CTRL_REG5, (i|0x01));
196                 if(result)
197                 {
198                         printk("%s:line=%d,error\n",__func__,__LINE__);
199                         return result;
200                 }
201
202         }
203
204         return result;
205 }
206
207
208 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
209 {
210         s64 result;
211         struct sensor_private_data *sensor =
212             (struct sensor_private_data *) i2c_get_clientdata(client);
213
214         switch (sensor->devid) {
215                 case LSM303D_DEVID:
216                         result = ((int)high_byte << 8) | (int)low_byte;
217                         if (result < LSM303D_BOUNDARY)
218                         result = result* LSM303D_GRAVITY_STEP;
219                 else
220                         result = ~( ((~result & (0x7fff>>(16-LSM303D_PRECISION)) ) + 1)
221                                                 * LSM303D_GRAVITY_STEP) + 1;
222                         break;
223
224                 default:
225                         printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
226                         return -EFAULT;
227     }
228
229     return (int)result;
230 }
231
232 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
233 {
234         struct sensor_private_data *sensor =
235                 (struct sensor_private_data *) i2c_get_clientdata(client);
236
237         /* Report acceleration sensor information */
238         input_report_abs(sensor->input_dev, ABS_X, axis->x);
239         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
240         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
241         input_sync(sensor->input_dev);
242         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
243
244         return 0;
245 }
246
247 #define GSENSOR_MIN  10
248 static int sensor_report_value(struct i2c_client *client)
249 {
250         struct sensor_private_data *sensor =
251                         (struct sensor_private_data *) i2c_get_clientdata(client);
252         struct sensor_platform_data *pdata = sensor->pdata;
253         int ret = 0;
254         int x,y,z;
255         struct sensor_axis axis;
256         char buffer[6] = {0};
257         char value = 0;
258
259         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6
260         {
261                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
262                 return -1;
263         }
264
265         memset(buffer, 0, 6);
266
267         value = sensor_read_reg(client, LSM303D_STATUS_REG);
268         if((value & 0x0f) == 0)
269         {
270                 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
271                 return -1;
272         }
273
274
275         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
276         do {
277                 *buffer = sensor->ops->read_reg;
278                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
279                 if (ret < 0)
280                 return ret;
281         } while (0);
282
283         //this gsensor need 6 bytes buffer
284         x = sensor_convert_data(sensor->client, buffer[1], buffer[0]);  //buffer[1]:high bit
285         y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
286         z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
287
288         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
289         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
290         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
291
292         DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
293         //printk( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
294
295         //Report event  only while value is changed to save some power
296         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))
297         {
298                 gsensor_report_value(client, &axis);
299
300                 /* »¥³âµØ»º´æÊý¾Ý. */
301                 mutex_lock(&(sensor->data_mutex) );
302                 sensor->axis = axis;
303                 mutex_unlock(&(sensor->data_mutex) );
304         }
305
306         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
307         {
308
309                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
310                 DBG("%s:sensor int status :0x%x\n",__func__,value);
311         }
312
313         return ret;
314 }
315
316 struct sensor_operate gsensor_lsm303d_ops = {
317         .name                           = "lsm303d",
318         .type                           = SENSOR_TYPE_ACCEL,            //sensor type and it should be correct
319         .id_i2c                         = ACCEL_ID_LSM303D,             //i2c id number
320         .read_reg                       = (LSM303D_OUT_X_L | 0x80),     //read data
321         .read_len                       = 6,                            //data length
322         .id_reg                         = LSM303D_WHO_AM_I,             //read device id from this register
323         .id_data                        = LSM303D_DEVID,                        //device id
324         .precision                      = LSM303D_PRECISION,            //16 bits
325         .ctrl_reg                       = LSM303D_CTRL_REG1,            //enable or disable
326         .int_status_reg                 = LSM303D_IG_SRC1,              //intterupt status register
327         .range                          = {-LSM303D_RANGE,LSM303D_RANGE},       //range
328         .trig                           = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),
329         .active                         = sensor_active,
330         .init                           = sensor_init,
331         .report                         = sensor_report_value,
332 };
333
334
335 /****************operate according to sensor chip:end************/
336
337 //function name should not be changed
338 static struct sensor_operate *gsensor_get_ops(void)
339 {
340         return &gsensor_lsm303d_ops;
341 }
342
343
344 static int __init gsensor_lis3dh_init(void)
345 {
346         struct sensor_operate *ops = gsensor_get_ops();
347         int result = 0;
348         int type = ops->type;
349         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
350         return result;
351 }
352
353 static void __exit gsensor_lis3dh_exit(void)
354 {
355         struct sensor_operate *ops = gsensor_get_ops();
356         int type = ops->type;
357         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
358 }
359
360
361 module_init(gsensor_lis3dh_init);
362 module_exit(gsensor_lis3dh_exit);
363
364
365
366