dd20d5fb9334b83ca8040acc17aea388f8b5669d
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / cm3217.c
1 /* drivers/input/sensors/access/kxtik.c\r
2  *\r
3  * Copyright (C) 2012-2015 ROCKCHIP.\r
4  * Author: luowei <lw@rock-chips.com>\r
5  *\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
9  *\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
14  *\r
15  */\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
32 #endif\r
33 #include <linux/sensor-dev.h>\r
34 \r
35 \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
40 \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
43 \r
44 #define CM3217_CLOSE    0x01\r
45 \r
46 \r
47 \r
48 /****************operate according to sensor chip:start************/\r
49 \r
50 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
51 {\r
52         struct sensor_private_data *sensor =\r
53             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
54         int result = 0;\r
55         int status = 0;\r
56         \r
57         sensor->client->addr = sensor->ops->ctrl_reg;   \r
58         sensor->ops->ctrl_data = sensor_read_reg_normal(client);\r
59         \r
60         //register setting according to chip datasheet          \r
61         if(!enable)\r
62         {       \r
63                 status = CM3217_CLOSE;  //cm3217        \r
64                 sensor->ops->ctrl_data |= status;       \r
65         }\r
66         else\r
67         {\r
68                 status = ~CM3217_CLOSE; //cm3217\r
69                 sensor->ops->ctrl_data &= status;\r
70         }\r
71 \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
74         if(result)\r
75                 printk("%s:fail to active sensor\n",__func__);\r
76         \r
77         return result;\r
78 \r
79 }\r
80 \r
81 \r
82 static int sensor_init(struct i2c_client *client)\r
83 {       \r
84         struct sensor_private_data *sensor =\r
85             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
86         int result = 0;\r
87         \r
88         result = sensor->ops->active(client,0,0);\r
89         if(result)\r
90         {\r
91                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
92                 return result;\r
93         }\r
94         \r
95         sensor->status_cur = SENSOR_OFF;\r
96         \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
100         if(result)\r
101         {\r
102                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
103                 return result;\r
104         }\r
105         \r
106         sensor->client->addr = CM3217_ADDR_COM2;        \r
107         result = sensor_write_reg_normal(client, CM3217_COM2_VALUE);\r
108         if(result)\r
109         {\r
110                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
111                 return result;\r
112         }\r
113         \r
114         return result;\r
115 }\r
116 \r
117 \r
118 static int light_report_value(struct input_dev *input, int data)\r
119 {\r
120         unsigned char index = 0;\r
121         \r
122         if(data <= 10){\r
123                 index = 0;goto report;\r
124         }\r
125         else if(data <= 160){\r
126                 index = 1;goto report;\r
127         }\r
128         else if(data <= 225){\r
129                 index = 2;goto report;\r
130         }\r
131         else if(data <= 320){\r
132                 index = 3;goto report;\r
133         }\r
134         else if(data <= 640){\r
135                 index = 4;goto report;\r
136         }\r
137         else if(data <= 1280){\r
138                 index = 5;goto report;\r
139         }\r
140         else if(data <= 2600){\r
141                 index = 6;goto report;\r
142         }\r
143         else{\r
144                 index = 7;goto report;\r
145         }\r
146 \r
147 report:\r
148         input_report_abs(input, ABS_MISC, index);\r
149         input_sync(input);\r
150 \r
151         return index;\r
152 }\r
153 \r
154 \r
155 static int sensor_report_value(struct i2c_client *client)\r
156 {\r
157         struct sensor_private_data *sensor =\r
158             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
159         int result = 0;\r
160         char msb = 0, lsb = 0;\r
161         int index = 0;\r
162         \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
168         \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
171         \r
172         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register\r
173         {\r
174                 \r
175                 result= sensor_read_reg(client, sensor->ops->int_status_reg);\r
176                 if(result)\r
177                 {\r
178                         printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);\r
179                 }\r
180         }\r
181         \r
182         return result;\r
183 }\r
184 \r
185 \r
186 struct sensor_operate light_cm3217_ops = {\r
187         .name                           = "cm3217",\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
203 };\r
204 \r
205 /****************operate according to sensor chip:end************/\r
206 \r
207 //function name should not be changed\r
208 static struct sensor_operate *light_get_ops(void)\r
209 {\r
210         return &light_cm3217_ops;\r
211 }\r
212 \r
213 \r
214 static int __init light_cm3217_init(void)\r
215 {\r
216         struct sensor_operate *ops = light_get_ops();\r
217         int result = 0;\r
218         int type = ops->type;\r
219         result = sensor_register_slave(type, NULL, NULL, light_get_ops);\r
220         return result;\r
221 }\r
222 \r
223 static void __exit light_cm3217_exit(void)\r
224 {\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
228 }\r
229 \r
230 \r
231 module_init(light_cm3217_init);\r
232 module_exit(light_cm3217_exit);\r
233 \r
234 \r