11870cd828dc2d6c38651e5901ae2eb1f4ab3b5b
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / gyro / l3g4200d.c
1 /* drivers/input/sensors/access/kxtik.c\r
2  *\r
3  * Copyright (C) 2012-2015 ROCKCHIP.\r
4  * Author: luowei <lw@rock-chips.com>\r
5  *\r
6  * This software is licensed under the terms of the GNU General Public\r
7  * License version 2, as published by the Free Software Foundation, and\r
8  * may be copied, distributed, and modified under those terms.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  */\r
16 #include <linux/interrupt.h>\r
17 #include <linux/i2c.h>\r
18 #include <linux/slab.h>\r
19 #include <linux/irq.h>\r
20 #include <linux/miscdevice.h>\r
21 #include <linux/gpio.h>\r
22 #include <asm/uaccess.h>\r
23 #include <asm/atomic.h>\r
24 #include <linux/delay.h>\r
25 #include <linux/input.h>\r
26 #include <linux/workqueue.h>\r
27 #include <linux/freezer.h>\r
28 #include <mach/gpio.h>\r
29 #include <mach/board.h> \r
30 #ifdef CONFIG_HAS_EARLYSUSPEND\r
31 #include <linux/earlysuspend.h>\r
32 #endif\r
33 #include <linux/l3g4200d.h>\r
34 #include <linux/sensor-dev.h>\r
35 \r
36 \r
37 #define L3G4200D_ENABLE                 0x08\r
38 \r
39 /****************operate according to sensor chip:start************/\r
40 \r
41 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
42 {\r
43         struct sensor_private_data *sensor =\r
44             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
45         int result = 0;\r
46         int status = 0;\r
47                 \r
48         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
49 \r
50         //register setting according to chip datasheet          \r
51         if(enable)\r
52         {       \r
53                 status = L3G4200D_ENABLE;       //l3g4200d      \r
54                 sensor->ops->ctrl_data |= status;       \r
55         }\r
56         else\r
57         {\r
58                 status = ~L3G4200D_ENABLE;      //l3g4200d\r
59                 sensor->ops->ctrl_data &= status;\r
60         }\r
61 \r
62         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
63         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
64         if(result)\r
65                 printk("%s:fail to active sensor\n",__func__);\r
66 \r
67         return result;\r
68 \r
69 }\r
70 \r
71 static int sensor_init(struct i2c_client *client)\r
72 {       \r
73         struct sensor_private_data *sensor =\r
74             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
75         int result = 0; \r
76         unsigned char buf[5];           \r
77         unsigned char data = 0;\r
78         int i = 0;\r
79         \r
80         result = sensor->ops->active(client,0,0);\r
81         if(result)\r
82         {\r
83                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
84                 return result;\r
85         }\r
86         \r
87         sensor->status_cur = SENSOR_OFF;\r
88         \r
89         buf[0] = 0x07;  //27\r
90         buf[1] = 0x00;  \r
91         buf[2] = 0x00;  \r
92         buf[3] = 0x20;  //0x00\r
93         buf[4] = 0x00;  \r
94         for(i=0; i<5; i++)\r
95         {\r
96                 result = sensor_write_reg(client, sensor->ops->ctrl_reg+i, buf[i]);\r
97                 if(result)\r
98                 {\r
99                         printk("%s:line=%d,error\n",__func__,__LINE__);\r
100                         return result;\r
101                 }\r
102         }\r
103         \r
104         result = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
105         if (result >= 0)\r
106                 data = result & 0x000F;\r
107
108         sensor->ops->ctrl_data = data + ODR100_BW12_5;  \r
109         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
110         if(result)\r
111         {\r
112                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
113                 return result;\r
114         }\r
115         \r
116         return result;\r
117 }\r
118 \r
119 \r
120 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)\r
121 {\r
122         struct sensor_private_data *sensor =\r
123                 (struct sensor_private_data *) i2c_get_clientdata(client);      \r
124 \r
125         /* Report GYRO  information */\r
126         input_report_rel(sensor->input_dev, ABS_RX, axis->x);\r
127         input_report_rel(sensor->input_dev, ABS_RY, axis->y);\r
128         input_report_rel(sensor->input_dev, ABS_RZ, axis->z);\r
129         input_sync(sensor->input_dev);\r
130         DBG("gyro x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);\r
131 \r
132         return 0;\r
133 }\r
134 \r
135 static int sensor_report_value(struct i2c_client *client)\r
136 {\r
137         struct sensor_private_data *sensor =\r
138                 (struct sensor_private_data *) i2c_get_clientdata(client);      \r
139         struct sensor_platform_data *pdata = sensor->pdata;\r
140         int ret = 0;\r
141         int x = 0, y = 0, z = 0;\r
142         struct sensor_axis axis;\r
143         char buffer[6] = {0};   \r
144         int i = 0;\r
145         int value = 0;\r
146         \r
147         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6\r
148         {\r
149                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);\r
150                 return -1;\r
151         }\r
152         \r
153         memset(buffer, 0, 6);\r
154 #if 0   \r
155         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   \r
156         do {\r
157                 buffer[0] = sensor->ops->read_reg;\r
158                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);\r
159                 if (ret < 0)\r
160                 return ret;\r
161         } while (0);\r
162 #else\r
163 \r
164         for(i=0; i<6; i++)\r
165         {
166                 //buffer[i] = sensor->ops->read_reg + i;        \r
167                 buffer[i] = sensor_read_reg(client, sensor->ops->read_reg + i);\r
168         }\r
169 #endif\r
170         x = (short) (((buffer[1]) << 8) | buffer[0]);\r
171         y = (short) (((buffer[3]) << 8) | buffer[2]);\r
172         z = (short) (((buffer[5]) << 8) | buffer[4]);\r
173
174         DBG("%s: x=%d  y=%d z=%d \n",__func__, x,y,z);
175         if(pdata && pdata->orientation)
176         {
177                 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
178                 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;     
179                 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
180         }
181         else
182         {
183                 axis.x = x;     
184                 axis.y = y;
185                 axis.z = z;     
186         }
187
188         //filter gyro data
189         if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))\r
190         {       \r
191                 gyro_report_value(client, &axis);       \r
192 \r
193                  /* »¥³âµØ»º´æÊý¾Ý. */\r
194                 mutex_lock(&(sensor->data_mutex) );\r
195                 sensor->axis = axis;\r
196                 mutex_unlock(&(sensor->data_mutex) );\r
197         }       \r
198 \r
199         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register\r
200         {\r
201                 \r
202                 value = sensor_read_reg(client, sensor->ops->int_status_reg);\r
203                 DBG("%s:sensor int status :0x%x\n",__func__,value);\r
204         }\r
205         \r
206         return ret;\r
207 }\r
208 \r
209 \r
210 struct sensor_operate gyro_l3g4200d_ops = {\r
211         .name                           = "l3g4200d",\r
212         .type                           = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct\r
213         .id_i2c                         = GYRO_ID_L3G4200D,             //i2c id number\r
214         .read_reg                       = GYRO_DATA_REG,                //read data\r
215         .read_len                       = 6,                            //data length\r
216         .id_reg                         = GYRO_WHO_AM_I,                //read device id from this register\r
217         .id_data                        = GYRO_DEVID_L3G4200D,          //device id\r
218         .precision                      = 8,                            //8 bits\r
219         .ctrl_reg                       = GYRO_CTRL_REG1,               //enable or disable \r
220         .int_status_reg                 = GYRO_INT_SRC,                 //intterupt status register,if no exist then -1\r
221         .range                          = {-32768,32768},               //range\r
222         .trig                           = IRQF_TRIGGER_LOW|IRQF_ONESHOT,                \r
223         .active                         = sensor_active,        \r
224         .init                           = sensor_init,\r
225         .report                         = sensor_report_value,\r
226 };\r
227 \r
228 /****************operate according to sensor chip:end************/\r
229 \r
230 //function name should not be changed\r
231 static struct sensor_operate *gyro_get_ops(void)\r
232 {\r
233         return &gyro_l3g4200d_ops;\r
234 }\r
235 \r
236 \r
237 static int __init gyro_l3g4200d_init(void)\r
238 {\r
239         struct sensor_operate *ops = gyro_get_ops();\r
240         int result = 0;\r
241         int type = ops->type;\r
242         result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);\r
243         return result;\r
244 }\r
245 \r
246 static void __exit gyro_l3g4200d_exit(void)\r
247 {\r
248         struct sensor_operate *ops = gyro_get_ops();\r
249         int type = ops->type;\r
250         sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);\r
251 }\r
252 \r
253 \r
254 module_init(gyro_l3g4200d_init);\r
255 module_exit(gyro_l3g4200d_exit);\r
256 \r
257 \r