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 <linux/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>
31 #endif
32 #include <linux/sensor-dev.h>
33
34
35 #define ALS_CMD 0x01
36 #define ALS_DT1 0x02
37 #define ALS_DT2 0X03
38 #define ALS_THDH1 0X04
39 #define ALS_THDH2 0X05
40 #define ALS_THDL1 0X06
41 #define ALS_THDL2 0X07
42 #define STA_TUS 0X08
43 #define PS_CMD 0X09
44 #define PS_DT 0X0A
45 #define PS_THDH 0X0B
46 #define PS_THDL 0X0C
47 #define SW_RESET 0X80
48
49 //ALS_CMD
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)
60
61 //PS_CMD
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)
78
79 //STA_TUS
80 #define STA_PS_INT (1<<5)
81 #define STA_ALS_INT (1<<4)
82
83
84 /****************operate according to sensor chip:start************/
85
sensor_active(struct i2c_client * client,int enable,int rate)86 static int sensor_active(struct i2c_client *client, int enable, int rate)
87 {
88 struct sensor_private_data *sensor =
89 (struct sensor_private_data *) i2c_get_clientdata(client);
90 int result = 0;
91 int status = 0;
92
93 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
94
95 //register setting according to chip datasheet
96 if(!enable)
97 {
98 status = PS_SD_DISABLE;
99 sensor->ops->ctrl_data |= status;
100 }
101 else
102 {
103 status = ~PS_SD_DISABLE;
104 sensor->ops->ctrl_data &= status;
105 }
106
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);
109 if(result)
110 printk("%s:fail to active sensor\n",__func__);
111
112 if(enable)
113 sensor->ops->report(sensor->client);
114
115 return result;
116
117 }
118
119
sensor_init(struct i2c_client * client)120 static int sensor_init(struct i2c_client *client)
121 {
122 struct sensor_private_data *sensor =
123 (struct sensor_private_data *) i2c_get_clientdata(client);
124 int result = 0;
125
126 result = sensor->ops->active(client,0,0);
127 if(result)
128 {
129 printk("%s:line=%d,error\n",__func__,__LINE__);
130 return result;
131 }
132
133 sensor->status_cur = SENSOR_OFF;
134
135 result = sensor_write_reg(client, SW_RESET, 0);
136 if(result)
137 {
138 printk("%s:line=%d,error\n",__func__,__LINE__);
139 return result;
140 }
141
142
143 sensor->ops->ctrl_data |= PS_15T_3MS| PS_SLP_90MS;
144 sensor->ops->ctrl_data &= ~TRIG_PS_AND_LS;
145
146 if(sensor->pdata->irq_enable)
147 sensor->ops->ctrl_data |= PS_INT_ENABLE;
148 else
149 sensor->ops->ctrl_data &= ~PS_INT_ENABLE;
150
151 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
152 if(result)
153 {
154 printk("%s:line=%d,error\n",__func__,__LINE__);
155 return result;
156 }
157
158 return result;
159 }
160
161
162
sensor_report_value(struct i2c_client * client)163 static int sensor_report_value(struct i2c_client *client)
164 {
165 struct sensor_private_data *sensor =
166 (struct sensor_private_data *) i2c_get_clientdata(client);
167 int result = 0;
168 int value = 0;
169 char buffer[1] = {0};
170
171 if(sensor->ops->read_len < 1) //sensor->ops->read_len = 1
172 {
173 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
174 return -1;
175 }
176
177 memset(buffer, 0, 1);
178
179 buffer[0] = sensor->ops->read_reg;
180 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
181 if(result)
182 {
183 printk("%s:line=%d,error\n",__func__,__LINE__);
184 return result;
185 }
186
187
188 value = buffer[0];
189
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);
193
194 if(sensor->pdata->irq_enable)
195 {
196 if(sensor->ops->int_status_reg)
197 {
198 value = sensor_read_reg(client, sensor->ops->int_status_reg);
199 }
200
201 if(value & STA_PS_INT)
202 {
203 value &= ~STA_PS_INT;
204 result = sensor_write_reg(client, sensor->ops->int_status_reg,value); //clear int
205 if(result)
206 {
207 printk("%s:line=%d,error\n",__func__,__LINE__);
208 return result;
209 }
210 }
211 }
212
213 return result;
214 }
215
216 static 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,
230 .init = sensor_init,
231 .report = sensor_report_value,
232 };
233
234 /****************operate according to sensor chip:end************/
proximity_stk3171_probe(struct i2c_client * client,const struct i2c_device_id * devid)235 static int proximity_stk3171_probe(struct i2c_client *client,
236 const struct i2c_device_id *devid)
237 {
238 return sensor_register_device(client, NULL, devid, &proximity_stk3171_ops);
239 }
240
proximity_stk3171_remove(struct i2c_client * client)241 static int proximity_stk3171_remove(struct i2c_client *client)
242 {
243 return sensor_unregister_device(client, NULL, &proximity_stk3171_ops);
244 }
245
246 static const struct i2c_device_id proximity_stk3171_id[] = {
247 {"ps_stk3171", PROXIMITY_ID_STK3171},
248 {}
249 };
250
251 static struct i2c_driver proximity_stk3171_driver = {
252 .probe = proximity_stk3171_probe,
253 .remove = proximity_stk3171_remove,
254 .shutdown = sensor_shutdown,
255 .id_table = proximity_stk3171_id,
256 .driver = {
257 .name = "proximity_stk3171",
258 #ifdef CONFIG_PM
259 .pm = &sensor_pm_ops,
260 #endif
261 },
262 };
263
264 module_i2c_driver(proximity_stk3171_driver);
265
266 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
267 MODULE_DESCRIPTION("ps_stk3171 proximity driver");
268 MODULE_LICENSE("GPL");
269