1 /* drivers/input/sensors/access/kxtik.c
\r
3 * Copyright (C) 2012-2015 ROCKCHIP.
\r
4 * Author: luowei <lw@rock-chips.com>
\r
6 * This software is licensed under the terms of the GNU General Public
\r
7 * License version 2, as published by the Free Software Foundation, and
\r
8 * may be copied, distributed, and modified under those terms.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
16 #include <linux/interrupt.h>
\r
17 #include <linux/i2c.h>
\r
18 #include <linux/slab.h>
\r
19 #include <linux/irq.h>
\r
20 #include <linux/miscdevice.h>
\r
21 #include <linux/gpio.h>
\r
22 #include <asm/uaccess.h>
\r
23 #include <asm/atomic.h>
\r
24 #include <linux/delay.h>
\r
25 #include <linux/input.h>
\r
26 #include <linux/workqueue.h>
\r
27 #include <linux/freezer.h>
\r
28 #include <linux/of_gpio.h>
\r
29 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
30 #include <linux/earlysuspend.h>
\r
32 #include <linux/sensor-dev.h>
\r
35 #define ALS_CMD 0x01
\r
36 #define ALS_DT1 0x02
\r
37 #define ALS_DT2 0X03
\r
38 #define ALS_THDH1 0X04
\r
39 #define ALS_THDH2 0X05
\r
40 #define ALS_THDL1 0X06
\r
41 #define ALS_THDL2 0X07
\r
42 #define STA_TUS 0X08
\r
45 #define PS_THDH 0X0B
\r
46 #define PS_THDL 0X0C
\r
47 #define SW_RESET 0X80
\r
50 #define ALS_SD_ENABLE (0<<0)
\r
51 #define ALS_SD_DISABLE (1<<0)
\r
52 #define ALS_INT_DISABLE (0<<1)
\r
53 #define ALS_INT_ENABLE (1<<1)
\r
54 #define ALS_1T_100MS (0<<2)
\r
55 #define ALS_2T_200MS (1<<2)
\r
56 #define ALS_4T_400MS (2<<2)
\r
57 #define ALS_8T_800MS (3<<2)
\r
58 #define ALS_RANGE_57671 (0<<6)
\r
59 #define ALS_RANGE_28836 (1<<6)
\r
62 #define PS_SD_ENABLE (0<<0)
\r
63 #define PS_SD_DISABLE (1<<0)
\r
64 #define PS_INT_DISABLE (0<<1)
\r
65 #define PS_INT_ENABLE (1<<1)
\r
66 #define PS_10T_2MS (0<<2)
\r
67 #define PS_15T_3MS (1<<2)
\r
68 #define PS_20T_4MS (2<<2)
\r
69 #define PS_25T_5MS (3<<2)
\r
70 #define PS_CUR_100MA (0<<4)
\r
71 #define PS_CUR_200MA (1<<4)
\r
72 #define PS_SLP_10MS (0<<5)
\r
73 #define PS_SLP_30MS (1<<5)
\r
74 #define PS_SLP_90MS (2<<5)
\r
75 #define PS_SLP_270MS (3<<5)
\r
76 #define TRIG_PS_OR_LS (0<<7)
\r
77 #define TRIG_PS_AND_LS (1<<7)
\r
80 #define STA_PS_INT (1<<5)
\r
81 #define STA_ALS_INT (1<<4)
\r
84 /****************operate according to sensor chip:start************/
\r
86 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
88 struct sensor_private_data *sensor =
\r
89 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
93 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
\r
95 //register setting according to chip datasheet
\r
98 status = PS_SD_DISABLE;
\r
99 sensor->ops->ctrl_data |= status;
\r
103 status = ~PS_SD_DISABLE;
\r
104 sensor->ops->ctrl_data &= status;
\r
107 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
\r
108 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
110 printk("%s:fail to active sensor\n",__func__);
\r
113 sensor->ops->report(sensor->client);
\r
120 static int sensor_init(struct i2c_client *client)
\r
122 struct sensor_private_data *sensor =
\r
123 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
126 result = sensor->ops->active(client,0,0);
\r
129 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
133 sensor->status_cur = SENSOR_OFF;
\r
135 result = sensor_write_reg(client, SW_RESET, 0);
\r
138 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
143 sensor->ops->ctrl_data |= PS_15T_3MS| PS_SLP_90MS;
\r
144 sensor->ops->ctrl_data &= ~TRIG_PS_AND_LS;
\r
146 if(sensor->pdata->irq_enable)
\r
147 sensor->ops->ctrl_data |= PS_INT_ENABLE;
\r
149 sensor->ops->ctrl_data &= ~PS_INT_ENABLE;
\r
151 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
154 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
163 static int sensor_report_value(struct i2c_client *client)
\r
165 struct sensor_private_data *sensor =
\r
166 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
169 char buffer[1] = {0};
\r
171 if(sensor->ops->read_len < 1) //sensor->ops->read_len = 1
\r
173 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
\r
177 memset(buffer, 0, 1);
\r
179 buffer[0] = sensor->ops->read_reg;
\r
180 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
\r
183 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
190 input_report_abs(sensor->input_dev, ABS_DISTANCE, (value>>2)?0:1);
\r
191 input_sync(sensor->input_dev);
\r
192 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,(value>>2)?0:1);
\r
194 if(sensor->pdata->irq_enable)
\r
196 if(sensor->ops->int_status_reg)
\r
198 value = sensor_read_reg(client, sensor->ops->int_status_reg);
\r
201 if(value & STA_PS_INT)
\r
203 value &= ~STA_PS_INT;
\r
204 result = sensor_write_reg(client, sensor->ops->int_status_reg,value); //clear int
\r
207 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
216 struct sensor_operate proximity_stk3171_ops = {
\r
217 .name = "ps_stk3171",
\r
218 .type = SENSOR_TYPE_PROXIMITY, //sensor type and it should be correct
\r
219 .id_i2c = PROXIMITY_ID_STK3171, //i2c id number
\r
220 .read_reg = PS_DT, //read data
\r
221 .read_len = 1, //data length
\r
222 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
\r
223 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
224 .precision = 8, //8 bits
\r
225 .ctrl_reg = PS_CMD, //enable or disable
\r
226 .int_status_reg = STA_TUS, //intterupt status register
\r
227 .range = {0,1}, //range
\r
228 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
\r
229 .active = sensor_active,
\r
230 .init = sensor_init,
\r
231 .report = sensor_report_value,
\r
234 /****************operate according to sensor chip:end************/
\r
236 //function name should not be changed
\r
237 static struct sensor_operate *proximity_get_ops(void)
\r
239 return &proximity_stk3171_ops;
\r
242 static int __init proximity_stk3171_init(void)
\r
244 struct sensor_operate *ops = proximity_get_ops();
\r
246 int type = ops->type;
\r
247 result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);
\r
251 static void __exit proximity_stk3171_exit(void)
\r
253 struct sensor_operate *ops = proximity_get_ops();
\r
254 int type = ops->type;
\r
255 sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);
\r
259 module_init(proximity_stk3171_init);
\r
260 module_exit(proximity_stk3171_exit);
\r