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 CM3217_ADDR_COM1 0x10
36 #define CM3217_ADDR_COM2 0x11
37 #define CM3217_ADDR_DATA_MSB 0x10
38 #define CM3217_ADDR_DATA_LSB 0x11
40 #define CM3217_COM1_VALUE 0xA7 // (GAIN1:GAIN0)=10, (IT_T1:IT_TO)=01,WMD=1,SD=1,
41 #define CM3217_COM2_VALUE 0xA0 //100ms
43 #define CM3217_CLOSE 0x01
47 /****************operate according to sensor chip:start************/
49 static int sensor_active(struct i2c_client *client, int enable, int rate)
51 struct sensor_private_data *sensor =
52 (struct sensor_private_data *) i2c_get_clientdata(client);
56 sensor->client->addr = sensor->ops->ctrl_reg;
57 sensor->ops->ctrl_data = sensor_read_reg_normal(client);
59 //register setting according to chip datasheet
62 status = CM3217_CLOSE; //cm3217
63 sensor->ops->ctrl_data |= status;
67 status = ~CM3217_CLOSE; //cm3217
68 sensor->ops->ctrl_data &= status;
71 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
72 result = sensor_write_reg_normal(client, sensor->ops->ctrl_data);
74 printk("%s:fail to active sensor\n",__func__);
81 static int sensor_init(struct i2c_client *client)
83 struct sensor_private_data *sensor =
84 (struct sensor_private_data *) i2c_get_clientdata(client);
87 result = sensor->ops->active(client,0,0);
90 printk("%s:line=%d,error\n",__func__,__LINE__);
94 sensor->status_cur = SENSOR_OFF;
96 sensor->client->addr = sensor->ops->ctrl_reg;
97 sensor->ops->ctrl_data = CM3217_COM1_VALUE;
98 result = sensor_write_reg_normal(client, sensor->ops->ctrl_data);
101 printk("%s:line=%d,error\n",__func__,__LINE__);
105 sensor->client->addr = CM3217_ADDR_COM2;
106 result = sensor_write_reg_normal(client, CM3217_COM2_VALUE);
109 printk("%s:line=%d,error\n",__func__,__LINE__);
117 static int light_report_value(struct input_dev *input, int data)
119 unsigned char index = 0;
122 index = 0;goto report;
124 else if(data <= 160){
125 index = 1;goto report;
127 else if(data <= 225){
128 index = 2;goto report;
130 else if(data <= 320){
131 index = 3;goto report;
133 else if(data <= 640){
134 index = 4;goto report;
136 else if(data <= 1280){
137 index = 5;goto report;
139 else if(data <= 2600){
140 index = 6;goto report;
143 index = 7;goto report;
147 input_report_abs(input, ABS_MISC, index);
154 static int sensor_report_value(struct i2c_client *client)
156 struct sensor_private_data *sensor =
157 (struct sensor_private_data *) i2c_get_clientdata(client);
159 char msb = 0, lsb = 0;
162 sensor->client->addr = CM3217_ADDR_DATA_LSB;
163 sensor_rx_data_normal(sensor->client, &lsb, 1);
164 sensor->client->addr = CM3217_ADDR_DATA_MSB;
165 sensor_rx_data_normal(sensor->client, &msb, 1);
166 result = ((msb << 8) | lsb) & 0xffff;
168 index = light_report_value(sensor->input_dev, result);
169 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, result,index);
171 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
174 result= sensor_read_reg(client, sensor->ops->int_status_reg);
177 printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);
185 struct sensor_operate light_cm3217_ops = {
187 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
188 .id_i2c = LIGHT_ID_CM3217, //i2c id number
189 .read_reg = CM3217_ADDR_DATA_LSB, //read data
190 .read_len = 2, //data length
191 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
192 .id_data = SENSOR_UNKNOW_DATA, //device id
193 .precision = 8, //8 bits
194 .ctrl_reg = CM3217_ADDR_COM1, //enable or disable
195 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
196 .range = {100,65535}, //range
197 .brightness ={10,255}, // brightness
198 .trig = SENSOR_UNKNOW_DATA,
199 .active = sensor_active,
201 .report = sensor_report_value,
204 /****************operate according to sensor chip:end************/
206 //function name should not be changed
207 static struct sensor_operate *light_get_ops(void)
209 return &light_cm3217_ops;
213 static int __init light_cm3217_init(void)
215 struct sensor_operate *ops = light_get_ops();
217 int type = ops->type;
218 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
222 static void __exit light_cm3217_exit(void)
224 struct sensor_operate *ops = light_get_ops();
225 int type = ops->type;
226 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
230 module_init(light_cm3217_init);
231 module_exit(light_cm3217_exit);