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