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>
35 #define CM3232_CLOSE 0x01
38 #define CM3232_ADDR_COM 0
39 #define CM3232_ADDR_DATA 50
41 #define CM3232_DRV_NAME "cm3232"
43 #define COMMAND_CTRL 0
44 #define COMMAND_ALS_DATA 50 //ALS: 15:8 MSB 8bits data
48 #define ALS_RESET(x) (((x)&1)<<6) //0 = Reset disable; 1 = Reset enable
49 #define ALS_IT(x) (((x)&7)<<2) //ALS integration time setting
50 #define HIGH_SENSITIVITY(x) (((x)&1)<<1) //0 = Normal mode; 1 = High sensitivity mode
51 #define SHUT_DOWN(x) (((x)&1)<<0) //ALS shut down setting: 0 = ALS Power on ; 1 = ALS Shut down
53 #define ALS_IT100MS 0 //100ms
54 #define ALS_IT200MS 1 //200ms
55 #define ALS_IT400MS 2 //400ms
56 #define ALS_IT800MS 3 //800ms
57 #define ALS_IT1600MS 4 //1600ms
58 #define ALS_IT3200MS 5 //3200ms
61 /****************operate according to sensor chip:start************/
63 static int sensor_active(struct i2c_client *client, int enable, int rate)
65 struct sensor_private_data *sensor =
66 (struct sensor_private_data *) i2c_get_clientdata(client);
70 //sensor->client->addr = sensor->ops->ctrl_reg;
71 //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
72 //printk("%s: client addr = %#x\n\n", __func__, client->addr);
73 //register setting according to chip datasheet
75 sensor->ops->ctrl_data = ALS_RESET(1);
76 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
78 printk("%s:fail to active sensor\n",__func__);
85 sensor->ops->ctrl_data = ALS_IT(ALS_IT200MS) | HIGH_SENSITIVITY(1);
89 sensor->ops->ctrl_data = SHUT_DOWN(1);
92 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
94 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
96 printk("%s:fail to active sensor\n",__func__);
103 static int sensor_init(struct i2c_client *client)
105 struct sensor_private_data *sensor =
106 (struct sensor_private_data *) i2c_get_clientdata(client);
109 result = sensor->ops->active(client,0,0);
112 printk("%s:line=%d,error\n",__func__,__LINE__);
116 sensor->status_cur = SENSOR_OFF;
121 static int light_report_value(struct input_dev *input, int data)
123 unsigned char index = 0;
126 index = 0;goto report;
128 else if(data <= 160){
129 index = 1;goto report;
131 else if(data <= 225){
132 index = 2;goto report;
134 else if(data <= 320){
135 index = 3;goto report;
137 else if(data <= 640){
138 index = 4;goto report;
140 else if(data <= 1280){
141 index = 5;goto report;
143 else if(data <= 2600){
144 index = 6;goto report;
147 index = 7;goto report;
151 input_report_abs(input, ABS_MISC, index);
158 static int sensor_report_value(struct i2c_client *client)
160 struct sensor_private_data *sensor =
161 (struct sensor_private_data *) i2c_get_clientdata(client);
163 //char msb = 0, lsb = 0;
165 unsigned short value = 0;
168 //sensor->client->addr = CM3232_ADDR_DATA;
169 data[0] = CM3232_ADDR_DATA;
170 sensor_rx_data(sensor->client, data, 2);
171 value = (data[1] << 8) | data[0] ;
173 DBG("%s:result=%d\n",__func__,value);
174 //printk("%s:result=%d\n",__func__,value);
175 index = light_report_value(sensor->input_dev, value);
176 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
178 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
181 result= sensor_read_reg(client, sensor->ops->int_status_reg);
184 printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);
192 struct sensor_operate light_cm3232_ops = {
194 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
195 .id_i2c = LIGHT_ID_CM3232, //i2c id number
196 .read_reg = CM3232_ADDR_DATA, //read data
197 .read_len = 2, //data length
198 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
199 .id_data = SENSOR_UNKNOW_DATA, //device id
200 .precision = 8, //8 bits
201 .ctrl_reg = CM3232_ADDR_COM, //enable or disable
202 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
203 .range = {100,65535}, //range
204 .brightness = {10,255}, // brightness
205 .trig = SENSOR_UNKNOW_DATA,
206 .active = sensor_active,
208 .report = sensor_report_value,
211 /****************operate according to sensor chip:end************/
213 //function name should not be changed
214 static struct sensor_operate *light_get_ops(void)
216 return &light_cm3232_ops;
220 static int __init light_cm3232_init(void)
222 struct sensor_operate *ops = light_get_ops();
224 int type = ops->type;
225 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
229 static void __exit light_cm3232_exit(void)
231 struct sensor_operate *ops = light_get_ops();
232 int type = ops->type;
233 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
237 module_init(light_cm3232_init);
238 module_exit(light_cm3232_exit);