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 <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
32 #include <linux/sensor-dev.h>
38 #define ALS_THDH1 0X04
39 #define ALS_THDH2 0X05
40 #define ALS_THDL1 0X06
41 #define ALS_THDL2 0X07
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)
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)
80 #define STA_PS_INT (1<<5)
81 #define STA_ALS_INT (1<<4)
84 /****************operate according to sensor chip:start************/
86 static int sensor_active(struct i2c_client *client, int enable, int rate)
88 struct sensor_private_data *sensor =
89 (struct sensor_private_data *) i2c_get_clientdata(client);
93 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
95 //register setting according to chip datasheet
98 status = PS_SD_DISABLE;
99 sensor->ops->ctrl_data |= status;
103 status = ~PS_SD_DISABLE;
104 sensor->ops->ctrl_data &= status;
107 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
108 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
110 printk("%s:fail to active sensor\n",__func__);
113 sensor->ops->report(sensor->client);
120 static int sensor_init(struct i2c_client *client)
122 struct sensor_private_data *sensor =
123 (struct sensor_private_data *) i2c_get_clientdata(client);
126 result = sensor->ops->active(client,0,0);
129 printk("%s:line=%d,error\n",__func__,__LINE__);
133 sensor->status_cur = SENSOR_OFF;
135 result = sensor_write_reg(client, SW_RESET, 0);
138 printk("%s:line=%d,error\n",__func__,__LINE__);
143 sensor->ops->ctrl_data |= PS_15T_3MS| PS_SLP_90MS;
144 sensor->ops->ctrl_data &= ~TRIG_PS_AND_LS;
146 if(sensor->pdata->irq_enable)
147 sensor->ops->ctrl_data |= PS_INT_ENABLE;
149 sensor->ops->ctrl_data &= ~PS_INT_ENABLE;
151 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
154 printk("%s:line=%d,error\n",__func__,__LINE__);
163 static int sensor_report_value(struct i2c_client *client)
165 struct sensor_private_data *sensor =
166 (struct sensor_private_data *) i2c_get_clientdata(client);
169 char buffer[1] = {0};
171 if(sensor->ops->read_len < 1) //sensor->ops->read_len = 1
173 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
177 memset(buffer, 0, 1);
179 buffer[0] = sensor->ops->read_reg;
180 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
183 printk("%s:line=%d,error\n",__func__,__LINE__);
190 input_report_abs(sensor->input_dev, ABS_DISTANCE, (value>>2)?0:1);
191 input_sync(sensor->input_dev);
192 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,(value>>2)?0:1);
194 if(sensor->pdata->irq_enable)
196 if(sensor->ops->int_status_reg)
198 value = sensor_read_reg(client, sensor->ops->int_status_reg);
201 if(value & STA_PS_INT)
203 value &= ~STA_PS_INT;
204 result = sensor_write_reg(client, sensor->ops->int_status_reg,value); //clear int
207 printk("%s:line=%d,error\n",__func__,__LINE__);
216 struct sensor_operate proximity_stk3171_ops = {
217 .name = "ps_stk3171",
218 .type = SENSOR_TYPE_PROXIMITY, //sensor type and it should be correct
219 .id_i2c = PROXIMITY_ID_STK3171, //i2c id number
220 .read_reg = PS_DT, //read data
221 .read_len = 1, //data length
222 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
223 .id_data = SENSOR_UNKNOW_DATA, //device id
224 .precision = 8, //8 bits
225 .ctrl_reg = PS_CMD, //enable or disable
226 .int_status_reg = STA_TUS, //intterupt status register
227 .range = {0,1}, //range
228 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
229 .active = sensor_active,
231 .report = sensor_report_value,
234 /****************operate according to sensor chip:end************/
236 //function name should not be changed
237 static struct sensor_operate *proximity_get_ops(void)
239 return &proximity_stk3171_ops;
242 static int __init proximity_stk3171_init(void)
244 struct sensor_operate *ops = proximity_get_ops();
246 int type = ops->type;
247 result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);
251 static void __exit proximity_stk3171_exit(void)
253 struct sensor_operate *ops = proximity_get_ops();
254 int type = ops->type;
255 sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);
259 module_init(proximity_stk3171_init);
260 module_exit(proximity_stk3171_exit);