fix sensors include file err
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / ls_stk3171.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/sensor-dev.h>\r
33 \r
34 \r
35 #define ALS_CMD         0x01\r
36 #define ALS_DT1         0x02\r
37 #define ALS_DT2         0X03\r
38 #define ALS_THDH1       0X04\r
39 #define ALS_THDH2       0X05\r
40 #define ALS_THDL1       0X06\r
41 #define ALS_THDL2       0X07\r
42 #define STA_TUS         0X08\r
43 #define PS_CMD          0X09\r
44 #define PS_DT           0X0A\r
45 #define PS_THDH         0X0B\r
46 #define PS_THDL         0X0C\r
47 #define SW_RESET        0X80\r
48 \r
49 //ALS_CMD\r
50 #define ALS_SD_ENABLE   (0<<0)\r
51 #define ALS_SD_DISABLE  (1<<0)\r
52 #define ALS_INT_DISABLE (0<<1)\r
53 #define ALS_INT_ENABLE  (1<<1)\r
54 #define ALS_1T_100MS    (0<<2)\r
55 #define ALS_2T_200MS    (1<<2)\r
56 #define ALS_4T_400MS    (2<<2)\r
57 #define ALS_8T_800MS    (3<<2)\r
58 #define ALS_RANGE_57671 (0<<6)\r
59 #define ALS_RANGE_28836 (1<<6)\r
60 \r
61 //PS_CMD\r
62 #define PS_SD_ENABLE    (0<<0)\r
63 #define PS_SD_DISABLE   (1<<0)\r
64 #define PS_INT_DISABLE  (0<<1)\r
65 #define PS_INT_ENABLE   (1<<1)\r
66 #define PS_10T_2MS      (0<<2)\r
67 #define PS_15T_3MS      (1<<2)\r
68 #define PS_20T_4MS      (2<<2)\r
69 #define PS_25T_5MS      (3<<2)\r
70 #define PS_CUR_100MA    (0<<4)\r
71 #define PS_CUR_200MA    (1<<4)\r
72 #define PS_SLP_10MS     (0<<5)\r
73 #define PS_SLP_30MS     (1<<5)\r
74 #define PS_SLP_90MS     (2<<5)\r
75 #define PS_SLP_270MS    (3<<5)\r
76 #define TRIG_PS_OR_LS   (0<<7)\r
77 #define TRIG_PS_AND_LS  (1<<7)\r
78 \r
79 //STA_TUS\r
80 #define STA_PS_INT      (1<<5)\r
81 #define STA_ALS_INT     (1<<4)\r
82 \r
83 \r
84 \r
85 /****************operate according to sensor chip:start************/\r
86 \r
87 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
88 {\r
89         struct sensor_private_data *sensor =\r
90             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
91         int result = 0;\r
92         int status = 0;\r
93 \r
94         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
95         \r
96         //register setting according to chip datasheet          \r
97         if(!enable)\r
98         {       \r
99                 status = ALS_SD_DISABLE;        \r
100                 sensor->ops->ctrl_data |= status;       \r
101         }\r
102         else\r
103         {\r
104                 status = ~ALS_SD_DISABLE;\r
105                 sensor->ops->ctrl_data &= status;\r
106         }\r
107                 \r
108         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
109         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
110         if(result)\r
111                 printk("%s:fail to active sensor\n",__func__);\r
112 \r
113         if(enable)\r
114         sensor->ops->report(sensor->client);\r
115         \r
116         return result;\r
117 \r
118 }\r
119 \r
120 \r
121 static int sensor_init(struct i2c_client *client)\r
122 {       \r
123         struct sensor_private_data *sensor =\r
124             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
125         int result = 0;\r
126         \r
127         result = sensor->ops->active(client,0,0);\r
128         if(result)\r
129         {\r
130                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
131                 return result;\r
132         }\r
133         \r
134         sensor->status_cur = SENSOR_OFF;\r
135 \r
136         result = sensor_write_reg(client, SW_RESET, 0);\r
137         if(result)\r
138         {\r
139                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
140                 return result;\r
141         }\r
142 \r
143         result = sensor_write_reg(client, ALS_THDH1, 0);//it is important,if not then als can not trig intterupt\r
144         if(result)\r
145         {\r
146                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
147                 return result;\r
148         }\r
149 \r
150         result = sensor_write_reg(client, ALS_THDH2, 0);\r
151         if(result)\r
152         {\r
153                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
154                 return result;\r
155         }       \r
156 \r
157         sensor->ops->ctrl_data |= ALS_1T_100MS;\r
158 \r
159         if(sensor->pdata->irq_enable)\r
160                 sensor->ops->ctrl_data |= ALS_INT_ENABLE;\r
161         else\r
162                 sensor->ops->ctrl_data &= ~ALS_INT_ENABLE;\r
163         \r
164         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
165         if(result)\r
166         {\r
167                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
168                 return result;\r
169         }\r
170                 \r
171         return result;\r
172 }\r
173 \r
174 \r
175 static int light_report_value(struct input_dev *input, int data)\r
176 {\r
177         unsigned char index = 0;\r
178         if(data <= 100){\r
179                 index = 0;goto report;\r
180         }\r
181         else if(data <= 1600){\r
182                 index = 1;goto report;\r
183         }\r
184         else if(data <= 2250){\r
185                 index = 2;goto report;\r
186         }\r
187         else if(data <= 3200){\r
188                 index = 3;goto report;\r
189         }\r
190         else if(data <= 6400){\r
191                 index = 4;goto report;\r
192         }\r
193         else if(data <= 12800){\r
194                 index = 5;goto report;\r
195         }\r
196         else if(data <= 26000){\r
197                 index = 6;goto report;\r
198         }\r
199         else{\r
200                 index = 7;goto report;\r
201         }\r
202 \r
203 report:\r
204         input_report_abs(input, ABS_MISC, index);\r
205         input_sync(input);\r
206         \r
207         return index;\r
208 }\r
209 \r
210 \r
211 static int sensor_report_value(struct i2c_client *client)\r
212 {\r
213         struct sensor_private_data *sensor =\r
214             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
215         int result = 0;\r
216         int value = 0;\r
217         char buffer[2] = {0};   \r
218         char index = 0;\r
219         \r
220         if(sensor->ops->read_len < 2)   //sensor->ops->read_len = 2\r
221         {\r
222                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);\r
223                 return -1;\r
224         }\r
225         \r
226         memset(buffer, 0, 2);\r
227 \r
228         buffer[0] = sensor->ops->read_reg;\r
229         result = sensor_rx_data(client, buffer, sensor->ops->read_len); \r
230         if(result)\r
231         {\r
232                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
233                 return result;\r
234         }\r
235 \r
236         value = (buffer[0] << 8) | buffer[1];\r
237 \r
238         \r
239         index = light_report_value(sensor->input_dev, value);\r
240 \r
241         DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);\r
242         \r
243         if(sensor->pdata->irq_enable)\r
244         {\r
245                 if(sensor->ops->int_status_reg)\r
246                 {       \r
247                         value = sensor_read_reg(client, sensor->ops->int_status_reg);\r
248                 }\r
249                 \r
250                 if(value & STA_ALS_INT)\r
251                 {\r
252                         value &= ~STA_ALS_INT;\r
253                         result = sensor_write_reg(client, sensor->ops->int_status_reg,value);   //clear int\r
254                         if(result)\r
255                         {\r
256                                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
257                                 return result;\r
258                         }\r
259                 }\r
260         }\r
261         \r
262                         \r
263         return result;\r
264 }\r
265 \r
266 struct sensor_operate light_stk3171_ops = {\r
267         .name                           = "ls_stk3171",\r
268         .type                           = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct\r
269         .id_i2c                         = LIGHT_ID_STK3171,     //i2c id number\r
270         .read_reg                       = ALS_DT1,              //read data\r
271         .read_len                       = 2,                    //data length\r
272         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register\r
273         .id_data                        = SENSOR_UNKNOW_DATA,   //device id\r
274         .precision                      = 16,                   //8 bits\r
275         .ctrl_reg                       = ALS_CMD,              //enable or disable \r
276         .int_status_reg                 = STA_TUS,              //intterupt status register\r
277         .range                          = {100,65535},          //range\r
278         .brightness                                        ={10,255},     //brightness\r
279         .trig                           = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,                \r
280         .active                         = sensor_active,        \r
281         .init                           = sensor_init,\r
282         .report                         = sensor_report_value,\r
283 };\r
284 \r
285 /****************operate according to sensor chip:end************/\r
286 \r
287 //function name should not be changed\r
288 static struct sensor_operate *light_get_ops(void)\r
289 {\r
290         return &light_stk3171_ops;\r
291 }\r
292 \r
293 \r
294 static int __init light_stk3171_init(void)\r
295 {\r
296         struct sensor_operate *ops = light_get_ops();\r
297         int result = 0;\r
298         int type = ops->type;\r
299         result = sensor_register_slave(type, NULL, NULL, light_get_ops);\r
300         return result;\r
301 }\r
302 \r
303 static void __exit light_stk3171_exit(void)\r
304 {\r
305         struct sensor_operate *ops = light_get_ops();\r
306         int type = ops->type;\r
307         sensor_unregister_slave(type, NULL, NULL, light_get_ops);\r
308 }\r
309 \r
310 \r
311 module_init(light_stk3171_init);\r
312 module_exit(light_stk3171_exit);\r
313 \r
314 \r