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 <mach/gpio.h>
\r
29 #include <mach/board.h>
\r
30 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
31 #include <linux/earlysuspend.h>
\r
33 #include <linux/sensor-dev.h>
\r
36 #define ALS_CMD 0x01
\r
37 #define ALS_DT1 0x02
\r
38 #define ALS_DT2 0X03
\r
39 #define ALS_THDH1 0X04
\r
40 #define ALS_THDH2 0X05
\r
41 #define ALS_THDL1 0X06
\r
42 #define ALS_THDL2 0X07
\r
43 #define STA_TUS 0X08
\r
46 #define PS_THDH 0X0B
\r
47 #define PS_THDL 0X0C
\r
48 #define SW_RESET 0X80
\r
51 #define ALS_SD_ENABLE (0<<0)
\r
52 #define ALS_SD_DISABLE (1<<0)
\r
53 #define ALS_INT_DISABLE (0<<1)
\r
54 #define ALS_INT_ENABLE (1<<1)
\r
55 #define ALS_1T_100MS (0<<2)
\r
56 #define ALS_2T_200MS (1<<2)
\r
57 #define ALS_4T_400MS (2<<2)
\r
58 #define ALS_8T_800MS (3<<2)
\r
59 #define ALS_RANGE_57671 (0<<6)
\r
60 #define ALS_RANGE_28836 (1<<6)
\r
63 #define PS_SD_ENABLE (0<<0)
\r
64 #define PS_SD_DISABLE (1<<0)
\r
65 #define PS_INT_DISABLE (0<<1)
\r
66 #define PS_INT_ENABLE (1<<1)
\r
67 #define PS_10T_2MS (0<<2)
\r
68 #define PS_15T_3MS (1<<2)
\r
69 #define PS_20T_4MS (2<<2)
\r
70 #define PS_25T_5MS (3<<2)
\r
71 #define PS_CUR_100MA (0<<4)
\r
72 #define PS_CUR_200MA (1<<4)
\r
73 #define PS_SLP_10MS (0<<5)
\r
74 #define PS_SLP_30MS (1<<5)
\r
75 #define PS_SLP_90MS (2<<5)
\r
76 #define PS_SLP_270MS (3<<5)
\r
77 #define TRIG_PS_OR_LS (0<<7)
\r
78 #define TRIG_PS_AND_LS (1<<7)
\r
81 #define STA_PS_INT (1<<5)
\r
82 #define STA_ALS_INT (1<<4)
\r
86 /****************operate according to sensor chip:start************/
\r
88 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
90 struct sensor_private_data *sensor =
\r
91 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
95 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
\r
97 //register setting according to chip datasheet
\r
100 status = ALS_SD_DISABLE;
\r
101 sensor->ops->ctrl_data |= status;
\r
105 status = ~ALS_SD_DISABLE;
\r
106 sensor->ops->ctrl_data &= status;
\r
109 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
\r
110 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
112 printk("%s:fail to active sensor\n",__func__);
\r
115 sensor->ops->report(sensor->client);
\r
122 static int sensor_init(struct i2c_client *client)
\r
124 struct sensor_private_data *sensor =
\r
125 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
128 result = sensor->ops->active(client,0,0);
\r
131 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
135 sensor->status_cur = SENSOR_OFF;
\r
137 result = sensor_write_reg(client, SW_RESET, 0);
\r
140 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
144 result = sensor_write_reg(client, ALS_THDH1, 0);//it is important,if not then als can not trig intterupt
\r
147 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
151 result = sensor_write_reg(client, ALS_THDH2, 0);
\r
154 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
158 sensor->ops->ctrl_data |= ALS_1T_100MS;
\r
160 if(sensor->pdata->irq_enable)
\r
161 sensor->ops->ctrl_data |= ALS_INT_ENABLE;
\r
163 sensor->ops->ctrl_data &= ~ALS_INT_ENABLE;
\r
165 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
168 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
176 static int light_report_value(struct input_dev *input, int data)
\r
178 unsigned char index = 0;
\r
180 index = 0;goto report;
\r
182 else if(data <= 1600){
\r
183 index = 1;goto report;
\r
185 else if(data <= 2250){
\r
186 index = 2;goto report;
\r
188 else if(data <= 3200){
\r
189 index = 3;goto report;
\r
191 else if(data <= 6400){
\r
192 index = 4;goto report;
\r
194 else if(data <= 12800){
\r
195 index = 5;goto report;
\r
197 else if(data <= 26000){
\r
198 index = 6;goto report;
\r
201 index = 7;goto report;
\r
205 input_report_abs(input, ABS_MISC, index);
\r
212 static int sensor_report_value(struct i2c_client *client)
\r
214 struct sensor_private_data *sensor =
\r
215 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
218 char buffer[2] = {0};
\r
221 if(sensor->ops->read_len < 2) //sensor->ops->read_len = 2
\r
223 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
\r
227 memset(buffer, 0, 2);
\r
229 buffer[0] = sensor->ops->read_reg;
\r
230 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
\r
233 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
237 value = (buffer[0] << 8) | buffer[1];
\r
240 index = light_report_value(sensor->input_dev, value);
\r
242 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
\r
244 if(sensor->pdata->irq_enable)
\r
246 if(sensor->ops->int_status_reg)
\r
248 value = sensor_read_reg(client, sensor->ops->int_status_reg);
\r
251 if(value & STA_ALS_INT)
\r
253 value &= ~STA_ALS_INT;
\r
254 result = sensor_write_reg(client, sensor->ops->int_status_reg,value); //clear int
\r
257 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
267 struct sensor_operate light_stk3171_ops = {
\r
268 .name = "ls_stk3171",
\r
269 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
\r
270 .id_i2c = LIGHT_ID_STK3171, //i2c id number
\r
271 .read_reg = ALS_DT1, //read data
\r
272 .read_len = 2, //data length
\r
273 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
\r
274 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
275 .precision = 16, //8 bits
\r
276 .ctrl_reg = ALS_CMD, //enable or disable
\r
277 .int_status_reg = STA_TUS, //intterupt status register
\r
278 .range = {100,65535}, //range
\r
279 .brightness ={10,255}, //brightness
\r
280 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
\r
281 .active = sensor_active,
\r
282 .init = sensor_init,
\r
283 .report = sensor_report_value,
\r
286 /****************operate according to sensor chip:end************/
\r
288 //function name should not be changed
\r
289 static struct sensor_operate *light_get_ops(void)
\r
291 return &light_stk3171_ops;
\r
295 static int __init light_stk3171_init(void)
\r
297 struct sensor_operate *ops = light_get_ops();
\r
299 int type = ops->type;
\r
300 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
\r
304 static void __exit light_stk3171_exit(void)
\r
306 struct sensor_operate *ops = light_get_ops();
\r
307 int type = ops->type;
\r
308 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
\r
312 module_init(light_stk3171_init);
\r
313 module_exit(light_stk3171_exit);
\r