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