input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / psensor / ps_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 AP3212B_OBJ_COMMAND     0x0f
54 #define AP3212B_OBJ_MASK                0x80
55 #define AP3212B_OBJ_SHIFT       (7)
56
57
58 /*
59  * register access helpers
60  */
61
62 static int __ap321xx_read_reg(struct i2c_client *client,
63                                u32 reg, u8 mask, u8 shift)
64 {
65         u8 val;
66
67         val = i2c_smbus_read_byte_data(client, reg);
68         return (val & mask) >> shift;
69 }
70
71 static int __ap321xx_write_reg(struct i2c_client *client,
72                                 u32 reg, u8 mask, u8 shift, u8 val)
73 {
74         int ret = 0;
75         u8 tmp;
76
77         tmp = i2c_smbus_read_byte_data(client, reg);
78         tmp &= ~mask;
79         tmp |= val << shift;
80
81         ret = i2c_smbus_write_byte_data(client, reg, tmp);
82
83         return ret;
84 }
85
86
87 /*
88  * internally used functions
89  */
90 /* range */
91 static int ap321xx_set_range(struct i2c_client *client, int range)
92 {
93         return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
94                 AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
95 }
96
97
98 /* mode */
99 static int ap321xx_get_mode(struct i2c_client *client)
100 {
101         struct sensor_private_data *sensor =
102             (struct sensor_private_data *) i2c_get_clientdata(client);
103         int ret;
104
105         ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
106                         AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
107         return ret;
108 }
109 static int ap321xx_set_mode(struct i2c_client *client, int mode)
110 {
111         struct sensor_private_data *sensor =
112             (struct sensor_private_data *) i2c_get_clientdata(client);
113         int ret;
114
115         ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
116                                 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
117         return ret;
118 }
119 static int ap321xx_get_intstat(struct i2c_client *client)
120 {
121         struct sensor_private_data *sensor =
122             (struct sensor_private_data *) i2c_get_clientdata(client);
123         int val;
124
125         val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
126         val &= AP3212B_INT_MASK;
127
128         return val >> AP3212B_INT_SHIFT;
129 }
130
131 static int ap321xx_get_object(struct i2c_client *client)
132 {
133         int val;
134
135         val = i2c_smbus_read_byte_data(client, AP3212B_OBJ_COMMAND);
136         val &= AP3212B_OBJ_MASK;
137
138         return val >> AP3212B_OBJ_SHIFT;
139 }
140
141
142 static int ap321xx_product_detect(struct i2c_client *client)
143 {
144         int mid = i2c_smbus_read_byte_data(client, 0x03);
145         int pid = i2c_smbus_read_byte_data(client, 0x04);
146         int rid = i2c_smbus_read_byte_data(client, 0x05);
147
148         if ( mid == 0x01 && pid == 0x01 &&
149             (rid == 0x03 || rid == 0x04) )
150         {
151                 //printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
152         }
153         else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
154                       (mid == 0x02 && pid == 0x02 && rid == 0x01))
155         {
156                 //printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
157         }
158         else
159         {
160                 printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
161                 return -EIO;
162         }
163
164         return 0;
165 }
166
167 static int ap321xx_init_client(struct i2c_client *client)
168 {
169         /* set defaults */
170         ap321xx_set_range(client, 0);
171         ap321xx_set_mode(client, 0);
172
173         return 0;
174 }
175
176 static int ap321xx_psensor_enable(struct i2c_client *client)
177 {
178         int ret = 0,mode;
179
180         mode = ap321xx_get_mode(client);
181         if((mode & 0x02) == 0){
182                 mode |= 0x02;
183                 ret = ap321xx_set_mode(client,mode);
184         }
185
186         return ret;
187 }
188
189 static int ap321xx_psensor_disable(struct i2c_client *client)
190 {
191         int ret = 0,mode;
192
193         mode = ap321xx_get_mode(client);
194         if(mode & 0x02){
195                 mode &= ~0x02;
196                 if(mode == 0x04)
197                         mode = 0x00;
198                 ret = ap321xx_set_mode(client,mode);
199         }
200         return ret;
201 }
202
203 /****************operate according to sensor chip:start************/
204
205 static int sensor_active(struct i2c_client *client, int enable, int rate)
206 {
207         int result = 0;
208
209         //register setting according to chip datasheet
210         if (enable){
211                 result = ap321xx_psensor_enable(client);
212         }
213         else
214                 result = ap321xx_psensor_disable(client);
215
216         if(result)
217                 printk("%s:fail to active sensor\n",__func__);
218
219         return result;
220
221 }
222
223
224 static int sensor_init(struct i2c_client *client)
225 {
226         struct sensor_private_data *sensor =
227             (struct sensor_private_data *) i2c_get_clientdata(client);
228         int result = 0;
229
230         result = ap321xx_product_detect(client);
231         if (result)
232         {
233                 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
234                 return result;
235         }
236
237         /* initialize the AP3212B chip */
238         result = ap321xx_init_client(client);
239         if (result)
240                 return result;
241
242         result = sensor->ops->active(client,0,0);
243         if(result)
244         {
245                 printk("%s:line=%d,error\n",__func__,__LINE__);
246                 return result;
247         }
248
249         sensor->status_cur = SENSOR_OFF;
250
251         return result;
252 }
253
254 static int sensor_report_value(struct i2c_client *client)
255 {
256         struct sensor_private_data *sensor =
257             (struct sensor_private_data *) i2c_get_clientdata(client);
258         int result = 0;
259         char value = 0;
260         u8 int_stat;
261
262         int_stat = ap321xx_get_intstat(client);
263         // ALS int
264         if (int_stat & AP3212B_INT_PMASK)
265         {
266                 value = ap321xx_get_object(client);
267                 input_report_abs(sensor->input_dev, ABS_DISTANCE, value);
268                 input_sync(sensor->input_dev);
269         }
270
271         return result;
272 }
273
274 struct sensor_operate proximity_ap321xx_ops = {
275         .name                           = "ps_ap321xx",
276         .type                           = SENSOR_TYPE_PROXIMITY,        //sensor type and it should be correct
277         .id_i2c                         = PROXIMITY_ID_AP321XX, //i2c id number
278         .read_reg                       = SENSOR_UNKNOW_DATA,   //read data             //there are two regs, we fix them in code.
279         .read_len                       = 1,                    //data length
280         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register   //there are 3 regs, we fix them in code.
281         .id_data                        = SENSOR_UNKNOW_DATA,   //device id
282         .precision                      = 8,                    //8 bits
283         .ctrl_reg                       = AP3212B_MODE_COMMAND,         //enable or disable
284         .int_status_reg                 = AP3212B_INT_COMMAND,  //intterupt status register
285         .range                          = {0,10},               //range
286         .brightness                                        ={10,255},                          // brightness
287         .trig                           = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
288         .active                         = sensor_active,
289         .init                           = sensor_init,
290         .report                         = sensor_report_value,
291 };
292
293 /****************operate according to sensor chip:end************/
294
295 //function name should not be changed
296 static struct sensor_operate *proximity_get_ops(void)
297 {
298         return &proximity_ap321xx_ops;
299 }
300
301
302 static int __init proximity_ap321xx_init(void)
303 {
304         struct sensor_operate *ops = proximity_get_ops();
305         int result = 0;
306         int type = ops->type;
307         result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);
308         return result;
309 }
310
311 static void __exit proximity_ap321xx_exit(void)
312 {
313         struct sensor_operate *ops = proximity_get_ops();
314         int type = ops->type;
315         sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);
316 }
317
318
319 module_init(proximity_ap321xx_init);
320 module_exit(proximity_ap321xx_exit);
321
322