225d7a4d98296c60a8477d1df37d23addabe1fd8
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / ls_ap321xx.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 <mach/gpio.h>
29 #include <mach/board.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34
35
36 #define AP3212B_NUM_CACHABLE_REGS       23
37 #define AP3216C_NUM_CACHABLE_REGS       26
38
39 #define AP3212B_RAN_COMMAND     0x10
40 #define AP3212B_RAN_MASK                0x30
41 #define AP3212B_RAN_SHIFT       (4)
42
43 #define AP3212B_MODE_COMMAND    0x00
44 #define AP3212B_MODE_SHIFT      (0)
45 #define AP3212B_MODE_MASK       0x07
46
47 #define AP3212B_INT_COMMAND     0x01
48 #define AP3212B_INT_SHIFT       (0)
49 #define AP3212B_INT_MASK                0x03
50 #define AP3212B_INT_PMASK               0x02
51 #define AP3212B_INT_AMASK               0x01
52
53 #define AL3212_ADC_LSB          0x0c
54 #define AL3212_ADC_MSB          0x0d
55
56 #define AP3212B_ALS_LTHL                        0x1a
57 #define AP3212B_ALS_LTHL_SHIFT  (0)
58 #define AP3212B_ALS_LTHL_MASK   0xff
59
60 #define AP3212B_ALS_LTHH                        0x1b
61 #define AP3212B_ALS_LTHH_SHIFT  (0)
62 #define AP3212B_ALS_LTHH_MASK   0xff
63
64 #define AP3212B_ALS_HTHL                        0x1c
65 #define AP3212B_ALS_HTHL_SHIFT  (0)
66 #define AP3212B_ALS_HTHL_MASK   0xff
67
68 #define AP3212B_ALS_HTHH                        0x1d
69 #define AP3212B_ALS_HTHH_SHIFT  (0)
70 #define AP3212B_ALS_HTHH_MASK   0xff
71
72 static u16 ap321xx_threshole[8] = {28,444,625,888,1778,3555,7222,0xffff};
73
74 /*
75  * register access helpers
76  */
77
78 static int __ap321xx_read_reg(struct i2c_client *client,
79                                u32 reg, u8 mask, u8 shift)
80 {
81         u8 val;
82
83         val = i2c_smbus_read_byte_data(client, reg);
84         return (val & mask) >> shift;
85 }
86
87 static int __ap321xx_write_reg(struct i2c_client *client,
88                                 u32 reg, u8 mask, u8 shift, u8 val)
89 {
90         int ret = 0;
91         u8 tmp;
92
93         tmp = i2c_smbus_read_byte_data(client, reg);
94         tmp &= ~mask;
95         tmp |= val << shift;
96
97         ret = i2c_smbus_write_byte_data(client, reg, tmp);
98
99         return ret;
100 }
101
102
103 /*
104  * internally used functions
105  */
106 /* range */
107 static int ap321xx_set_range(struct i2c_client *client, int range)
108 {
109         return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
110                 AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
111 }
112
113
114 /* mode */
115 static int ap321xx_get_mode(struct i2c_client *client)
116 {
117         struct sensor_private_data *sensor =
118             (struct sensor_private_data *) i2c_get_clientdata(client);
119         int ret;
120
121         ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
122                         AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
123         return ret;
124 }
125 static int ap321xx_set_mode(struct i2c_client *client, int mode)
126 {
127         struct sensor_private_data *sensor =
128             (struct sensor_private_data *) i2c_get_clientdata(client);
129         int ret;
130
131         ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
132                                 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
133         return ret;
134 }
135
136 static int ap321xx_get_adc_value(struct i2c_client *client)
137 {
138         unsigned int lsb, msb, val;
139         unsigned char index=0;
140
141         lsb = i2c_smbus_read_byte_data(client, AL3212_ADC_LSB);
142         if (lsb < 0) {
143                 return lsb;
144         }
145
146         msb = i2c_smbus_read_byte_data(client, AL3212_ADC_MSB);
147         if (msb < 0)
148                 return msb;
149
150         val = msb << 8 | lsb;
151         for(index = 0; index < 7 && val > ap321xx_threshole[index];index++)
152                 ;
153
154         return index;
155 }
156
157 /* ALS low threshold */
158 static int ap321xx_set_althres(struct i2c_client *client, int val)
159 {
160         int lsb, msb, err;
161
162         msb = val >> 8;
163         lsb = val & AP3212B_ALS_LTHL_MASK;
164
165         err = __ap321xx_write_reg(client, AP3212B_ALS_LTHL,
166                 AP3212B_ALS_LTHL_MASK, AP3212B_ALS_LTHL_SHIFT, lsb);
167         if (err)
168                 return err;
169
170         err = __ap321xx_write_reg(client, AP3212B_ALS_LTHH,
171                 AP3212B_ALS_LTHH_MASK, AP3212B_ALS_LTHH_SHIFT, msb);
172
173         return err;
174 }
175
176 /* ALS high threshold */
177 static int ap321xx_set_ahthres(struct i2c_client *client, int val)
178 {
179         int lsb, msb, err;
180
181         msb = val >> 8;
182         lsb = val & AP3212B_ALS_HTHL_MASK;
183
184         err = __ap321xx_write_reg(client, AP3212B_ALS_HTHL,
185                 AP3212B_ALS_HTHL_MASK, AP3212B_ALS_HTHL_SHIFT, lsb);
186         if (err)
187                 return err;
188
189         err = __ap321xx_write_reg(client, AP3212B_ALS_HTHH,
190                 AP3212B_ALS_HTHH_MASK, AP3212B_ALS_HTHH_SHIFT, msb);
191
192         return err;
193 }
194
195 static int ap321xx_get_intstat(struct i2c_client *client)
196 {
197         struct sensor_private_data *sensor =
198             (struct sensor_private_data *) i2c_get_clientdata(client);
199         int val;
200
201         val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
202         val &= AP3212B_INT_MASK;
203
204         return val >> AP3212B_INT_SHIFT;
205 }
206
207 static int ap321xx_product_detect(struct i2c_client *client)
208 {
209         int mid = i2c_smbus_read_byte_data(client, 0x03);
210         int pid = i2c_smbus_read_byte_data(client, 0x04);
211         int rid = i2c_smbus_read_byte_data(client, 0x05);
212
213         if ( mid == 0x01 && pid == 0x01 &&
214             (rid == 0x03 || rid == 0x04) )
215         {
216                 //printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
217         }
218         else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
219                       (mid == 0x02 && pid == 0x02 && rid == 0x01))
220         {
221                 //printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
222         }
223         else
224         {
225                 //printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
226                 return -EIO;
227         }
228
229         return 0;
230 }
231
232 static int ap321xx_init_client(struct i2c_client *client)
233 {
234         /* set defaults */
235         ap321xx_set_range(client, 0);
236         ap321xx_set_mode(client, 0);
237
238         return 0;
239 }
240
241 static int ap321xx_lsensor_enable(struct i2c_client *client)
242 {
243         int ret = 0,mode;
244
245         mode = ap321xx_get_mode(client);
246         if((mode & 0x01) == 0){
247                 mode |= 0x01;
248                 ret = ap321xx_set_mode(client,mode);
249         }
250
251         return ret;
252 }
253
254 static int ap321xx_lsensor_disable(struct i2c_client *client)
255 {
256         int ret = 0,mode;
257
258         mode = ap321xx_get_mode(client);
259         if(mode & 0x01){
260                 mode &= ~0x01;
261                 if(mode == 0x04)
262                         mode = 0;
263                 ret = ap321xx_set_mode(client,mode);
264         }
265
266         return ret;
267 }
268
269 static void ap321xx_change_ls_threshold(struct i2c_client *client)
270 {
271         struct sensor_private_data *sensor =
272             (struct sensor_private_data *) i2c_get_clientdata(client);
273         int value;
274
275         value = ap321xx_get_adc_value(client);
276         DBG("ALS lux index: %u\n", value);
277         if(value > 0){
278                 ap321xx_set_althres(client,ap321xx_threshole[value-1]);
279                 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
280         }
281         else{
282                 ap321xx_set_althres(client,0);
283                 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
284         }
285
286         input_report_abs(sensor->input_dev, ABS_MISC, value);
287         input_sync(sensor->input_dev);
288 }
289
290
291 /****************operate according to sensor chip:start************/
292
293 static int sensor_active(struct i2c_client *client, int enable, int rate)
294 {
295         int result = 0;
296
297         //register setting according to chip datasheet
298         if (enable){
299                 result = ap321xx_lsensor_enable(client);
300                 if(!result){
301                         msleep(200);
302                         ap321xx_change_ls_threshold(client);
303                 }
304         }
305         else
306                 result = ap321xx_lsensor_disable(client);
307
308         if(result)
309                 printk("%s:fail to active sensor\n",__func__);
310
311         return result;
312
313 }
314
315
316 static int sensor_init(struct i2c_client *client)
317 {
318         struct sensor_private_data *sensor =
319             (struct sensor_private_data *) i2c_get_clientdata(client);
320         int result = 0;
321
322         result = ap321xx_product_detect(client);
323         if (result)
324         {
325                 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
326                 return result;
327         }
328
329         /* initialize the AP3212B chip */
330         result = ap321xx_init_client(client);
331         if (result)
332                 return result;
333
334         result = sensor->ops->active(client,0,0);
335         if(result)
336         {
337                 printk("%s:line=%d,error\n",__func__,__LINE__);
338                 return result;
339         }
340
341         sensor->status_cur = SENSOR_OFF;
342
343         return result;
344 }
345
346 static int sensor_report_value(struct i2c_client *client)
347 {
348         int result = 0;
349         u8 int_stat;
350
351         int_stat = ap321xx_get_intstat(client);
352         // ALS int
353         if (int_stat & AP3212B_INT_AMASK)
354         {
355                 ap321xx_change_ls_threshold(client);
356         }
357
358         return result;
359 }
360
361 struct sensor_operate light_ap321xx_ops = {
362         .name                           = "ls_ap321xx",
363         .type                           = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct
364         .id_i2c                         = LIGHT_ID_AP321XX,     //i2c id number
365         .read_reg                       = SENSOR_UNKNOW_DATA,   //read data             //there are two regs, we fix them in code.
366         .read_len                       = 1,                    //data length
367         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register   //there are 3 regs, we fix them in code.
368         .id_data                        = SENSOR_UNKNOW_DATA,   //device id
369         .precision                      = 16,                   //8 bits
370         .ctrl_reg                       = AP3212B_MODE_COMMAND,         //enable or disable
371         .int_status_reg                 = AP3212B_INT_COMMAND,  //intterupt status register
372         .range                          = {100,65535},          //range
373         .brightness                                        ={10,255},                          // brightness
374         .trig                           = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
375         .active                         = sensor_active,
376         .init                           = sensor_init,
377         .report                         = sensor_report_value,
378 };
379
380 /****************operate according to sensor chip:end************/
381
382 //function name should not be changed
383 static struct sensor_operate *light_get_ops(void)
384 {
385         return &light_ap321xx_ops;
386 }
387
388
389 static int __init light_ap321xx_init(void)
390 {
391         struct sensor_operate *ops = light_get_ops();
392         int result = 0;
393         int type = ops->type;
394         result = sensor_register_slave(type, NULL, NULL, light_get_ops);
395         return result;
396 }
397
398 static void __exit light_ap321xx_exit(void)
399 {
400         struct sensor_operate *ops = light_get_ops();
401         int type = ops->type;
402         sensor_unregister_slave(type, NULL, NULL, light_get_ops);
403 }
404
405
406 module_init(light_ap321xx_init);
407 module_exit(light_ap321xx_exit);
408
409