fix sensors include file err
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / gyro / l3g20d.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 <linux/of_gpio.h>\r
29 #ifdef CONFIG_HAS_EARLYSUSPEND\r
30 #include <linux/earlysuspend.h>\r
31 #endif\r
32 #include <linux/l3g4200d.h>\r
33 #include <linux/sensor-dev.h>\r
34 \r
35 \r
36 #define L3G4200D_ENABLE                 0x08\r
37 \r
38 /****************operate according to sensor chip:start************/\r
39 \r
40 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
41 {\r
42         struct sensor_private_data *sensor =\r
43             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
44         int result = 0;\r
45         int status = 0;\r
46                 \r
47         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
48 \r
49         //register setting according to chip datasheet          \r
50         if(enable)\r
51         {       \r
52                 status = L3G4200D_ENABLE;       //l3g20d        \r
53                 sensor->ops->ctrl_data |= status;       \r
54         }\r
55         else\r
56         {\r
57                 status = ~L3G4200D_ENABLE;      //l3g20d\r
58                 sensor->ops->ctrl_data &= status;\r
59         }\r
60 \r
61         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
62         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
63         if(result)\r
64                 printk("%s:fail to active sensor\n",__func__);\r
65 \r
66         return result;\r
67 \r
68 }\r
69 \r
70 static int sensor_init(struct i2c_client *client)\r
71 {       \r
72         struct sensor_private_data *sensor =\r
73             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
74         int result = 0; \r
75         unsigned char buf[5];           \r
76         unsigned char data = 0;\r
77         int i = 0;\r
78         \r
79         result = sensor->ops->active(client,0,0);\r
80         if(result)\r
81         {\r
82                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
83                 return result;\r
84         }\r
85         \r
86         sensor->status_cur = SENSOR_OFF;\r
87         \r
88         buf[0] = 0x07;  //27\r
89         buf[1] = 0x00;  \r
90         buf[2] = 0x00;  \r
91         buf[3] = 0x20;  //0x00\r
92         buf[4] = 0x00;  \r
93         for(i=0; i<5; i++)\r
94         {\r
95                 result = sensor_write_reg(client, sensor->ops->ctrl_reg+i, buf[i]);\r
96                 if(result)\r
97                 {\r
98                         printk("%s:line=%d,error\n",__func__,__LINE__);\r
99                         return result;\r
100                 }\r
101         }\r
102         \r
103         result = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
104         if (result >= 0)\r
105                 data = result & 0x000F;\r
106
107         sensor->ops->ctrl_data = data + ODR100_BW12_5;  \r
108         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
109         if(result)\r
110         {\r
111                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
112                 return result;\r
113         }\r
114         \r
115         return result;\r
116 }\r
117 \r
118 \r
119 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)\r
120 {\r
121         struct sensor_private_data *sensor =\r
122                 (struct sensor_private_data *) i2c_get_clientdata(client);      \r
123 \r
124         /* Report GYRO  information */\r
125         input_report_rel(sensor->input_dev, ABS_RX, axis->x);\r
126         input_report_rel(sensor->input_dev, ABS_RY, axis->y);\r
127         input_report_rel(sensor->input_dev, ABS_RZ, axis->z);\r
128         input_sync(sensor->input_dev);\r
129         DBG("gyro x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);\r
130 \r
131         return 0;\r
132 }\r
133 \r
134 static int sensor_report_value(struct i2c_client *client)\r
135 {\r
136         struct sensor_private_data *sensor =\r
137                 (struct sensor_private_data *) i2c_get_clientdata(client);      \r
138         struct sensor_platform_data *pdata = sensor->pdata;\r
139         int ret = 0;\r
140         int x = 0, y = 0, z = 0;\r
141         struct sensor_axis axis;\r
142         char buffer[6] = {0};   \r
143         int i = 0;\r
144         int value = 0;\r
145         \r
146         if(sensor->ops->read_len < 6)   //sensor->ops->read_len = 6\r
147         {\r
148                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);\r
149                 return -1;\r
150         }\r
151         \r
152         memset(buffer, 0, 6);\r
153 #if 0   \r
154         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   \r
155         do {\r
156                 buffer[0] = sensor->ops->read_reg;\r
157                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);\r
158                 if (ret < 0)\r
159                 return ret;\r
160         } while (0);\r
161 #else\r
162 \r
163         for(i=0; i<6; i++)\r
164         {
165                 //buffer[i] = sensor->ops->read_reg + i;        \r
166                 buffer[i] = sensor_read_reg(client, sensor->ops->read_reg + i);\r
167         }\r
168 #endif\r
169         x = (short) (((buffer[1]) << 8) | buffer[0]);\r
170         y = (short) (((buffer[3]) << 8) | buffer[2]);\r
171         z = (short) (((buffer[5]) << 8) | buffer[4]);\r
172
173         DBG("%s: x=%d  y=%d z=%d \n",__func__, x,y,z);
174         if(pdata && pdata->orientation)
175         {
176                 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
177                 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;     
178                 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
179         }
180         else
181         {
182                 axis.x = x;     
183                 axis.y = y;
184                 axis.z = z;     
185         }
186
187         //filter gyro data
188         if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))\r
189         {       \r
190                 gyro_report_value(client, &axis);       \r
191 \r
192                  /* »¥³âµØ»º´æÊý¾Ý. */\r
193                 mutex_lock(&(sensor->data_mutex) );\r
194                 sensor->axis = axis;\r
195                 mutex_unlock(&(sensor->data_mutex) );\r
196         }       \r
197 \r
198         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register\r
199         {\r
200                 \r
201                 value = sensor_read_reg(client, sensor->ops->int_status_reg);\r
202                 DBG("%s:sensor int status :0x%x\n",__func__,value);\r
203         }\r
204         \r
205         return ret;\r
206 }\r
207 \r
208 \r
209 static struct sensor_operate gyro_l3g20d_ops = {\r
210         .name                           = "l3g20d",\r
211         .type                           = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct\r
212         .id_i2c                         = GYRO_ID_L3G20D,               //i2c id number\r
213         .read_reg                       = GYRO_DATA_REG,                //read data\r
214         .read_len                       = 6,                            //data length\r
215         .id_reg                         = GYRO_WHO_AM_I,                //read device id from this register\r
216         .id_data                        = GYRO_DEVID_L3G20D,            //device id\r
217         .precision                      = 8,                            //8 bits\r
218         .ctrl_reg                       = GYRO_CTRL_REG1,               //enable or disable \r
219         .int_status_reg                 = GYRO_INT_SRC,                 //intterupt status register,if no exist then -1\r
220         .range                          = {-32768,32768},               //range\r
221         .trig                           = IRQF_TRIGGER_LOW|IRQF_ONESHOT,                \r
222         .active                         = sensor_active,        \r
223         .init                           = sensor_init,\r
224         .report                         = sensor_report_value,\r
225 };\r
226 \r
227 /****************operate according to sensor chip:end************/\r
228 \r
229 //function name should not be changed\r
230 static struct sensor_operate *gyro_get_ops(void)\r
231 {\r
232         return &gyro_l3g20d_ops;\r
233 }\r
234 \r
235 \r
236 static int __init gyro_l3g20d_init(void)\r
237 {\r
238         struct sensor_operate *ops = gyro_get_ops();\r
239         int result = 0;\r
240         int type = ops->type;\r
241         result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);\r
242         return result;\r
243 }\r
244 \r
245 static void __exit gyro_l3g20d_exit(void)\r
246 {\r
247         struct sensor_operate *ops = gyro_get_ops();\r
248         int type = ops->type;\r
249         sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);\r
250 }\r
251 \r
252 \r
253 module_init(gyro_l3g20d_init);\r
254 module_exit(gyro_l3g20d_exit);\r
255 \r
256 \r