f8090d56127e4c551aeb6bcd2c01bf46d0c5b584
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / psensor / ps_al3006.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 CONFIG_REG        (0x00)\r
37 #define TIM_CTL_REG       (0x01)\r
38 #define ALS_CTL_REG       (0x02)\r
39 #define INT_STATUS_REG    (0x03)\r
40 #define PS_CTL_REG        (0x04)\r
41 #define PS_ALS_DATA_REG   (0x05)\r
42 #define ALS_WINDOWS_REG   (0x08)\r
43 \r
44 //enable bit[ 0-1], in register CONFIG_REG\r
45 #define ONLY_ALS_EN       (0x00)\r
46 #define ONLY_PROX_EN      (0x01)\r
47 #define ALL_PROX_ALS_EN   (0x02)\r
48 #define ALL_IDLE          (0x03)\r
49 \r
50 #define POWER_MODE_MASK   (0x0C)\r
51 #define POWER_UP_MODE     (0x00)\r
52 #define POWER_DOWN_MODE   (0x08)\r
53 #define POWER_RESET_MODE  (0x0C)\r
54 \r
55 static int sensor_power_updown(struct i2c_client *client, int on)\r
56 {\r
57         int result = 0;\r
58         char value = 0;\r
59         int i = 0;\r
60         for(i=0; i<3; i++)\r
61         {\r
62                 if(!on)\r
63                 {\r
64                         value = sensor_read_reg(client, CONFIG_REG);\r
65                         value &= ~POWER_MODE_MASK;\r
66                         value |= POWER_DOWN_MODE;\r
67                         result = sensor_write_reg(client, CONFIG_REG, value);\r
68                         if(result)\r
69                                 return result;\r
70                 }\r
71                 else\r
72                 {\r
73                         value = sensor_read_reg(client, CONFIG_REG);\r
74                         value &= ~POWER_MODE_MASK;\r
75                         value |= POWER_UP_MODE;\r
76                         result = sensor_write_reg(client, CONFIG_REG, value);\r
77                         if(result)\r
78                                 return result;\r
79                 }\r
80                 \r
81                 if(!result)\r
82                 break;\r
83         }\r
84 \r
85         if(i>1)\r
86         printk("%s:set %d times",__func__,i);\r
87         \r
88         return result;\r
89 }\r
90 \r
91 \r
92 /****************operate according to sensor chip:start************/\r
93 \r
94 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
95 {\r
96         struct sensor_private_data *sensor =\r
97             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
98         int result = 0;\r
99         char value = 0;\r
100 \r
101         if(enable)      \r
102         sensor_power_updown(client, 1);\r
103 \r
104         value = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
105         \r
106         //register setting according to chip datasheet          \r
107         if(enable)\r
108         {       \r
109                 if( (value & 0x03) == ONLY_ALS_EN )\r
110                 {\r
111                         value &= ~0x03;\r
112                         value |= ALL_PROX_ALS_EN;\r
113                 }\r
114                 else if((value & 0x03) == ALL_IDLE )\r
115                 {\r
116                         value &= ~0x03;\r
117                         value |= ONLY_PROX_EN;\r
118                 }\r
119                 \r
120         }\r
121         else\r
122         {\r
123                 if( (value & 0x03) == ONLY_PROX_EN )\r
124                 {\r
125                         value &= ~0x03;\r
126                         value |= ALL_IDLE;\r
127                 }\r
128                 else if((value & 0x03) == ALL_PROX_ALS_EN )\r
129                 {\r
130                         value &= ~0x03;\r
131                         value |= ONLY_ALS_EN;\r
132                 }\r
133         }\r
134         \r
135         sensor->ops->ctrl_data = value;\r
136         \r
137         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
138         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
139         if(result)\r
140                 printk("%s:fail to active sensor\n",__func__);\r
141 \r
142         return result;\r
143 \r
144 }\r
145 \r
146 \r
147 static int sensor_init(struct i2c_client *client)\r
148 {       \r
149         struct sensor_private_data *sensor =\r
150             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
151         int result = 0;\r
152         char value = 0;\r
153         \r
154         sensor_power_updown(client, 0);\r
155                 \r
156         result = sensor->ops->active(client,0,0);\r
157         if(result)\r
158         {\r
159                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
160                 return result;\r
161         }\r
162         \r
163         sensor->status_cur = SENSOR_OFF;\r
164 \r
165         value = 0x41;//The ADC effective resolution = 9;  Low lux threshold level = 1;\r
166         //value = 0x69; //The ADC effective resolution = 17;  Low lux threshold level = 9;
167         result = sensor_write_reg(client, ALS_CTL_REG, value);\r
168         if(result)\r
169         {\r
170                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
171                 return result;\r
172         }\r
173
174         //value = 0x04;//0x01-0x0f; 17%->93.5% if value = 0x04,then Compensate Loss 52%
175         value = 0x02;//0x01-0x0f; 17%->93.5% if value = 0x02,then Compensate Loss 31%   \r
176         result = sensor_write_reg(client, ALS_WINDOWS_REG, value);\r
177         if(result)\r
178         {\r
179                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
180                 return result;\r
181         }\r
182                 \r
183         return result;\r
184 }\r
185 \r
186 static int sensor_report_value(struct i2c_client *client)\r
187 {\r
188         struct sensor_private_data *sensor =\r
189             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
190         int result = 0;\r
191         char value = 0;\r
192 \r
193         if(sensor->pdata->irq_enable)\r
194         {\r
195                 if(sensor->ops->int_status_reg)\r
196                 {\r
197                         value = sensor_read_reg(client, sensor->ops->int_status_reg);\r
198                 }\r
199                 \r
200         }\r
201 \r
202         value = sensor_read_reg(client, sensor->ops->read_reg); \r
203         input_report_abs(sensor->input_dev, ABS_DISTANCE, (value>>7)?0:1);\r
204         input_sync(sensor->input_dev);\r
205         DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,(value>>7)?0:1);           \r
206 \r
207         return result;\r
208 }\r
209 \r
210 struct sensor_operate proximity_al3006_ops = {\r
211         .name                           = "ps_al3006",\r
212         .type                           = SENSOR_TYPE_PROXIMITY,//sensor type and it should be correct\r
213         .id_i2c                         = PROXIMITY_ID_AL3006,  //i2c id number\r
214         .read_reg                       = PS_ALS_DATA_REG,      //read data\r
215         .read_len                       = 1,                    //data length\r
216         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register\r
217         .id_data                        = SENSOR_UNKNOW_DATA,   //device id\r
218         .precision                      = 8,                    //8 bits\r
219         .ctrl_reg                       = CONFIG_REG,           //enable or disable \r
220         .int_status_reg                 = INT_STATUS_REG,       //intterupt status register\r
221         .range                          = {0,10},               //range\r
222         .trig                           = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,                \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 *proximity_get_ops(void)\r
232 {\r
233         return &proximity_al3006_ops;\r
234 }\r
235 \r
236 \r
237 static int __init proximity_al3006_init(void)\r
238 {\r
239         struct sensor_operate *ops = proximity_get_ops();\r
240         int result = 0;\r
241         int type = ops->type;\r
242         result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);\r
243         return result;\r
244 }\r
245 \r
246 static void __exit proximity_al3006_exit(void)\r
247 {\r
248         struct sensor_operate *ops = proximity_get_ops();\r
249         int type = ops->type;\r
250         sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);\r
251 }\r
252 \r
253 \r
254 module_init(proximity_al3006_init);\r
255 module_exit(proximity_al3006_exit);\r
256 \r
257 \r