65b234bd383538f468a803b7188247e439124b09
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / cm3232.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 CM3232_CLOSE    0x01\r
37 \r
38 \r
39 #define CM3232_ADDR_COM 0\r
40 #define CM3232_ADDR_DATA 50     \r
41 \r
42 #define CM3232_DRV_NAME "cm3232"\r
43 //command code\r
44 #define COMMAND_CTRL            0\r
45 #define COMMAND_ALS_DATA        50              //ALS: 15:8 MSB 8bits data\r
46                                                 //7:0 LSB 8bits data\r
47 \r
48 //ctrl bit\r
49 #define ALS_RESET(x)                    (((x)&1)<<6) //0 = Reset disable; 1 = Reset enable\r
50 #define ALS_IT(x)                       (((x)&7)<<2) //ALS integration time setting\r
51 #define HIGH_SENSITIVITY(x)             (((x)&1)<<1) //0 = Normal mode; 1 = High sensitivity mode\r
52 #define SHUT_DOWN(x)                    (((x)&1)<<0) //ALS shut down setting: 0 = ALS Power on ; 1 = ALS Shut down\r
53 \r
54 #define ALS_IT100MS     0               //100ms\r
55 #define ALS_IT200MS     1               //200ms\r
56 #define ALS_IT400MS     2               //400ms\r
57 #define ALS_IT800MS     3               //800ms\r
58 #define ALS_IT1600MS    4               //1600ms\r
59 #define ALS_IT3200MS    5               //3200ms\r
60 \r
61 \r
62 /****************operate according to sensor chip:start************/\r
63 \r
64 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
65 {\r
66         struct sensor_private_data *sensor =\r
67             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
68         int result = 0;\r
69         //int status = 0;\r
70         \r
71         //sensor->client->addr = sensor->ops->ctrl_reg; \r
72         //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
73         //printk("%s:  client addr = %#x\n\n", __func__, client->addr); \r
74         //register setting according to chip datasheet          \r
75         if (enable) {\r
76                 sensor->ops->ctrl_data = ALS_RESET(1);  \r
77                 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
78                 if(result) {\r
79                         printk("%s:fail to active sensor\n",__func__);\r
80                         return -1;\r
81                 }\r
82         }\r
83 \r
84         if(enable)\r
85         {       \r
86                 sensor->ops->ctrl_data = ALS_IT(ALS_IT200MS) | HIGH_SENSITIVITY(1);\r
87         }\r
88         else\r
89         {\r
90                 sensor->ops->ctrl_data = SHUT_DOWN(1);\r
91         }\r
92 \r
93         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
94 \r
95         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
96         if(result)\r
97                 printk("%s:fail to active sensor\n",__func__);\r
98         \r
99         return result;\r
100 \r
101 }\r
102 \r
103 \r
104 static int sensor_init(struct i2c_client *client)\r
105 {       \r
106         struct sensor_private_data *sensor =\r
107             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
108         int result = 0;\r
109         \r
110         result = sensor->ops->active(client,0,0);\r
111         if(result)\r
112         {\r
113                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
114                 return result;\r
115         }\r
116         \r
117         sensor->status_cur = SENSOR_OFF;\r
118         return result;\r
119 }\r
120 \r
121 \r
122 static int light_report_value(struct input_dev *input, int data)\r
123 {\r
124         unsigned char index = 0;\r
125         \r
126         if(data <= 10){\r
127                 index = 0;goto report;\r
128         }\r
129         else if(data <= 160){\r
130                 index = 1;goto report;\r
131         }\r
132         else if(data <= 225){\r
133                 index = 2;goto report;\r
134         }\r
135         else if(data <= 320){\r
136                 index = 3;goto report;\r
137         }\r
138         else if(data <= 640){\r
139                 index = 4;goto report;\r
140         }\r
141         else if(data <= 1280){\r
142                 index = 5;goto report;\r
143         }\r
144         else if(data <= 2600){\r
145                 index = 6;goto report;\r
146         }\r
147         else{\r
148                 index = 7;goto report;\r
149         }\r
150 \r
151 report:\r
152         input_report_abs(input, ABS_MISC, index);\r
153         input_sync(input);\r
154         \r
155         return index;\r
156 }\r
157 \r
158 \r
159 static int sensor_report_value(struct i2c_client *client)\r
160 {\r
161         struct sensor_private_data *sensor =\r
162             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
163         int result = 0;\r
164         //char msb = 0, lsb = 0;\r
165         char data[2] = {0};\r
166         unsigned short value = 0;\r
167         int index = 0;\r
168         \r
169         //sensor->client->addr = CM3232_ADDR_DATA;\r
170         data[0] = CM3232_ADDR_DATA;\r
171         sensor_rx_data(sensor->client, data, 2);\r
172         value = (data[1] << 8) | data[0] ;\r
173 \r
174         DBG("%s:result=%d\n",__func__,value);\r
175         //printk("%s:result=%d\n",__func__,value);\r
176         index = light_report_value(sensor->input_dev, value);   \r
177         DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);\r
178 \r
179         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register\r
180         {\r
181                 \r
182                 result= sensor_read_reg(client, sensor->ops->int_status_reg);\r
183                 if(result)\r
184                 {\r
185                         printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);\r
186                 }\r
187         }\r
188         \r
189         return result;\r
190 }\r
191 \r
192 \r
193 struct sensor_operate light_cm3232_ops = {\r
194         .name                           = "cm3232",\r
195         .type                           = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct\r
196         .id_i2c                         = LIGHT_ID_CM3232,      //i2c id number\r
197         .read_reg                       = CM3232_ADDR_DATA,     //read data\r
198         .read_len                       = 2,                    //data length\r
199         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register\r
200         .id_data                        = SENSOR_UNKNOW_DATA,   //device id\r
201         .precision                      = 8,                    //8 bits\r
202         .ctrl_reg                       = CM3232_ADDR_COM,      //enable or disable \r
203         .int_status_reg                 = SENSOR_UNKNOW_DATA,   //intterupt status register\r
204         .range                          = {100,65535},          //range\r
205         .brightness                     = {10,255},             // brightness\r
206         .trig                           = SENSOR_UNKNOW_DATA,           \r
207         .active                         = sensor_active,        \r
208         .init                           = sensor_init,\r
209         .report                         = sensor_report_value,\r
210 };\r
211 \r
212 /****************operate according to sensor chip:end************/\r
213 \r
214 //function name should not be changed\r
215 static struct sensor_operate *light_get_ops(void)\r
216 {\r
217         return &light_cm3232_ops;\r
218 }\r
219 \r
220 \r
221 static int __init light_cm3232_init(void)\r
222 {\r
223         struct sensor_operate *ops = light_get_ops();\r
224         int result = 0;\r
225         int type = ops->type;\r
226         result = sensor_register_slave(type, NULL, NULL, light_get_ops);\r
227         return result;\r
228 }\r
229 \r
230 static void __exit light_cm3232_exit(void)\r
231 {\r
232         struct sensor_operate *ops = light_get_ops();\r
233         int type = ops->type;\r
234         sensor_unregister_slave(type, NULL, NULL, light_get_ops);\r
235 }\r
236 \r
237 \r
238 module_init(light_cm3232_init);\r
239 module_exit(light_cm3232_exit);\r
240 \r
241 \r