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)
85 /****************operate according to sensor chip:start************/
87 static int sensor_active(struct i2c_client *client, int enable, int rate)
89 struct sensor_private_data *sensor =
90 (struct sensor_private_data *) i2c_get_clientdata(client);
94 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
96 //register setting according to chip datasheet
99 status = ALS_SD_DISABLE;
100 sensor->ops->ctrl_data |= status;
104 status = ~ALS_SD_DISABLE;
105 sensor->ops->ctrl_data &= status;
108 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
109 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
111 printk("%s:fail to active sensor\n",__func__);
114 sensor->ops->report(sensor->client);
121 static int sensor_init(struct i2c_client *client)
123 struct sensor_private_data *sensor =
124 (struct sensor_private_data *) i2c_get_clientdata(client);
127 result = sensor->ops->active(client,0,0);
130 printk("%s:line=%d,error\n",__func__,__LINE__);
134 sensor->status_cur = SENSOR_OFF;
136 result = sensor_write_reg(client, SW_RESET, 0);
139 printk("%s:line=%d,error\n",__func__,__LINE__);
143 result = sensor_write_reg(client, ALS_THDH1, 0);//it is important,if not then als can not trig intterupt
146 printk("%s:line=%d,error\n",__func__,__LINE__);
150 result = sensor_write_reg(client, ALS_THDH2, 0);
153 printk("%s:line=%d,error\n",__func__,__LINE__);
157 sensor->ops->ctrl_data |= ALS_1T_100MS;
159 if(sensor->pdata->irq_enable)
160 sensor->ops->ctrl_data |= ALS_INT_ENABLE;
162 sensor->ops->ctrl_data &= ~ALS_INT_ENABLE;
164 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
167 printk("%s:line=%d,error\n",__func__,__LINE__);
175 static int light_report_value(struct input_dev *input, int data)
177 unsigned char index = 0;
179 index = 0;goto report;
181 else if(data <= 1600){
182 index = 1;goto report;
184 else if(data <= 2250){
185 index = 2;goto report;
187 else if(data <= 3200){
188 index = 3;goto report;
190 else if(data <= 6400){
191 index = 4;goto report;
193 else if(data <= 12800){
194 index = 5;goto report;
196 else if(data <= 26000){
197 index = 6;goto report;
200 index = 7;goto report;
204 input_report_abs(input, ABS_MISC, index);
211 static int sensor_report_value(struct i2c_client *client)
213 struct sensor_private_data *sensor =
214 (struct sensor_private_data *) i2c_get_clientdata(client);
217 char buffer[2] = {0};
220 if(sensor->ops->read_len < 2) //sensor->ops->read_len = 2
222 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
226 memset(buffer, 0, 2);
228 buffer[0] = sensor->ops->read_reg;
229 result = sensor_rx_data(client, buffer, sensor->ops->read_len);
232 printk("%s:line=%d,error\n",__func__,__LINE__);
236 value = (buffer[0] << 8) | buffer[1];
239 index = light_report_value(sensor->input_dev, value);
241 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
243 if(sensor->pdata->irq_enable)
245 if(sensor->ops->int_status_reg)
247 value = sensor_read_reg(client, sensor->ops->int_status_reg);
250 if(value & STA_ALS_INT)
252 value &= ~STA_ALS_INT;
253 result = sensor_write_reg(client, sensor->ops->int_status_reg,value); //clear int
256 printk("%s:line=%d,error\n",__func__,__LINE__);
266 struct sensor_operate light_stk3171_ops = {
267 .name = "ls_stk3171",
268 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
269 .id_i2c = LIGHT_ID_STK3171, //i2c id number
270 .read_reg = ALS_DT1, //read data
271 .read_len = 2, //data length
272 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
273 .id_data = SENSOR_UNKNOW_DATA, //device id
274 .precision = 16, //8 bits
275 .ctrl_reg = ALS_CMD, //enable or disable
276 .int_status_reg = STA_TUS, //intterupt status register
277 .range = {100,65535}, //range
278 .brightness ={10,255}, //brightness
279 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
280 .active = sensor_active,
282 .report = sensor_report_value,
285 /****************operate according to sensor chip:end************/
287 //function name should not be changed
288 static struct sensor_operate *light_get_ops(void)
290 return &light_stk3171_ops;
294 static int __init light_stk3171_init(void)
296 struct sensor_operate *ops = light_get_ops();
298 int type = ops->type;
299 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
303 static void __exit light_stk3171_exit(void)
305 struct sensor_operate *ops = light_get_ops();
306 int type = ops->type;
307 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
311 module_init(light_stk3171_init);
312 module_exit(light_stk3171_exit);