input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / ls_al3006.c
1 /* drivers/input/sensors/access/kxtik.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@rock-chips.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33
34
35 #define CONFIG_REG        (0x00)
36 #define TIM_CTL_REG       (0x01)
37 #define ALS_CTL_REG       (0x02)
38 #define INT_STATUS_REG    (0x03)
39 #define PS_CTL_REG        (0x04)
40 #define PS_ALS_DATA_REG   (0x05)
41 #define ALS_WINDOWS_REG   (0x08)
42
43 //enable bit[ 0-1], in register CONFIG_REG
44 #define ONLY_ALS_EN       (0x00)
45 #define ONLY_PROX_EN      (0x01)
46 #define ALL_PROX_ALS_EN   (0x02)
47 #define ALL_IDLE          (0x03)
48
49 #define POWER_MODE_MASK   (0x0C)
50 #define POWER_UP_MODE     (0x00)
51 #define POWER_DOWN_MODE   (0x08)
52 #define POWER_RESET_MODE  (0x0C)
53
54 static int sensor_power_updown(struct i2c_client *client, int on)
55 {
56         int result = 0;
57         char value = 0;
58         int i = 0;
59         for(i=0; i<3; i++)
60         {
61                 if(!on)
62                 {
63                         value = sensor_read_reg(client, CONFIG_REG);
64                         value &= ~POWER_MODE_MASK;
65                         value |= POWER_DOWN_MODE;
66                         result = sensor_write_reg(client, CONFIG_REG, value);
67                         if(result)
68                                 return result;
69                 }
70                 else
71                 {
72                         value = sensor_read_reg(client, CONFIG_REG);
73                         value &= ~POWER_MODE_MASK;
74                         value |= POWER_UP_MODE;
75                         result = sensor_write_reg(client, CONFIG_REG, value);
76                         if(result)
77                                 return result;
78                 }
79
80                 if(!result)
81                 break;
82         }
83
84         if(i>1)
85         printk("%s:set %d times",__func__,i);
86
87         return result;
88 }
89
90 /****************operate according to sensor chip:start************/
91
92 static int sensor_active(struct i2c_client *client, int enable, int rate)
93 {
94         struct sensor_private_data *sensor =
95             (struct sensor_private_data *) i2c_get_clientdata(client);
96         int result = 0;
97         char value = 0;
98
99         if(enable)
100         sensor_power_updown(client, 1);
101
102         value = sensor_read_reg(client, sensor->ops->ctrl_reg);
103
104         //register setting according to chip datasheet
105         if(enable)
106         {
107                 if( (value & 0x03) == ONLY_PROX_EN )
108                 {
109                         value &= ~0x03;
110                         value |= ALL_PROX_ALS_EN;
111                 }
112                 else if((value & 0x03) == ALL_IDLE )
113                 {
114                         value &= ~0x03;
115                         value |= ONLY_ALS_EN;
116                 }
117
118         }
119         else
120         {
121                 if( (value & 0x03) == ONLY_ALS_EN )
122                 {
123                         value &= ~0x03;
124                         value |= ALL_IDLE;
125                 }
126                 else if((value & 0x03) == ALL_PROX_ALS_EN )
127                 {
128                         value &= ~0x03;
129                         value |= ONLY_PROX_EN;
130                 }
131
132         }
133
134         sensor->ops->ctrl_data = value;
135
136         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
137         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
138         if(result)
139                 printk("%s:fail to active sensor\n",__func__);
140
141         return result;
142
143 }
144
145
146 static int sensor_init(struct i2c_client *client)
147 {
148         struct sensor_private_data *sensor =
149             (struct sensor_private_data *) i2c_get_clientdata(client);
150         int result = 0;
151         char value = 0;
152
153         sensor_power_updown(client, 0);
154
155         result = sensor->ops->active(client,0,0);
156         if(result)
157         {
158                 printk("%s:line=%d,error\n",__func__,__LINE__);
159                 return result;
160         }
161
162         sensor->status_cur = SENSOR_OFF;
163
164         value = 0x41;//The ADC effective resolution = 9;  Low lux threshold level = 1;
165         //value = 0x69; //The ADC effective resolution = 17;  Low lux threshold level = 9;
166         result = sensor_write_reg(client, ALS_CTL_REG, value);
167         if(result)
168         {
169                 printk("%s:line=%d,error\n",__func__,__LINE__);
170                 return result;
171         }
172
173         //value = 0x04;//0x01-0x0f; 17%->93.5% if value = 0x04,then Compensate Loss 52%
174         value = 0x02;//0x01-0x0f; 17%->93.5% if value = 0x02,then Compensate Loss 31%
175         result = sensor_write_reg(client, ALS_WINDOWS_REG, value);
176         if(result)
177         {
178                 printk("%s:line=%d,error\n",__func__,__LINE__);
179                 return result;
180         }
181
182         return result;
183 }
184
185
186 static int light_report_value(struct input_dev *input, int data)
187 {
188         unsigned char index = 0;
189         if(data <= 0){
190                 index = 0;goto report;
191         }
192         else if(data <= 2){
193                 index = 1;goto report;
194         }
195         else if(data <= 4){
196                 index = 2;goto report;
197         }
198         else if(data <= 8){
199                 index = 3;goto report;
200         }
201         else if(data <= 14){
202                 index = 4;goto report;
203         }
204         else if(data <= 20){
205                 index = 5;goto report;
206         }
207         else if(data <= 26){
208                 index = 6;goto report;
209         }
210         else{
211                 index = 7;goto report;
212         }
213
214 report:
215         input_report_abs(input, ABS_MISC, index);
216         input_sync(input);
217
218         return index;
219 }
220
221
222 static int sensor_report_value(struct i2c_client *client)
223 {
224         struct sensor_private_data *sensor =
225             (struct sensor_private_data *) i2c_get_clientdata(client);
226         int result = 0;
227         char value = 0;
228         char index = 0;
229
230         if(sensor->pdata->irq_enable)
231         {
232                 if(sensor->ops->int_status_reg)
233                 {
234                         value = sensor_read_reg(client, sensor->ops->int_status_reg);
235                 }
236
237         }
238
239         value = sensor_read_reg(client, sensor->ops->read_reg);
240         index = light_report_value(sensor->input_dev, value&0x3f); // bit0-5  is ls data;
241
242         DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
243
244         return result;
245 }
246
247 struct sensor_operate light_al3006_ops = {
248         .name                           = "ls_al3006",
249         .type                           = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct
250         .id_i2c                         = LIGHT_ID_AL3006,      //i2c id number
251         .read_reg                       = PS_ALS_DATA_REG,      //read data
252         .read_len                       = 1,                    //data length
253         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register
254         .id_data                        = SENSOR_UNKNOW_DATA,   //device id
255         .precision                      = 8,                    //8 bits
256         .ctrl_reg                       = CONFIG_REG,           //enable or disable
257         .int_status_reg                 = INT_STATUS_REG,       //intterupt status register
258         .range                          = {100,65535},          //range
259         .brightness                                        ={10,255},                          // brightness
260         .trig                           = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
261         .active                         = sensor_active,
262         .init                           = sensor_init,
263         .report                         = sensor_report_value,
264 };
265
266 /****************operate according to sensor chip:end************/
267
268 //function name should not be changed
269 static struct sensor_operate *light_get_ops(void)
270 {
271         return &light_al3006_ops;
272 }
273
274
275 static int __init light_al3006_init(void)
276 {
277         struct sensor_operate *ops = light_get_ops();
278         int result = 0;
279         int type = ops->type;
280         result = sensor_register_slave(type, NULL, NULL, light_get_ops);
281         return result;
282 }
283
284 static void __exit light_al3006_exit(void)
285 {
286         struct sensor_operate *ops = light_get_ops();
287         int type = ops->type;
288         sensor_unregister_slave(type, NULL, NULL, light_get_ops);
289 }
290
291
292 module_init(light_al3006_init);
293 module_exit(light_al3006_exit);
294
295