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 CM3217_ADDR_COM1 0x10
\r
37 #define CM3217_ADDR_COM2 0x11
\r
38 #define CM3217_ADDR_DATA_MSB 0x10
\r
39 #define CM3217_ADDR_DATA_LSB 0x11
\r
41 #define CM3217_COM1_VALUE 0xA7 // (GAIN1:GAIN0)=10, (IT_T1:IT_TO)=01,WMD=1,SD=1,
\r
42 #define CM3217_COM2_VALUE 0xA0 //100ms
\r
44 #define CM3217_CLOSE 0x01
\r
48 /****************operate according to sensor chip:start************/
\r
50 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
52 struct sensor_private_data *sensor =
\r
53 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
57 sensor->client->addr = sensor->ops->ctrl_reg;
\r
58 sensor->ops->ctrl_data = sensor_read_reg_normal(client);
\r
60 //register setting according to chip datasheet
\r
63 status = CM3217_CLOSE; //cm3217
\r
64 sensor->ops->ctrl_data |= status;
\r
68 status = ~CM3217_CLOSE; //cm3217
\r
69 sensor->ops->ctrl_data &= status;
\r
72 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
\r
73 result = sensor_write_reg_normal(client, sensor->ops->ctrl_data);
\r
75 printk("%s:fail to active sensor\n",__func__);
\r
82 static int sensor_init(struct i2c_client *client)
\r
84 struct sensor_private_data *sensor =
\r
85 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
88 result = sensor->ops->active(client,0,0);
\r
91 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
95 sensor->status_cur = SENSOR_OFF;
\r
97 sensor->client->addr = sensor->ops->ctrl_reg;
\r
98 sensor->ops->ctrl_data = CM3217_COM1_VALUE;
\r
99 result = sensor_write_reg_normal(client, sensor->ops->ctrl_data);
\r
102 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
106 sensor->client->addr = CM3217_ADDR_COM2;
\r
107 result = sensor_write_reg_normal(client, CM3217_COM2_VALUE);
\r
110 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
118 static int light_report_value(struct input_dev *input, int data)
\r
120 unsigned char index = 0;
\r
123 index = 0;goto report;
\r
125 else if(data <= 160){
\r
126 index = 1;goto report;
\r
128 else if(data <= 225){
\r
129 index = 2;goto report;
\r
131 else if(data <= 320){
\r
132 index = 3;goto report;
\r
134 else if(data <= 640){
\r
135 index = 4;goto report;
\r
137 else if(data <= 1280){
\r
138 index = 5;goto report;
\r
140 else if(data <= 2600){
\r
141 index = 6;goto report;
\r
144 index = 7;goto report;
\r
148 input_report_abs(input, ABS_MISC, index);
\r
155 static int sensor_report_value(struct i2c_client *client)
\r
157 struct sensor_private_data *sensor =
\r
158 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
160 char msb = 0, lsb = 0;
\r
163 sensor->client->addr = CM3217_ADDR_DATA_LSB;
\r
164 sensor_rx_data_normal(sensor->client, &lsb, 1);
\r
165 sensor->client->addr = CM3217_ADDR_DATA_MSB;
\r
166 sensor_rx_data_normal(sensor->client, &msb, 1);
\r
167 result = ((msb << 8) | lsb) & 0xffff;
\r
169 index = light_report_value(sensor->input_dev, result);
\r
170 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, result,index);
\r
172 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
\r
175 result= sensor_read_reg(client, sensor->ops->int_status_reg);
\r
178 printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);
\r
186 struct sensor_operate light_cm3217_ops = {
\r
188 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
\r
189 .id_i2c = LIGHT_ID_CM3217, //i2c id number
\r
190 .read_reg = CM3217_ADDR_DATA_LSB, //read data
\r
191 .read_len = 2, //data length
\r
192 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
\r
193 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
194 .precision = 8, //8 bits
\r
195 .ctrl_reg = CM3217_ADDR_COM1, //enable or disable
\r
196 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
\r
197 .range = {100,65535}, //range
\r
198 .brightness ={10,255}, // brightness
\r
199 .trig = SENSOR_UNKNOW_DATA,
\r
200 .active = sensor_active,
\r
201 .init = sensor_init,
\r
202 .report = sensor_report_value,
\r
205 /****************operate according to sensor chip:end************/
\r
207 //function name should not be changed
\r
208 static struct sensor_operate *light_get_ops(void)
\r
210 return &light_cm3217_ops;
\r
214 static int __init light_cm3217_init(void)
\r
216 struct sensor_operate *ops = light_get_ops();
\r
218 int type = ops->type;
\r
219 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
\r
223 static void __exit light_cm3217_exit(void)
\r
225 struct sensor_operate *ops = light_get_ops();
\r
226 int type = ops->type;
\r
227 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
\r
231 module_init(light_cm3217_init);
\r
232 module_exit(light_cm3217_exit);
\r