1 /* drivers/input/sensors/access/kxtik.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
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.
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.
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>
33 #include <linux/sensor-dev.h>
36 #define AP3212B_NUM_CACHABLE_REGS 23
37 #define AP3216C_NUM_CACHABLE_REGS 26
39 #define AP3212B_RAN_COMMAND 0x10
40 #define AP3212B_RAN_MASK 0x30
41 #define AP3212B_RAN_SHIFT (4)
43 #define AP3212B_MODE_COMMAND 0x00
44 #define AP3212B_MODE_SHIFT (0)
45 #define AP3212B_MODE_MASK 0x07
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
53 #define AP3212B_OBJ_COMMAND 0x0f
54 #define AP3212B_OBJ_MASK 0x80
55 #define AP3212B_OBJ_SHIFT (7)
59 * register access helpers
62 static int __ap321xx_read_reg(struct i2c_client *client,
63 u32 reg, u8 mask, u8 shift)
67 val = i2c_smbus_read_byte_data(client, reg);
68 return (val & mask) >> shift;
71 static int __ap321xx_write_reg(struct i2c_client *client,
72 u32 reg, u8 mask, u8 shift, u8 val)
77 tmp = i2c_smbus_read_byte_data(client, reg);
81 ret = i2c_smbus_write_byte_data(client, reg, tmp);
88 * internally used functions
91 static int ap321xx_set_range(struct i2c_client *client, int range)
93 return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
94 AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
99 static int ap321xx_get_mode(struct i2c_client *client)
101 struct sensor_private_data *sensor =
102 (struct sensor_private_data *) i2c_get_clientdata(client);
105 ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
106 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
109 static int ap321xx_set_mode(struct i2c_client *client, int mode)
111 struct sensor_private_data *sensor =
112 (struct sensor_private_data *) i2c_get_clientdata(client);
115 ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
116 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
119 static int ap321xx_get_intstat(struct i2c_client *client)
121 struct sensor_private_data *sensor =
122 (struct sensor_private_data *) i2c_get_clientdata(client);
125 val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
126 val &= AP3212B_INT_MASK;
128 return val >> AP3212B_INT_SHIFT;
131 static int ap321xx_get_object(struct i2c_client *client)
135 val = i2c_smbus_read_byte_data(client, AP3212B_OBJ_COMMAND);
136 val &= AP3212B_OBJ_MASK;
138 return val >> AP3212B_OBJ_SHIFT;
142 static int ap321xx_product_detect(struct i2c_client *client)
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);
148 if ( mid == 0x01 && pid == 0x01 &&
149 (rid == 0x03 || rid == 0x04) )
151 //printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
153 else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
154 (mid == 0x02 && pid == 0x02 && rid == 0x01))
156 //printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
160 printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
167 static int ap321xx_init_client(struct i2c_client *client)
170 ap321xx_set_range(client, 0);
171 ap321xx_set_mode(client, 0);
176 static int ap321xx_psensor_enable(struct i2c_client *client)
180 mode = ap321xx_get_mode(client);
181 if((mode & 0x02) == 0){
183 ret = ap321xx_set_mode(client,mode);
189 static int ap321xx_psensor_disable(struct i2c_client *client)
193 mode = ap321xx_get_mode(client);
198 ret = ap321xx_set_mode(client,mode);
203 /****************operate according to sensor chip:start************/
205 static int sensor_active(struct i2c_client *client, int enable, int rate)
209 //register setting according to chip datasheet
211 result = ap321xx_psensor_enable(client);
214 result = ap321xx_psensor_disable(client);
217 printk("%s:fail to active sensor\n",__func__);
224 static int sensor_init(struct i2c_client *client)
226 struct sensor_private_data *sensor =
227 (struct sensor_private_data *) i2c_get_clientdata(client);
230 result = ap321xx_product_detect(client);
233 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
237 /* initialize the AP3212B chip */
238 result = ap321xx_init_client(client);
242 result = sensor->ops->active(client,0,0);
245 printk("%s:line=%d,error\n",__func__,__LINE__);
249 sensor->status_cur = SENSOR_OFF;
254 static int sensor_report_value(struct i2c_client *client)
256 struct sensor_private_data *sensor =
257 (struct sensor_private_data *) i2c_get_clientdata(client);
262 int_stat = ap321xx_get_intstat(client);
264 if (int_stat & AP3212B_INT_PMASK)
266 value = ap321xx_get_object(client);
267 input_report_abs(sensor->input_dev, ABS_DISTANCE, value);
268 input_sync(sensor->input_dev);
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,
290 .report = sensor_report_value,
293 /****************operate according to sensor chip:end************/
295 //function name should not be changed
296 static struct sensor_operate *proximity_get_ops(void)
298 return &proximity_ap321xx_ops;
302 static int __init proximity_ap321xx_init(void)
304 struct sensor_operate *ops = proximity_get_ops();
306 int type = ops->type;
307 result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);
311 static void __exit proximity_ap321xx_exit(void)
313 struct sensor_operate *ops = proximity_get_ops();
314 int type = ops->type;
315 sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);
319 module_init(proximity_ap321xx_init);
320 module_exit(proximity_ap321xx_exit);